使用echarts实现3d柱状图+折线图

以下代码有问题请直接问国内直连GPT/Claude
HTML

需要注意threeDchart一定要设置宽度高度,不然图不显示,然后echarts版本不要太低,不然也不显示

html 复制代码
<div id="threeDchart" class="threeDchart"></div>

js

javascript 复制代码
     set3DBarChart2(data) {
            var myChart = echarts.init(document.getElementById('middle-right-top-chart'));
            data = [2000, 1529, 2251, 1173];
            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 - 9, shape.y - 9];
                    const c2 = [xAxisPoint[0] - 9, xAxisPoint[1] - 9];
                    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 + 9, shape.y - 18];
                    const c4 = [shape.x - 9, 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();
                },
            });
            echarts.graphic.registerShape('CubeLeft', CubeLeft);
            echarts.graphic.registerShape('CubeRight', CubeRight);
            echarts.graphic.registerShape('CubeTop', CubeTop);
        
          let  option = {
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true,
                },
                tooltip: {
                    trigger: 'item',
                    formatter: (params) => {
                        let markerArr = params.marker.split('color:');
                        let marker = markerArr[0] + 'color:#1472FF;"></span>';
                        dataStr =
                            `<div style="color:#000A3A">
                            <div style="color:#666666">${params.name}</div>
                            <div>` +
                            marker +
                            `<span>合同金额</span>` +
                            `<span style="margin-left:15px">${params.data}万元</span></div>` +
                            `</div>`;
                        return dataStr;
                    },
                },
                xAxis: {
                    type: 'category',
                    data: ['隆德矿业', '榆横煤电', '锦兴能源', '不连沟'],
                    axisTick: {
                        show: false, // 不显示坐标轴刻度线
                    },
                    axisPoint: {
                        type: 'shadow',
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#666666',
                            fontSize: '14',
                        },
                    },
                },
                yAxis: [
                    {
                        type: 'value',
                        min: 0,
                        name: '万元',
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#F0F0F0',
                            },
                        },
                        nameTextStyle: {
                            color: '#666666',
                            padding: [0, 30, 0, 0],
                        },
                        axisLabel: {
                            color: '#666666',
                        },
                    },
                    // 右侧纵向坐标轴
                    {
                        type: 'value',
                        name: '折线图值',
                        position: 'right', // 右侧显示
                        splitLine: {
                            show: false, // 不显示右侧坐标轴的分隔线
                        },
                        axisLabel: {
                            color: '#FF7F50',
                        },
                        nameTextStyle: {
                            color: '#FF7F50',
                        },
                    }
                ],
                series: [
                    // 3D柱状图
                    {
                        type: 'custom',
                        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: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                                {
                                                    offset: 0,
                                                    color: '#1D6CE2',
                                                },
                                                {
                                                    offset: 1,
                                                    color: 'rgba(51, 135, 234, 0.10)',
                                                },
                                            ]),
                                        },
                                    },
                                    {
                                        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: 'rgba(51, 124, 234, 1)',
                                                },
                                                {
                                                    offset: 0.3,
                                                    color: 'rgba(51, 124, 234, 0.97)',
                                                },
                                                {
                                                    offset: 0.7,
                                                    color: 'rgba(51, 135, 234, 0.36)',
                                                },
                                                {
                                                    offset: 1,
                                                    color: 'rgba(51, 143, 234, 0.10)',
                                                },
                                            ]),
                                        },
                                    },
                                    {
                                        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: '#1472FF',
                                        },
                                    },
                                ],
                            };
                        },
                        data: data,
                    },
                    // 折线图
                    {
                        type: 'line',
                        name: '折线图',
                        smooth: true, // 平滑曲线
                        data: [2000, 1800, 2300, 1500], // 你可以根据实际数据修改这个数组
                        lineStyle: {
                            color: '#FF7F50',
                            width: 3,
                        },
                        symbol: 'circle', // 设置折线图标记为圆形
                        symbolSize: 8, // 设置圆形标记的大小
                        yAxisIndex: 1, // 使用右侧的 y 轴
                    },
                ],
            };
        
            myChart.setOption(option);
        
            window.addEventListener('resize', function () {
                myChart.resize();
            });
        },  
相关推荐
华玥作者15 小时前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai
Mr Xu_15 小时前
告别冗长 switch-case:Vue 项目中基于映射表的优雅路由数据匹配方案
前端·javascript·vue.js
前端摸鱼匠16 小时前
Vue 3 的toRefs保持响应性:讲解toRefs在解构响应式对象时的作用
前端·javascript·vue.js·前端框架·ecmascript
sleeppingfrog16 小时前
zebra通过zpl语言实现中文打印(二)
javascript
lang2015092816 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
好家伙VCC17 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc
未来之窗软件服务17 小时前
未来之窗昭和仙君(六十五)Vue与跨地区多部门开发—东方仙盟练气
前端·javascript·vue.js·仙盟创梦ide·东方仙盟·昭和仙君
baidu_2474386117 小时前
Android ViewModel定时任务
android·开发语言·javascript
嘿起屁儿整17 小时前
面试点(网络层面)
前端·网络
VT.馒头18 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript