1

我希望我的第二个 y 轴零位置与我的第一个 y 轴相同。

是否有内置方法可以做到这一点,或者我必须使用最小/最大轴?

我有动态数据(当用户选择事物时会刷新大量图表)以正/负值显示,因此 y 轴上总是有一个零刻度。

两个 y 轴的缩放比例和值不同,可以,但零位置必须位于完全相同的像素位置(在一条水平线上)。如何做到这一点?

我知道'alignTicksWithAxis',但正如你所见,这不是我想要的。

我写了一个小 js 脚本来在图表显示后修剪零位,如果我将轴最小/最大值设置为滑动轴,这很好用(零同步),但是这样一些点会从画布中消失。

所以我必须只设置一个比率的最小值或最大值,但两个零位置之间总是有 10-20 像素的差异。

编辑1:

PlotOptions po = simplePlot.getPlotOptions();
Axis axis1 = simplePlot.getAxes().getY(1), axis2 = simplePlot.getAxes().getY(2);
AbstractAxisOptions ao1 = po.getYAxesOptions().getAxisOptions(1);
AbstractAxisOptions ao2 = po.getYAxesOptions().getAxisOptions(2);
double yMin1 = axis1.getMinimumValue(), yMax1 = axis1.getMaximumValue(), yDiff1 = yMax1-yMin1;
double yMin2 = axis2.getMinimumValue(), yMax2 = axis2.getMaximumValue(), yDiff2 = yMax2-yMin2;
double diffPx = (axis2.p2c(0) - axis1.p2c(0))/2.0; // diff between the two zero points in px, divided by 2, axis1 will be compressed from the top, axis2 from bottom
if (diffPx<0) { // in this case change axis1 vars with axis2 vars
    diffPx=-diffPx;
    AbstractAxisOptions tAO=ao1; ao1=ao2; ao2=tAO;
    Axis tAX = axis1; axis1=axis2; axis2=tAX;
}
double diff1 = axis1.c2p(axis1.p2c(0)-diffPx); // axis1 zero pos must moved by this value
double diff2 = axis2.c2p(axis2.p2c(0)-diffPx); // axis2 zero pos must moved by this value

// calculate ratio values to compress the axes
double bottomRat1 = -yMin1 / yDiff1; // bottom part of axis1 (below zero) e.g. 1/3
double bottomRatNew1 = (-yMin1-diff1) / yDiff1; // same, but when compressed (with diff1)
double fullRat1 = bottomRat1 / bottomRatNew1; // ratio of original and compressed axis
double yDiffNew1 = yDiff1 / fullRat1; // the new axis1 length
double diffReal1 = yDiff1 - yDiffNew1; // the difference between the original and the new axis length (this is the value by which the axis must be compressed)

// same calculation for axis2 (but for the top part)
double topRat2 = yMax2 / yDiff2;
double topRatNew2 = (yMax2-diff2) / yDiff2;
double fullRat2 = topRat2 / topRatNew2;
double yDiffNew2 = yDiff2 / fullRat2;
double diffReal2 = yDiff2 - yDiffNew2;

boolean slide = false; // if true then axes slide (not compress) and zero positions will be in one line (this is the goal), but some points get out of the canvas
if (slide) {
    ao1.setMaximum(yMax1 + diff1);ao1.setMinimum(yMin1 + diff1);
    ao2.setMaximum(yMax2 - diff2);ao2.setMinimum(yMin2 - diff2);
} else {
// axes will be compressed (axis1 from the top, axis2 from the bottom), by the calculated values
// here I have some 10-30 px extra gap, why?
    ao1.setMaximum(yMax1 + diffReal1);ao1.setMinimum(yMin1);
    ao2.setMaximum(yMax2);ao2.setMinimum(yMin2 - diffReal2);
}
simplePolt.redraw();
4

0 回答 0