我用Trae生成了一个Echarts 3D柱状图的Demo

最近在开发个人的大屏项目,作为简历的一部分找工作的时候拿出来撑撑门面。

对于前端而言,最难的并不是技术部分,而是 UI 部分,毕竟前端大部分时候只是切图仔 ,而不是设计师

但是我现在找到了一个新的方案,用 AI 工具。比如我现在用的 Trae,大面上的框架还是建议个人写,毕竟AI虽然智能,但个人才是代码的最终把控者。

Echarts官网上找了找关于柱状图的内容,无法达到以下的效果,在网上搜了搜,发现也没有类似的效果。

于是用 Trae 简单写了一个指令:

请帮我生成一个Echarts柱状图组件,内容是一个柱状图,但是柱子显现出一种3D效果。分别由三部分构成,柱子的顶端是一个四边形,左右两侧各是一个渐变的四边形,三个图形组成一个侧视图的3D柱子效果。

Trae表示这种效果在柱状图中不存在,推荐使用Echarts的custom自行开发,并给出了相应的开发逻辑。

js 复制代码
// 先生成柱子效果
// 绘制左侧面
const barLeft = echarts.graphic.extendShape({
    shape: {
        x: 0,
        y: 0,
    },
    buildPath: function (ctx, shape) {
        const xAxisPoint = shape.xAxisPoint;
        const c0 = [shape.x + 2, shape.y];
        const c1 = [shape.x - 7, shape.y - 1];
        const c2 = [xAxisPoint[0] - 7, xAxisPoint[1] - 3];
        const c3 = [xAxisPoint[0] + 2, 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 barRight = echarts.graphic.extendShape({
    shape: {
        x: 0,
        y: 0,
    },
    buildPath: function (ctx, shape) {
        const xAxisPoint = shape.xAxisPoint;
        const c1 = [shape.x + 2, shape.y];
        const c2 = [xAxisPoint[0] + 2, xAxisPoint[1]];
        const c3 = [xAxisPoint[0] + 10, xAxisPoint[1] - 5];
        const c4 = [shape.x + 10, 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();
    },
});
// 绘制顶面
const barTop = echarts.graphic.extendShape({
    shape: {
        x: 0,
        y: 0,
    },
    buildPath: function (ctx, shape) {
        const c1 = [shape.x + 2, shape.y + 3];
        const c2 = [shape.x + 10, shape.y - 5]; //右点
        const c3 = [shape.x - 0, shape.y - 7];
        const c4 = [shape.x - 7, shape.y - 1];
        ctx
            .moveTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
    },
});

将渲染完成的面注册到Echarts中:

js 复制代码
// 注册三个面图形
echarts.graphic.registerShape("barLeft", barLeft);
echarts.graphic.registerShape("barRight", barRight);
echarts.graphic.registerShape("barTop", barTop);

应用已经注册好的面,生成Echarts柱状图:

js 复制代码
series: [
    {
        type: "custom",
        renderItem: (params, api) => {
            let barLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {offset: 0, color: "rgba(27, 126, 242, 0.8)"},
                {offset: 0.5, color: "rgba(27, 126, 242, 0.6)"},
                {offset: 0.8, color: "rgba(27, 126, 242, 0.3)"},
                {offset: 1, color: "rgba(0,0,0,0.2)"},
            ]);
            let barRightStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {offset: 0, color: "rgba(23,176,255,1)"},
                {offset: 0.5, color: "rgba(23,176,255, 0.6)"},
                {offset: 0.8, color: "rgba(23,176,255, 0.3)"},
                {offset: 1, color: "rgba(0,0,0,0.2)"},
            ]);
            let barTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {offset: 0, color: "rgba(27, 126, 242, 1)"},
                {offset: 1, color: "rgba(27, 126, 242, 1)"},
            ]);
    },
],

完整代码

