Echart 柱形图 相同项提取复用

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
    <style>
        .chart-item {
            width: 50%;
            height: 300px;
            margin: 48px auto;
        }
    </style>
</head>

<body>
    <div class="chart-item" id="chart"></div>
    <script>
        // 提取
        // 把会重复用到的部分提取出来,可以封装到另一个js文件中
        var commonTool = {
            opt: {
                axis: {
                    splitLine: {
                        show: true,
                        lineStyle: {
                            type: 'dashed',
                            color: "#e0e0e0",
                            width: 1
                        }
                    },
                    axisLine: {
                        show: true,
                        lineStyle: {
                            color: "#e0e0e0",
                            width: 1
                        }
                    },
                    axisLabel: function (opt) {
                        var defaulte = {
                            textStyle: {
                                color: "#666",
                                fontSize: 12
                            },
                            fontFamily: "Microsoft YaHei",
                            margin: 15

                        };
                        var opt = $.extend("", defaulte, opt);
                        return opt;
                    },
                }
            }
        }
        // 提取 END

        var myChartArr = [];
        function drawChart(id, data) {
            var myEchart = echarts.init(document.getElementById(id));
            var option = {
                tooltip: {
                    trigger: "axis",
                    backgroundColor: "rgba(0,0,0,0.66)",
                    borderColor: 'rgba(0,0,0,0)',
                    axisPointer: {
                        type: "shadow",
                        label: {
                            show: false,
                        },
                    },
                    textStyle: {
                        color: '#fff',
                        fontSize: 12,
                    },
                },
          
                grid: {
                    left: "6%",
                    right: "3%",
                    bottom: "3%",
                    top: "15%",
                    containLabel: true,
                    z: 22,
                },
                xAxis: [{
                    type: "category",
                    data: data[0],
                    axisLabel: commonTool.opt.axis.axisLabel(),
                    axisLine: commonTool.opt.axis.axisLine,
                    axisTick: commonTool.opt.axis.axisTick,
                    z: 1
                }],
                yAxis: [{
                    type: "value",
                    name: "%",
                    nameTextStyle: {
                        color: '#666',
                        fontSize: 12,
                        padding: [0, 40, 3, 0]
                    },
                    axisLabel: commonTool.opt.axis.axisLabel(),
                    splitLine: commonTool.opt.axis.splitLine,
                    splitArea: {
                        show: false
                    },
                    axisTick: {
                        show: false
                    },
                    axisLine: {
                        show: false,
                    },
                    z: 1
                }],
                series: [{
                    name: "item1",
                    type: "bar",
                    barWidth: "12",
                    barGap: "80%",
                    data: data[1],
                    label: {
                        normal: {
                            show: true,
                            position: 'top',
                            color: "#333",
                            distance: 0,
                            formatter: '{c}%',
                            fontSize: 10
                        }
                    },
                    z: 1
                }, {
                    name: "item2",
                    type: "bar",
                    barWidth: "12",
                    barGap: "80%",
                    data: data[2],
                    label: {
                        normal: {
                            show: true,
                            position: 'top',
                            color: "#333",
                            distance: 0,
                            formatter: '{c}%',
                            fontSize: 10
                        }
                    },
                    z: 1
                }, {
                    name: "item3",
                    type: "bar",
                    barWidth: "12",
                    barGap: "80%",
                    data: data[3],
                    label: {
                        normal: {
                            show: true,
                            position: 'top',
                            color: "#333",
                            distance: 0,
                            formatter: '{c}%',
                            fontSize: 10
                        }
                    },
                    z: 1
                }]
            };
            myEchart.setOption(option)
            myChartArr.push(myEchart)
        }



        var dataX = [],
            dataX2 = [],
            dataY1 = [],
            dataY2 = [],
            dataY3 = [];
        for (var i = 0; i < 4; i++) {
            dataX.push("x" + i);
            dataY1.push(Math.ceil(Math.random() * 1000 % 100));
            dataY2.push(Math.ceil(Math.random() * 1000 % 100));
            dataY3.push(Math.ceil(Math.random() * 1000 % 100));
        }
        drawChart('chart', [dataX, dataY1, dataY2, dataY3])




        $(window).resize(function () {
            for (var i = 0; i < myChartArr.length; i++) {
                myChartArr[i].resize();
            }
        })
    </script>
</body>

</html>
相关推荐
codingandsleeping2 分钟前
pnpm + monorepo:高效的项目管理方式
前端
程序员三千_19 分钟前
最近爆火的MCP到底是什么?
前端
古时的风筝32 分钟前
暴论:2025年,程序员必学技能就是MCP
前端·后端·mcp
古时的风筝32 分钟前
这编程圈子变化太快了,谁能告诉我 MCP 是什么
前端·后端·mcp
王月lydia37 分钟前
环境变量篇-vue3的H5项目从0到1工程化落地经验篇2
前端
赵要上天37 分钟前
利用TTP协议 ETag + 路由守卫 实现前端发版后通知用户更新得一个方案
前端
李剑一39 分钟前
写一个vitepress新建文章脚本,自动化创建链接,别再手写了!
前端·node.js·vitepress
火星思想40 分钟前
你来说说JavaScript作用域
javascript·ecmascript 6
火星思想40 分钟前
React如何实现时间切片
前端·react.js
小学生豆豆1 小时前
eslint以及其扩展插件
前端