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/echarts@5.2.2/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>
相关推荐
liliangcsdn2 分钟前
mac mlx大模型框架的安装和使用
java·前端·人工智能·python·macos
CssHero5 分钟前
基于vue3完成领域模型架构建设
前端
PanZonghui8 分钟前
用项目说话:我的React博客构建成果与经验复盘
前端·react.js·typescript
挽淚11 分钟前
JavaScript 数组详解:从入门到精通
javascript
言兴12 分钟前
教你如何理解useContext加上useReducer
前端·javascript·面试
sunbyte15 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
前端·javascript·css·vue.js·tailwindcss
前端的日常16 分钟前
网页视频录制新技巧,代码实现超简单!
前端
前端的日常18 分钟前
什么是 TypeScript 中的泛型?请给出一个使用泛型的示例。
前端
今禾19 分钟前
一行代码引发的血案:new Array(5) 到底发生了什么?
前端·javascript·算法
ccc101822 分钟前
老师问我localhost和127.0.0.1,有什么区别?
前端