在初级和次级 Y 轴上设置公共零

Setting a common zero on primary and secondary Y axes

如何让辅助 Y 轴与主 Y 轴共享一个零点?我不关心其余的间隔排列或任何类似的东西,我只希望主要和次要 Y 轴共享相同的零。

我默认按周汇总数据,但我的图表也可以更改为按天或按月汇总。例如,当我按周汇总时,所有值在两个轴上都是正的,因此图表底部位于 0(参见 http://imgur.com/aIe2kaW), but when I aggregate daily, I get some negative values on the primary axis. At that point, I still have all positive values on the secondary axis, so the bottom of the graph jumps down to -2000 on the primary axis and stays at 0 on the secondary (see http://imgur.com/Q1E8bLw)。

我试过在轴上设置 Axis.Crossing 属性,我试过将最小值设置为相等,我试过许多其他方法,但似乎没有任何效果。有什么方法可以使用共同的零,使次轴上的低正值不像第二张图片中那样看起来像负值吗?

编辑:我在 Visual Studio 2010

中使用 WinForms

您可以通过设置 Axis.MinimumMaximum 值强制 Axes 共享它们的 MinimumMaximum 值,例如像这样:

 ChartArea CA = chart1.ChartAreas[0];
 CA.AxisY2.Maximum = CA.AxisY.Maximum;
 CA.AxisY2.Minimum = CA.AxisY.Minimum;

如果您不知道这些值是如何分布的,您可能需要计算常见的最小值和最大值:

 ChartArea CA = chart1.ChartAreas[0];

 double min = Math.Min(CA.AxisY.Minimum, CA.AxisY2.Minimum);
 double max = Math.Max(CA.AxisY.Maximum, CA.AxisY2.Maximum);

 CA.AxisY.Maximum = max;
 CA.AxisY2.Maximum = max;
 CA.AxisY.Minimum = min;
 CA.AxisY2.Minimum = min; 

请注意,这将更改 Points 在一个或两个 Series 中的显示..:[=​​28=]

另请注意,无论您以何种方式更改这些值,坐标轴都必须显示有关值的真实情况;因此,如果两个系列以不同的比例显示值,您将必须找到一种方法来计算最小值和最大值,以便它们共享一个公共零。

如果例如您知道第二个 Series 的值是 x 倍,您可以将其 min/max 的值设置为 Series1..:[=​​28 的 x 倍=]

 CA.AxisY.Maximum = 15 * CA.AxisY2.Maximum;
 CA.AxisY.Minimum = 15 * CA.AxisY2.Minimum;

这是一种相当简单的方法,但它有效(使用不同的数据点值):

强制两个轴具有相同的比例,只需要决定使用哪个轴比例。 在这个例子中,我使用的是主轴比例:

 private static void ForceCommonZero(Chart chart)
    {
        chart.ChartAreas[0].RecalculateAxesScale();
        Axis y1 = chart.ChartAreas[0].AxisY;
        Axis y2 = chart.ChartAreas[0].AxisY2;
        double proportion = y1.Maximum / (-y1.Minimum);
        y2.Maximum = proportion * (-y2.Minimum);
    }

请注意,我假设两个轴都过零(最大值 > 0 && 最小值 < 0),这需要检查(然后这可以作为扩展方法实现)