echarts 双y轴0刻度线对称

发布时间 2023-04-07 12:27:59作者: IT小兔

第一步:分别找出双y轴的最大最小值

const max1 = Math.max(...data1);
const min1 = Math.min(...data1) ;
const max2 = Math.max(...data2) ;
const min2 = Math.min(...data2) ;

第二步:计算两组数据范围的比值(相当于比例尺)

const ratio = (max1 - min1) / (max2 - min2);

第三步:两组数据对阶,然后确定应当使用哪一组的数据作为最大值或最小值

if (max1 < max2 * ratio) {
y1Max = max2 * ratio;
y2Max = max2;
} else {
y1Max = max1;
y2Max = max1 / ratio;
}
if (min1 < min2 * ratio) {
y1Min = min1;
y2Min = min1 / ratio;
} else {
y1Min = min2 * ratio;
y2Min = min2;
}

第四步:给y轴分别设置最大最小值(乘以1.2是为了最大最小值不在顶格的位置)

yAxis: [
{
...
max: (y1Max * 1.2).toFixed(0),
min: (y1Min * 1.2).toFixed(0),
...
},
{
...
max: (y2Max * 1.2).toFixed(0),
min: (y2Min * 1.2).toFixed(0),
...
}
],