const myChart = echarts.init(document.getElementById('main'));
// 数据都为整数类型const yData1 = [79, 85, 224, 218, 121, 147, 280]const yData2 = [94, 300, 345, 320, 400, 601, 821]
// 1. 找出最大值 和 最小值let max1 = Math.max.apply(null, yData1)let max2 = Math.max.apply(null, yData2)let min1 = Math.min.apply(null, yData1)let min2 = Math.min.apply(null, yData2)
console.log('max1', max1)console.log('max2', max2)console.log('min1', min1)console.log('min2', min2)
// 2. 最大值: 向上取整, 且为5的倍速let newMax1 = Math.ceil(max1 / 5) * 5 // note: 关键代码let newMax2 = Math.ceil(max2 / 5) * 5 // note: 关键代码
// 3. 最小值: 向下取整, 且为5的倍数let newMin1 = Math.floor( min1 / 5) * 5 // note: 关键代码let newMin2 = Math.floor( min2 / 5) * 5 // note: 关键代码
console.log('newMax1', newMax1)console.log('newMax2', newMax2)console.log('newMin1', newMin1)console.log('newMin2', newMin2)let part = 5 // 分5段let interval1 = (newMax1 - newMin1) / 5 // note: 这个地方是用: 最大值 - 最小值let interval2 = (newMax2 - newMin2) / 5 // note: 这个地方是用: 最大值 - 最小值console.log('interval1', interval1)console.log('interval2', interval2)
const option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: [{ type: 'value', min: newMin1, max: newMax1, interval: interval1, splitLine: { lineStyle: { color: 'red' } } }, { type: 'value', min: newMin2, max: newMax2, interval: interval2, splitLine: { lineStyle: { color: 'green' } } }], series: [{ data: yData1, type: 'line' }, { data: yData2, type: 'line', yAxisIndex: 1 }]};myChart.setOption(option);
JavaScript