Echarts 柱状图及折线图

效果

柱状及折线图表

功能
  1. 防止悬浮框溢出
  2. 图例图标宽高度修改
  3. 柱状图之间间距设置
  4. X轴数据名称柱状图内显示(注释)
  5. X轴数据名称倾斜显示
  6. X轴文字显示优先级(注释)
  7. 柱状图初始显示数量
  8. 柱状图颜色渐变
  9. 折线图点大小
  10. 鼠标点击,滚动,拖动柱状图展示所有数据
代码
let option = {
    tooltip: {
        trigger: 'axis',
        confine: true, // 防止悬浮框溢出图表
    },
    grid: {
        bottom: "21.2%",
        height: 96, 
    },
    legend: {
        data: ['水质达标率', '水量达标率', '水质达标个数', '水量达标个数'],
        textStyle: {  // 图例样式
            color: "#fff",
            fontSize: 9
        },
        itemWidth: 10,  // 设置宽度 图例图标的宽高
        itemHeight: 6, // 设置高度
        itemGap: 4 // 设置间距  图例间距
        // bottom: "1%",
    },
    color: ["#FF7F00", "#00FF00", "#4472C4", "#FFC000"],
    xAxis: [
        {
            // type: 'category',
            data: xAxisData,  // x轴数据
            axisPointer: {
                type: 'shadow'
            },
            axisLabel: {
                // inside: true,  // x轴文字显示在柱状图内
                // formatter: function (value) {
                //     return value.split("").join("\n");
                // },
                textStyle: {
                    fontSize: 9,
                    itemSize: '',
                    color: '#fff'
                },
                rotate: -30,  // x轴文字倾斜显示
            },
            axisLine: {
                // lineStyle: {color: '#37445A'}    // x轴坐标轴颜色
            },
            // z: 10,  // 显示优先级
            splitLine: {  // 网格线样式
                show: false,
                lineStyle: {
                    color: ['#37445A'],
                    width: 1,
                    type: 'solid'
                }
            }
        }
    ],
    yAxis: [
        {
            type: 'value',
            name: '',
            min: 0,
            max: 20,
            // interval: 40,
            axisLabel: {
                formatter: '{value}',
                textStyle: {
                    color: '#fff'
                }
            },
            axisLine: {
                // lineStyle: {color: '#fff'}    // y轴坐标轴颜色
            },
            splitLine: {
                show: true,
                lineStyle: {
                    color: ['#37445A'],
                    width: 1,
                    type: 'solid'
                }
            }
        },
        {
            type: 'value',
            name: '',
            min: 0,
            max: 20,
            // interval: 40,
            axisLabel: {
                formatter: '{value}',
                textStyle: {
                    color: '#fff'
                }
            },
            axisLine: {
                // lineStyle: {color: '#fff'}    // y轴坐标轴颜色
            },
            splitLine: {
                show: true,
                lineStyle: {
                    color: ['#37445A'],
                    width: 1,
                    type: 'solid'
                }
            }
        }
    ],
    dataZoom: [  // 展示前10条数据
        {
            type: 'inside',
            startValue: 0,
            endValue: 9
        }
    ],
    series: [
        {
            name: '水质达标个数',
            type: 'line',
            yAxisIndex: 1,
            symbol: 'circle',     //设定为实心点
            symbolSize: 6,   //设定实心点的大小
            data: lineDataFirst
        },
        {
            name: '水量达标个数',
            type: 'line',
            yAxisIndex: 1,
            symbol: 'circle',     //设定为实心点
            symbolSize: 6,   //设定实心点的大小
            data: lineDataSecond
        },
        {
            name: '水质达标率',
            type: 'bar',
            itemStyle: {  //柱状图颜色渐变
                color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                        {offset: 0, color: '#079FF9'},
                        {offset: 0.5, color: '#0F76BC'},
                        {offset: 1, color: '#1A568A'}
                    ]
                )
            },
            data: barDataFirst
        },
        {
            name: '水量达标率',
            type: 'bar',
            barGap: '0%',  // 柱状图间距0
		 itemStyle: {
                color: new echarts.graphic.LinearGradient(
                    0, 0, 0, 1,
                    [
                        {offset: 0, color: '#D2AD29'},
                        {offset: 0.5, color: '#9C893F'},
                        {offset: 1, color: '#63634B'}
                    ]
                )
            },
            data: barDataSecond
        },
    ]
};
this.drinkBarLine = this.$echarts.init(document.getElementById(id));
// 页面刷时延迟加载,以便柱状图自适应父级div宽高
setTimeout(() => {  
    this.drinkBarLine.resize();
}, 500);
this.drinkBarLine.setOption(option);

// 鼠标点击,滚动,拖动柱状图展示所有数据
var zoomSize = 6;
let _this = this;
this.drinkBarLine.on('click', function (params) {
    _this.drinkBarLine.dispatchAction({
        type: 'dataZoom',
        startValue: xAxisData[Math.max(params.dataIndex - zoomSize / 2, 0)],
        endValue: xAxisData[Math.min(params.dataIndex + zoomSize / 2, (barDataFirst).length - 1)]
    });
});

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值