echarts实现正负轴柱状图

效果: 

 data变量配置:

     // 正负柱状图
            zhengfuZhu: {},
            data1: [],
            data2: [],
            data3: [],
            data4: [],
            xAxisData1: [],//横轴刻度
            seriesLabel: { //柱子上的字体
                show: false,
                position: 'outside',
                color: '#BEC3EB',
            },
            seriesEmphasis: { //
                itemStyle: {
                    shadowBlur: 20,
                    shadowColor: 'rgba(0,0,0,0.3)'
                }
            },
            seriesBarWidth: 17,//设置柱子粗细
            seriesYears: ['2022', '2023'],
            seriesColors: ['#4A86DE', '#ECE058', '#23C16D', '#F4AB67'], // 设置柱子颜色

html:

<div class="byn_box">
     <div id="lineEcharts" style="width:400px;height:300px"></div>
</div>

methods:

drawLine(){
        this.zhengfuZhu = {
                legend: {
                    data: [`${this.seriesYears[0]}计划完成任务`, `${this.seriesYears[0]}实际完成任务`,
                    `${this.seriesYears[1]}计划完成任务`, `${this.seriesYears[1]}实际完成任务`],
                    right: '10%',
                    icon: 'circle',
                    textStyle: {
                        color: ' #BEC3EB',
                        fontSize: 13
                    },
                    itemWidth: 12, // 设置宽度
                    itemHeight: 12, // 设置高度
                    itemGap: 15,
                    formatter: function (value) {
                        return value
                    },
                },
                tooltip: {
                    formatter: function (params) {
                        var value = params.value;
                        //首先要看看params是怎样的数据结构,里面有什么内容;
                        if (value < 0) {
                            value = -value
                        }
                        return params.seriesName + ':' + value + ''
                    }
                },
                xAxis: {
                    data: this.xAxisData1,
                    splitArea: { show: false },
                    axisLabel: {
                        textStyle: {
                            color: '#BEC3EB',
                            fontSize: 13
                        },

                    },
                },
                yAxis: [
                    {
                        type: 'value',
                        splitNumber: 10,
                        splitLine: {
                            show: true, lineStyle: {
                                color: '#6469A1',
                                width: 1,
                                type: 'solid'
                            }
                        },
                        axisLabel: {
                            formatter: function (value) {
                                // Function formatter
                                if (value < 0) {
                                    value = -value
                                } else {
                                    value = value
                                }
                                return value + ''
                            },
                            textStyle: {
                                color: ' #BEC3EB',
                                fontSize: 13
                            },
                        },
                    }],
                grid: {
                    bottom: 25,
                    top: 35,
                    right: 0
                },
                series: [
                    {
                        name: `${this.seriesYears[0]}计划完成任务`,
                        type: 'bar',
                        stack: 'one',
                        label: this.seriesLabel,
                        emphasis: this.seriesEmphasis,
                        data: this.data1,
                        barWidth: this.seriesBarWidth,
                        itemStyle: {
                            // 柱状图颜色
                            color: this.seriesColors[0]
                        }
                    },
                    {
                        name: `${this.seriesYears[0]}实际完成任务`,
                        type: 'bar',
                        label: this.seriesLabel,
                        stack: 'two',
                        emphasis: this.seriesEmphasis,
                        barWidth: this.seriesBarWidth,
                        data: this.data3,
                        itemStyle: {
                            // 柱状图颜色
                            color: this.seriesColors[1]
                        }
                    },
                    {
                        name: `${this.seriesYears[1]}实际完成任务`,
                        type: 'bar',
                        label: this.seriesLabel,
                        stack: 'one',
                        emphasis: this.seriesEmphasis,
                        data: this.data2,
                        barWidth: this.seriesBarWidth,
                        itemStyle: {
                            // 柱状图颜色
                            color: this.seriesColors[2]
                        }
                    },
                    {
                        name: `${this.seriesYears[1]}计划完成任务`,
                        type: 'bar',
                        label: this.seriesLabel,
                        stack: 'two',
                        emphasis: this.seriesEmphasis,
                        barWidth: this.seriesBarWidth,
                        data: this.data4,
                        itemStyle: {
                            // 柱状图颜色
                            color: this.seriesColors[3]
                        }
                    },
                ],
            }

            var myChart2 = echarts.init(document.getElementById('lineEcharts'));
            myChart2.setOption(this.zhengfuZhu);
}

在mounted获取数据并调用:

 mounted() {
        for (let i = 0; i < 12; i++) {
            this.xAxisData1.push(i + 1 + "月");
            this.data1.push(+(Math.random() * 200).toFixed(2));
            this.data3.push(+(Math.random() * 500).toFixed(2));
            this.data2.push(-(Math.random() + 300).toFixed(2));
            this.data4.push(-(Math.random() + 100).toFixed(2));
        }
        this.drawLine();
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Echarts 支持立体正负柱状图,可以通过以下步骤实现: 1. 安装 Echarts 如果你还没有安装 Echarts,可以通过以下命令安装: ```bash npm install echarts --save ``` 2. 引入 Echarts 在 HTML 文件中引入 Echarts: ```html <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> ``` 3. 创建容器 在 HTML 文件中创建一个容器,用于显示图表: ```html <div id="chart" style="height: 400px;"></div> ``` 4. 初始化图表 在 JavaScript 文件中初始化图表: ```javascript var myChart = echarts.init(document.getElementById('chart')); ``` 5. 配置数据 配置图表的数据,包括 x 和 y 的数据,以及立体正负柱状图的配置项: ```javascript var option = { tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'value' }, yAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, series: [ { name: 'Positive', type: 'bar', stack: 'Total', label: { show: true, position: 'insideRight' }, data: [10, 20, 30, 40, 50] }, { name: 'Negative', type: 'bar', stack: 'Total', label: { show: true, position: 'insideLeft' }, data: [-10, -20, -30, -40, -50] } ] }; ``` 6. 渲染图表 将配置项设置给图表对象,并渲染图表: ```javascript myChart.setOption(option); ``` 完整代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Echarts 立体正负柱状图</title> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> </head> <body> <div id="chart" style="height: 400px;"></div> <script> var myChart = echarts.init(document.getElementById('chart')); var option = { tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: { type: 'value' }, yAxis: { type: 'category', data: ['A', 'B', 'C', 'D', 'E'] }, series: [ { name: 'Positive', type: 'bar', stack: 'Total', label: { show: true, position: 'insideRight' }, data: [10, 20, 30, 40, 50] }, { name: 'Negative', type: 'bar', stack: 'Total', label: { show: true, position: 'insideLeft' }, data: [-10, -20, -30, -40, -50] } ] }; myChart.setOption(option); </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suoh's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值