// style的参考文档 https://ecomfe.github.io/zrender-doc/public/api.html#zrenderdisplayable // 柱体左面 const CubeLeft = echarts.graphic.extendShape({ shape: { x: 0, y: 0 }, buildPath: function (ctx, shape) { const xAxisPoint = shape.xAxisPoint const c0 = [shape.x, shape.y] // 右上点 const c1 = [shape.x - 12, shape.y - 5] // 左上点 const c2 = [xAxisPoint[0] - 12, xAxisPoint[1] - 6] // 左下点 const c3 = [xAxisPoint[0], xAxisPoint[1]] // 右下点 ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath() } }) // 柱体右面 const CubeRight = echarts.graphic.extendShape({ shape: { x: 0, y: 0 }, buildPath: function (ctx, shape) { const xAxisPoint = shape.xAxisPoint const c1 = [shape.x, shape.y] // 左上点 const c2 = [xAxisPoint[0], xAxisPoint[1]] // 左下点 const c3 = [xAxisPoint[0] + 20, xAxisPoint[1] - 6] // 右下点 const c4 = [shape.x + 20, shape.y - 3.5] // 右上点 ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath() } }) // 柱体顶面 const CubeTop = echarts.graphic.extendShape({ shape: { x: 0, y: 0 }, buildPath: function (ctx, shape) { const c1 = [shape.x, shape.y] // 下点 const c2 = [shape.x + 20, shape.y - 4] // 右点 const c3 = [shape.x + 9, shape.y - 8] // 上点 const c4 = [shape.x - 12, shape.y - 5] // 左点 ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath() } }) echarts.graphic.registerShape('CubeLeft', CubeLeft) echarts.graphic.registerShape('CubeRight', CubeRight) echarts.graphic.registerShape('CubeTop', CubeTop) const MAX = [6000, 6000, 6000, 6000, 6000, 5000, 4000, 3000, 2000, 4000, 3000, 2000] const VALUE = [2012, 1230, 3790, 2349, 1654, 1230, 3790, 2349, 1654, 3790, 2349, 1654] const GenerateBarGradient = function () { return new echarts.graphic.LinearGradient(0, 0, 0, 1, [ { offset: 0, color: "#01E2EF", }, { offset: 1, color: "#134E9F", }, ]); } const ChartBgColor = 'rgba(1, 13, 58, 1)'; const OptityBarBgColor = 'rgba(7, 29, 97, .6)' option = { // 图表背景大图 backgroundColor: ChartBgColor, // 标题 title: { text: '测试背景大图', top: 32, left: 18, textStyle: { color: '#00F6FF', fontSize: 24 } }, // 控制图片位置偏移 grid: { left: 20, right: 40, bottom: '19%', top: 107, containLabel: true }, tooltip: { trigger: 'axis' }, xAxis: { type: 'category', data: ['德州', '德城区', '陵城区', '禹城市', '乐陵市', '临邑县', '平原县', '夏津县', '武城县', '庆云县', '宁津县', '齐河县' ], axisLine: { show: true, lineStyle: { color: 'white' } }, offset: 20, axisTick: { show: false, length: 9, alignWithLabel: true, lineStyle: { color: '#7DFFFD' } }, axisLabel: { fontSize: 10 } }, yAxis: { type: 'value', axisLine: { show: true, lineStyle: { color: 'white' } }, splitLine: { show: false }, axisTick: { show: false }, axisLabel: { fontSize: 16 }, boundaryGap: ['20%', '20%'] }, series: [ // 透明的柱体,和背景色是同色系的 { type: 'custom', data: MAX, renderItem: function (params, api) { const location = api.coord([api.value(0), api.value(1)]) return { type: 'group', children: [ { type: 'CubeLeft', shape: { api, xValue: api.value(0), yValue: api.value(1), x: location[0], y: location[1], xAxisPoint: api.coord([api.value(0), 0]) }, style: { fill: OptityBarBgColor } }, { type: 'CubeRight', shape: { api, xValue: api.value(0), yValue: api.value(1), x: location[0], y: location[1], xAxisPoint: api.coord([api.value(0), 0]) }, style: { fill: 'rgba(10,35,108,.7)' } }, { type: 'CubeTop', shape: { api, xValue: api.value(0), yValue: api.value(1), x: location[0], y: location[1], xAxisPoint: api.coord([api.value(0), 0]) }, style: { fill: 'rgba(11,42,106,.8)' } } ] } } }, // 显示柱体文本 { type: 'bar', data: MAX, label: { normal: { show: true, position: 'top', formatter: (e) => { switch (e.name) { case '10kV线路': return VALUE[0] case '公用配变': return VALUE[1] case '35kV主变': return VALUE[2] case '水': } }, fontSize: 16, color: '#fff', offset: [4, -25] } }, itemStyle: { color: 'transparent' } }, // 实际显示的柱体 { type: 'custom', data: VALUE, tooltip: {}, renderItem: (params, api) => { const location = api.coord([api.value(0), api.value(1)]) return { type: 'group', children: [ { type: 'CubeLeft', shape: { api, xValue: api.value(0), yValue: api.value(1), x: location[0], y: location[1], xAxisPoint: api.coord([api.value(0), 0]) }, style: { fill: GenerateBarGradient() } }, { type: 'CubeRight', shape: { api, xValue: api.value(0), yValue: api.value(1), x: location[0], y: location[1], xAxisPoint: api.coord([api.value(0), 0]) }, style: { fill: GenerateBarGradient() } }, { type: 'CubeTop', shape: { api, xValue: api.value(0), yValue: api.value(1), x: location[0], y: location[1], xAxisPoint: api.coord([api.value(0), 0]) }, style: { fill: GenerateBarGradient() } } ] } } } ] }