柱状图-3D

描述:当前是关于Echarts图表中的 柱状图 示例。
 
            // 绘制左侧面
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 = [800,900, 800, 800, 800, 800, 800]
const VALUE = [210.9, 260.8, 204.2, 504.9, 740.5, 600.3, 119.0]
option = {
    // 标题组件,包含主标题和副标题。
    title: {
        id: 'bar-chart',
        show: true,
        text: '3D柱状图标题',
        textStyle: {
            color: '#fff'
        },
        subtext: '副标题副标题',
        subtextStyle: {
            color: '#eee'
        },
        left: '20',//标题位置
        top: '20'//标题位置
    },
    // 图例组件
    legend: {
        type: 'plain',//plain:普通图例;scroll:可滚动翻页的图例。当图例数量较多时使。
        show: true,
        top: '50',
        right: '50',
        left: 'center',
        // 图例的公用文本样式
        textStyle: {
            color: '#fff'
        }
    },
    // 直角坐标系内绘图网格
    grid: {
        show: false,//是否显示直角坐标系网格,外边框
        top: '150',//离容器左侧的距离
        right: '50',//离容器上侧的距离
        left: '20',//离容器左侧的距离
        bottom: '12%',//离容器底部的距离
        borderColor: '#ff0',//外边框颜色
        containLabel: true,//是否包含坐标轴的刻度标签,默认为false;true时防止标签溢出
    },
    // 提示框组件
    tooltip: {
        trigger: 'axis',//触发类型,axis:坐标轴触发
        axisPointer: {//坐标轴指示器配置项
            type: 'shadow'//指示器类型:line、shadow、none、cross
        },
        backgroundColor: 'rgba(50,50,50,0.7)',//提示框浮层的背景颜色
        borderColor: '#fff',//提示框浮层的边框颜色
        textStyle: {
            color: '#fff',
            fontSize: '15'
        },
        // {a}(系列名称) {b}(x轴的值),{c}(y轴的值)
        formatter: '{b}:<br />{a0}:{c0}万元'//1.字符串模板
        // formatter:function(params){  //2.回调函数,可return dom 自定义样式
        //     console.log('params',params)
        //     return params[0].name
        // }
    },
    //图表背景色
    backgroundColor: '#081736',
    // x 轴设置
    xAxis: [{
        type: 'category',
        name: '月份',//X轴名称
        nameLocation: 'end',//X轴名称位置
        nameTextStyle: {//X轴名称样式
            color: '#fff',
            fontWeight: 'bold'
        },
        nameGap: 10,//X轴名称与轴线之间的距离
        nameRotate: 0,//坐标轴名称旋转
        axisLabel: {//X轴类目名称样式
            color: '#fff',
            rotate: 0 //X轴类目名称旋转角度
        },
        axisLine: { //X轴轴线设置
            show: true,
            lineStyle: {
                color: '#fff',
                width: 2
            },
        },
        axisTick: {//X轴刻度相关设置
            show: false,
        },
        splitLine: {// 横向分隔线
            show: true,
            lineStyle: {
                color: '#195384',
                type: "dotted",
            }
        },
         data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月'],

    }],
    // y轴设置
    yAxis: [
        {
            type: 'value',
            position: 'left',
            name: "万元",
            nameTextStyle: {
                color: "#fff",
                fontSize: 12,
                fontWeight: 'bold'
            },
            min: 0,//坐标轴刻度最小值
            axisLine: {//y轴轴线设置
                show: true,
                lineStyle: {
                    color: '#fff',
                    width: 2
                }
            },
            axisLabel: {//y轴刻度标签
                formatter: '{value}',
                inside: false,//刻度标签是否朝内,默认朝外
                textStyle: {
                    color: "#fff",
                }
            },
            axisTick: {//刻度设置
                show: false,
            },
            splitLine: {//纵向分隔线
                show: true,
                lineStyle: {
                    color: '#0a3e98',
                    type: "dotted",
                }
            },
        }
    ],
    series: [ {
        type: 'custom',
        name:'销售额',
        renderItem: function(params, api) {
            const location = api.coord([api.value(0), api.value(1)])
            return {
                type: 'group',
                children: [{
                    type: 'CubeLeft',
                    shape: {
                        api,
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0])
                    },
                    style: {
                        fill: 'rgba(47,102,192,.27)'
                    }
                }, {
                    type: 'CubeRight',
                    shape: {
                        api,
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0])
                    },
                    style: {
                        fill: 'rgba(59,128,226,.27)'
                    }
                }, {
                    type: 'CubeTop',
                    shape: {
                        api,
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0])
                    },
                    style: {
                        fill: 'rgba(72,156,221,.27)'
                    }
                }]
            }
        },
        data: MAX
    },
     {
        type: 'custom',
        renderItem: (params, api) => {
            const location = api.coord([api.value(0), api.value(1)])
            var color = api.value(1) > 800 ? 'red' : new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: 'rgba(37,170,254,1)'
                },
                {
                    offset: 0.8,
                    color: 'rgba(37,170,254,0.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: color
                    }
                }, {
                    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: color
                    }
                }, {
                    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: color
                    }
                }]
            }
        },
        data: VALUE
    },
    {
        type: 'bar',
        label: {
            show: true,
                position: 'top',
                fontSize: 16,
                color: '#fff',
                offset: [2, -25]
        },
        itemStyle: {
            color: 'transparent',
        },
        tooltip: {

        },
        data: MAX
    }],
};