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>
相关推荐
燃先生._.20 分钟前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
高山我梦口香糖1 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
m0_748235241 小时前
前端实现获取后端返回的文件流并下载
前端·状态模式
m0_748240252 小时前
前端如何检测用户登录状态是否过期
前端
black^sugar2 小时前
纯前端实现更新检测
开发语言·前端·javascript
寻找沙漠的人3 小时前
前端知识补充—CSS
前端·css
GISer_Jing3 小时前
2025前端面试热门题目——计算机网络篇
前端·计算机网络·面试
m0_748245523 小时前
吉利前端、AI面试
前端·面试·职场和发展
理想不理想v3 小时前
webpack最基础的配置
前端·webpack·node.js