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>
相关推荐
十八朵郁金香11 分钟前
【JavaScript】深入理解模块化
开发语言·javascript·ecmascript
m0_7482309414 分钟前
Redis 通用命令
前端·redis·bootstrap
YaHuiLiang1 小时前
一切的根本都是前端“娱乐圈化”
前端·javascript·代码规范
菜鸟一枚在这2 小时前
深入解析设计模式之单例模式
开发语言·javascript·单例模式
ObjectX前端实验室2 小时前
个人网站开发记录-引流公众号 & 谷歌分析 & 谷歌广告 & GTM
前端·程序员·开源
CL_IN2 小时前
企业数据集成:实现高效调拨出库自动化
java·前端·自动化
浪九天3 小时前
Vue 不同大版本与 Node.js 版本匹配的详细参数
前端·vue.js·node.js
qianmoQ4 小时前
第五章:工程化实践 - 第三节 - Tailwind CSS 大型项目最佳实践
前端·css
C#Thread4 小时前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
椰果uu4 小时前
前端八股万文总结——JS+ES6
前端·javascript·es6