echarts折线图,折线如何实现偏移??

我想要这个样子的:clipboard.png
而实际搞出来这个样子的

clipboard.png
折线的起点根本不是随着柱子动的,而是在两个柱子的中间,求问各位大神how to do啊,查了配置项手册,根本没有看到属性啊!!!!
最后,跪求!

阅读 12.4k
2 个回答

增加了一个隐藏的x轴,用来控制线图的点的位置.

参考下面的例子

option = {
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            crossStyle: {
                color: '#999'
            }
        }
    },
    xAxis: [{
        type: 'category',
        data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        axisPointer: {
            type: 'shadow'
        }
    }, ],
    yAxis: [{
            type: 'value',
            name: '水量',
            min: 0,
            max: 250,
            interval: 50,
            axisLabel: {
                formatter: '{value} ml'
            }
        }
    ],
    series: [{
            name: '柱1',
            type: 'bar',
            data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
            z: 1
        },
        {
            name: '柱2',
            type: 'bar',
            data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
            z: 1

        },
        {
            xAxisIndex: 1,
            name: '线1',
            type: 'line',
            itemStyle:{
                normal:{
                    lineStyle:{
                        color: '#c23c31',
                        type:'dotted' //'dotted'虚线 'solid'实线
                    }
                }
            }, 
            data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3],
        },
        {
            xAxisIndex: 1,
            name: '线2',
            type: 'line',
            itemStyle:{
                normal:{
                    lineStyle:{
                        color: '#2f4554',
                        type:'dotted'
                    }
                }
            }, 
            data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3],
        }
    ]
};

// 增加了一个隐藏的x轴,用来控制线图的点的位置
option.xAxis[1] = {
    type: 'value',
    max: option.xAxis[0].data.length * 100,
    show: false
}
option.series[2].data = option.series[2].data.map((x, i) => [27 + i * 100, x])
option.series[3].data = option.series[3].data.map((x, i) => [73 + i * 100, x])

效果如图:
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题