Echarts立體柱狀圖

天使的同類發表於2020-11-07

echarts繪製漂亮的立體柱狀圖(超詳細)

為了做一個漂亮的立體柱狀圖,幾乎吧echarts所有的柱狀圖都看了一遍,一遍遍的改,終於繪製出我想要的效果,下面是程式碼:
HTML:

<div class="content bgBox">
	<div class="firstTitle">
		<p class="titleName">購買力最強</p>
	</div>
	<div id="buyTop" style="width: 100%;height:90%;"></div>
</div>

JS:

// 購買力最強
var buyTop = echarts.init(document.getElementById('buyTop'));  //圖表容器
function fetchBuytop() {  //定一個方法
    $.ajax({  //傳送ajax請求資料
        type: "POST",
        url: url,
        success: function (e) {
            var areaData = e.data.area;  //橫座標值
            var payPersonNum = e.data.payPersonNum;  //柱狀圖的值
            const VALUE1 = [{    //每個柱子的顏色(可自行改變顏色)
                value: payPersonNum[0],
                itemStyle: {
                    color: 'rgba(0,137,255,1.0)'
                }
            },
            {
                value: payPersonNum[1],
                itemStyle: {
                    color: 'rgba(0,25,255,1.0)'
                }
            },
            {
                value: payPersonNum[2],
                itemStyle: {
                    color: 'rgba(115,5,255,1.0)'
                }
            },
            {
                value: payPersonNum[3],
                itemStyle: {
                    color: 'rgba(215,1,255,1.0)'
                }
            },
            {
                value: payPersonNum[4],
                itemStyle: {
                    color: 'rgba(182,105,86,1.0)'
                }
            }
            ]
            const VALUE2 = [{  //最高值的柱子顏色  就是你所看到的透明那部分
                value: Math.max.apply(null, payPersonNum),    //這個value是柱狀圖的值裡的最大值
                itemStyle: {
                    color: 'rgba(0,137,255,.1)'
                }
            },
            {
                value:  Math.max.apply(null, payPersonNum),
                itemStyle: {
                    color: 'rgba(0,25,255,.1)'
                }
            },
            {
                value:  Math.max.apply(null, payPersonNum),
                itemStyle: {
                    color: 'rgba(115,5,255,.1)'
                }
            },
            {
                value:  Math.max.apply(null, payPersonNum),
                itemStyle: {
                    color: 'rgba(215,1,255,.1)'
                }
            },
            {
                value:  Math.max.apply(null, payPersonNum),
                itemStyle: {
                    color: 'rgba(182,105,86,.1)'
                }
            }
            ]
            // 繪製左側面
            const CubeLeft = echarts.graphic.extendShape({
                shape: {
                    x: 0,
                    y: 0
                },
                buildPath: function (ctx, shape) {
                    // 會canvas的應該都能看得懂,shape是從custom傳入的
                    const xAxisPoint = shape.xAxisPoint
                    const c0 = [shape.x, shape.y]
                    const c1 = [shape.x - 13, shape.y - 13]
                    const c2 = [xAxisPoint[0] - 13, xAxisPoint[1] - 13]
                    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] + 18, xAxisPoint[1] - 9]
                    const c4 = [shape.x + 18, shape.y - 9]
                    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 + 18, shape.y - 9]
                    const c3 = [shape.x + 5, shape.y - 22]
                    const c4 = [shape.x - 13, shape.y - 13]
                    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 = [300, 300, 300, 300, 300]
            const VALUE = payPersonNum
            buyTopOption = {
                backgroundColor: "transparent",
                tooltip: {
                    textStyle: {
                        color: '#fff',
                        fontStyle: 'normal',
                        fontFamily: '微軟雅黑',
                        fontSize: 12,
                    },
                    trigger: 'axis',
                    axisPointer: {
                        lineStyle: {
                            color: {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [{
                                    offset: 0,
                                    color: 'rgba(0, 255, 233,0)'
                                }, {
                                    offset: 0.5,
                                    color: 'rgba(255, 255, 255,1)',
                                }, {
                                    offset: 1,
                                    color: 'rgba(0, 255, 233,0)'
                                }],
                                global: false
                            }
                        },
                    },
                    formatter: function (params, ticket, callback) {
                        const item = params[1]
                        return item.name + ' : ' + item.value + '人';
                    }
                },
                grid: {  //圖表的上下左右的距離
                    left: '1%',
                    right: '4%',
                    bottom: '10%',
                    top: '20%',
                    padding: '0 0 0 0',
                    containLabel: true,
                },
                xAxis: {  //x軸的樣式設定
                    type: 'category',
                    data: areaData,
                    axisLine: {
                        show: false,
                        lineStyle: {
                            color: 'rgba(255, 168, 1,1.0)'
                        }
                    },
                    offset: 20,
                    axisTick: {
                        show: false,
                    },
                    axisLabel: {
                        interval: 0,
                        margin: 0,
                        textStyle: {
                            color: '#7B4D9F',
                            fontStyle: 'normal',
                            fontFamily: '微軟雅黑',
                            fontSize: 12,
                        }
                    }
                },
                yAxis: {  //y軸的樣式設定
                    type: 'value',
                    name: "人數",
                    nameTextStyle: {
                        color: '#7B4D9F',
                        fontSize: 15,
                        offset: [2, -25]
                    },
                    axisLine: {
                        show: false,
                        lineStyle: {
                            color: '#7B4D9F'
                        }
                    },
                    splitLine: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        fontSize: 16
                    },
                    boundaryGap: false
                },
                series: [{    //下面就是柱狀圖的每個柱的顏色設定
                    type: 'custom',
                    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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                        offset: 0,
                                        color: api.style().fill
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(210, 218, 226,1.0)'
                                    }
                                    ])
                                }
                            },
                            {
                                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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                        offset: 0,
                                        color: api.style().fill
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(210, 218, 226,1.0)'
                                    }
                                    ])
                                }
                            },
                            {
                                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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                        offset: 0,
                                        color: api.style().fill
                                    },
                                    {
                                        offset: 1,
                                        color: api.style().fill
                                    }
                                    ])
                                }
                            }
                            ]
                        }
                    },
                    data: VALUE2
                }, {
                    type: 'custom',
                    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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                        offset: 0,
                                        color: api.style().fill
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(210, 218, 226,1.0)'
                                    }
                                    ])
                                }
                            },
                            {
                                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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                        offset: 0,
                                        color: api.style().fill
                                    },
                                    {
                                        offset: 1,
                                        color: 'rgba(210, 218, 226,1.0)'
                                    }
                                    ])
                                }
                            },
                            {
                                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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                        offset: 0,
                                        color: api.style().fill
                                    },
                                    {
                                        offset: 1,
                                        color: api.style().fill
                                    }
                                    ])
                                }
                            }
                            ]
                        }
                    },
                    data: VALUE1
                }, {
                    type: 'bar',
                    label: {
                        normal: {
                            show: true,
                            position: 'top',
                            fontSize: 14,
                            color: '#fff',
                            offset: [2, -25]
                        }
                    },
                    itemStyle: {
                        color: 'transparent'
                    },
                    tooltip: {

                    },
                    data: VALUE
                }]
            }
            buyTop.setOption(buyTopOption);
        },
        error: function () {
            console.log("介面調取失敗")
        }
    });
}
fetchBuytop()  //最後呼叫下剛剛設定的方法

不要看程式碼多,其實都是基礎配置,都很簡單的,相信你能看的懂的,哈哈哈哈~~~
最後就是效果圖了,是不是很好看,哈哈哈!!!

在這裡插入圖片描述

相關文章