Echart 柱形图 相同项提取复用

<!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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值