使用echarts实现类似3D效果的柱状图:效果如下:
HTML
1.首先:定义一个div用来展示echarts图表数据
<div id="centerChart_1" style="width: 100%; height: 508px"></div>
Echarts
分为三个部分(正常正方形柱状图、顶部椭圆形、底部椭圆形)
1.正常柱状图
{
name: '年度检查量',
z: 13,
type: 'bar',
itemStyle: {
normal: {
// barBorderRadius: [0, 0, 36, 30], // 无圆角
color: new echarts.graphic.LinearGradient(
0, 0, 1, 0, [
{ offset: 0, color: '#00357B' },
{ offset: 0.3, color: '#074494' },
{ offset: 0.66, color: '#7FB6FF' },
{ offset: 1, color: '#004CAE' }
]
), opacity: .9,
}
},
silent: true,
barWidth: 42,
barGap: '-100%', // Make series be overlap
data: [122999, 132523, 152452, 172819, 112214],
}
其中柱子颜色设通过使用 new echarts.graphic.LinearGradient()设置为线性渐变
2.顶部椭圆
{
name: '年度检查量',
type: 'pictorialBar',
symbolSize: [42, 20],// 生成圆的半径尺寸
symbolOffset: [0, -10], //位置距离
z: 14,
itemStyle: {
color: {
type: 'linear',
x: 0, y: 0, x2: 0, y2: 1,
colorStops: [
{ offset: 0, color: '#9DC7FE' },
{ offset: 1, color: '#074494' }
]
},
borderColor: '#2760AB', // 模拟边框渐变
borderWidth: 3,
barBorderRadius: [0, 0, 0, 0],
},
data: [{
value: 122999,
symbolPosition: 'end'
}, {
value: 132523,
symbolPosition: 'end'
}, {
value: 152452,
symbolPosition: 'end'
},
{
value: 172819,
symbolPosition: 'end'
}, {
value: 112214,
symbolPosition: 'end'
}
]
},
注意:类型type: 'pictorialBar',数据源data中需要设置symbolPosition:'end'这样图形会在柱子的最上方
3.设置底部椭圆
{
label: {
show: false,
color: "#FFFFFF",
position: 'top',
fontSize: 34,
itemStyle: {
color: "#FFFFFF"
},
offset: [0, -15]
},
emphasis: { // 鼠标悬停时的状态
label: {
show: true // 鼠标悬停时显示数值
}
},
name: '年度检查量',
type: 'pictorialBar',
symbolSize: [42, 20], //生成圆的半径尺寸
symbolOffset: [0, 10], //位置距离
z: 12,
color: new echarts.graphic.LinearGradient(
0, 0, 1, 0, // 渐变方向:从右到左(90度)
[
{ offset: 0, color: 'rgba(0,53,123,1)' }, // 开始颜色
{ offset: 0.3, color: 'rgba(7,68,148,1)' }, // 30%位置的颜色
{ offset: 0.66, color: 'rgba(147,194,255,1)' },// 66%位置的颜色
{ offset: 1, color: 'rgba(0,76,174,1)' } // 结束颜色
]
),
opacity: .1,
data: [122999, 132523, 152452, 172819, 112214]
},
完整代码
myEcharts.setOption({
grid: {
top: "10%",
bottom: '15%',
left: '20%', // 增加图表底部的空白区域
right: '2%' // 增加图表底部的空白区域
},
tooltip: {
axisPointer: {
type: 'shadow', // 可以是 'line' 或 'shadow'
lineStyle: {
show: false, // 隐藏悬浮线
},
shadowStyle: {
color: 'rgba(0, 0, 0, 0)', // 如果使用阴影模式,可以设置透明色来避免阴影显示
}
},
trigger: 'axis',
show: true,
formatter: function (params) {
return params[2].seriesName + '<br/>'
+ params[2].name + ': ' + params[2].value;
},
textStyle: {
fontSize: 34,
color: '#FFFFFF'
},
backgroundColor: 'rgba(5, 18, 46,0.9)', // 背景颜色
borderColor: '#ccc', // 边框颜色
borderWidth: 0, // 边框宽度
},
xAxis: [{
data: [2021, 2022, 2023, 2024, 2025],
axisLabel: {
textStyle: {
color: '#fff',
fontSize: 32
},
margin: 20, //刻度标签与轴线之间的距离。
},
axisLine: {
show: false //不显示x轴
},
axisTick: {
show: false //不显示刻度
},
boundaryGap: true,
splitLine: {
show: false,
width: 0.08,
lineStyle: {
type: "solid",
color: "#03202E"
}
}
}],
yAxis: [{
// min: -20,
splitLine: {
show: true,
lineStyle: {
width: 4,
color: 'rgba(229,240,255,0.2)',
type: 'solid'
}
},
axisTick: {
show: true,
length: 20,
margin: 10,
lineStyle: {
width: 4,
color: '#fff',
type: 'solid'
}
},
axisLine: {
show: false
},
axisLabel: {
margin: 40,
textStyle: {
color: '#fff',
fontSize: 32
},
}
}],
series: [{
name: '年度检查量',
type: 'pictorialBar',
symbolSize: [42, 20],
symbolOffset: [0, -10],
z: 14,
itemStyle: {
color: {
type: 'linear',
x: 0, y: 0, x2: 0, y2: 1,
colorStops: [
{ offset: 0, color: '#9DC7FE' },
{ offset: 1, color: '#074494' }
]
},
borderColor: '#2760AB', // 模拟边框渐变
borderWidth: 3,
barBorderRadius: [0, 0, 0, 0],
},
data: [{
value: 122999,
symbolPosition: 'end'
}, {
value: 132523,
symbolPosition: 'end'
}, {
value: 152452,
symbolPosition: 'end'
},
{
value: 172819,
symbolPosition: 'end'
}, {
value: 112214,
symbolPosition: 'end'
}
]
},
{
label: {
show: false,
color: "#FFFFFF",
position: 'top',
fontSize: 34,
itemStyle: {
color: "#FFFFFF"
},
offset: [0, -15]
},
emphasis: { // 鼠标悬停时的状态
label: {
show: true // 鼠标悬停时显示数值
}
},
name: '年度检查量',
type: 'pictorialBar',
symbolSize: [42, 20],
symbolOffset: [0, 10],
z: 12,
color: new echarts.graphic.LinearGradient(
0, 0, 1, 0, // 渐变方向:从右到左(90度)
[
{ offset: 0, color: 'rgba(0,53,123,1)' }, // 开始颜色
{ offset: 0.3, color: 'rgba(7,68,148,1)' }, // 30%位置的颜色
{ offset: 0.66, color: 'rgba(147,194,255,1)' },// 66%位置的颜色
{ offset: 1, color: 'rgba(0,76,174,1)' } // 结束颜色
]
),
opacity: .1,
data: [122999, 132523, 152452, 172819, 112214]
},
{
name: '年度检查量',
z: 13,
type: 'bar',
itemStyle: {
normal: {
// barBorderRadius: [0, 0, 36, 30], // 无圆角
color: new echarts.graphic.LinearGradient(
0, 0, 1, 0, [
{ offset: 0, color: '#00357B' },
{ offset: 0.3, color: '#074494' },
{ offset: 0.66, color: '#7FB6FF' },
{ offset: 1, color: '#004CAE' }
]
), opacity: .9,
}
},
silent: true,
barWidth: 42,
barGap: '-100%', // Make series be overlap
data: [122999, 132523, 152452, 172819, 112214],
}
]
}
)