js 复制代码
const createCharts = (data) => {
    const myChart = echarts.init(document.getElementById('firstCharts'));
    // 绘制左侧面
    const barLeft = echarts.graphic.extendShape({
        shape: {
            x: 0,
            y: 0,
        },
        buildPath: function (ctx, shape) {
            const xAxisPoint = shape.xAxisPoint;
            const c0 = [shape.x + 2, shape.y];
            const c1 = [shape.x - 7, shape.y - 1];
            const c2 = [xAxisPoint[0] - 7, xAxisPoint[1] - 3];
            const c3 = [xAxisPoint[0] + 2, 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 barRight = echarts.graphic.extendShape({
        shape: {
            x: 0,
            y: 0,
        },
        buildPath: function (ctx, shape) {
            const xAxisPoint = shape.xAxisPoint;
            const c1 = [shape.x + 2, shape.y];
            const c2 = [xAxisPoint[0] + 2, xAxisPoint[1]];
            const c3 = [xAxisPoint[0] + 10, xAxisPoint[1] - 5];
            const c4 = [shape.x + 10, 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();
        },
    });
    // 绘制顶面
    const barTop = echarts.graphic.extendShape({
        shape: {
            x: 0,
            y: 0,
        },
        buildPath: function (ctx, shape) {
            const c1 = [shape.x + 2, shape.y + 3];
            const c2 = [shape.x + 10, shape.y - 5]; //右点
            const c3 = [shape.x - 0, shape.y - 7];
            const c4 = [shape.x - 7, shape.y - 1];
            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("barLeft", barLeft);
    echarts.graphic.registerShape("barRight", barRight);
    echarts.graphic.registerShape("barTop", barTop);
    let option = {
        tooltip: {
            trigger: "axis",
            formatter: '<b>{b}</b> <br/> 建设完成度: <b style="color: #2AB6FF">{c}%</b>'
        },
        grid: {
            left: 20,
            top: 10,
            bottom: 0,
            right: 20,
            containLabel: true,
        },
        xAxis: {
            type: "category",
            data: data.name,
            axisLine: {
                show: true,
                lineStyle: {
                    color: "rgba(255,255,255,0.5)",
                },
            },
            axisLabel: {
                interval: 0,
                textStyle: {
                    color: "rgba(255,255,255,0.5)",
                },
            },
            axisTick: {
                show: false,
            }
        },
        yAxis: {
            type: "value",
            axisLine: {
                show: false,
                lineStyle: {
                    color: "#7ebaf2",
                },
            },
            splitLine: {
                show: false,
            },
            axisTick: {
                show: false,
            },
            splitNumber: 4,
            axisLabel: {
                fontSize: 12,
                textStyle: {
                    color: "#FFF",
                },
            },
        },
        series: [
            {
                type: "custom",
                renderItem: (params, api) => {
                    let barLeftStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {offset: 0, color: "rgba(27, 126, 242, 0.8)"},
                        {offset: 0.5, color: "rgba(27, 126, 242, 0.6)"},
                        {offset: 0.8, color: "rgba(27, 126, 242, 0.3)"},
                        {offset: 1, color: "rgba(0,0,0,0.2)"},
                    ]);
                    let barRightStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {offset: 0, color: "rgba(23,176,255,1)"},
                        {offset: 0.5, color: "rgba(23,176,255, 0.6)"},
                        {offset: 0.8, color: "rgba(23,176,255, 0.3)"},
                        {offset: 1, color: "rgba(0,0,0,0.2)"},
                    ]);
                    let barTopStyle = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {offset: 0, color: "rgba(27, 126, 242, 1)"},
                        {offset: 1, color: "rgba(27, 126, 242, 1)"},
                    ]);
                    const location = api.coord([api.value(0), api.value(1)]);
                    return {
                        type: "group",
                        children: [
                            {
                                type: "barLeft",
                                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: barLeftStyle,
                                },
                            },
                            {
                                type: "barRight",
                                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: barRightStyle,
                                },
                            },
                            {
                                type: "barTop",
                                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: barTopStyle,
                                },
                            },
                        ],
                    };
                },
                data: data.value,
            },
        ],
        animation: true,
        animationDuration: 1000
    };
    myChart.setOption(option);
};

Ps: 其他不明之处可以参考 Echarts官方文档 custom 篇

相关推荐
Crystal3282 小时前
3D实战案例(飞行的火箭/创建3D导航/翻书效果/创建长方体/环环相扣效果)
前端·css
6***x5452 小时前
前端组件库发展趋势,原子化CSS会成为主流吗
前端·css
Goboy2 小时前
从 AI Coding 到技术债务,Cloudflare 3个小时才全面恢复,出来混的,迟早要还的
trae
00后程序员张2 小时前
接口调试从入门到精通,Fiddler抓包工具、代理配置与HTTPS抓包实战技巧
前端·ios·小程序·https·fiddler·uni-app·webview
快手技术2 小时前
闪耀NeurIPS 2025!快手13篇论文入选,Spotlight 成果跻身前三!
前端·后端
一 乐2 小时前
宠物猫店管理|宠物店管理|基于Java+vue的宠物猫店管理管理系统(源码+数据库+文档)
java·前端·数据库·vue.js·后端·宠物管理
熊猫比分管理员2 小时前
【全栈源码解决方案】Vue+Java四端齐全,一周交付可运行项目!
java·前端·vue.js
o***74172 小时前
【Nginx 】Nginx 部署前端 vue 项目
前端·vue.js·nginx
坚持就完事了2 小时前
CSS-5:盒子模型
前端·css·html