X轴上的自动缩放Y轴缩放

时间:2012-04-17 16:53:06

标签: c# mschart

是的我知道(This post),如果你搜索,你会发现事件AxisValueChanged似乎只存在于这一个帖子中。

我的目标只是在用户在X轴上进行选择时自动缩放Y轴,但我无法弄清楚如何。

我也试图使用SelectionRangeChanged事件,但这个事件似乎有点破,因为我无法弄清楚什么是实际范围选择? (IE所以我可以找到最大/最小范围)。

我正在使用MS图表(Microsoft图表)

最终目标是当我缩放X轴(这是一个FastLine图表)时,它应该在Y轴上看到新的“max”可见值并相应地调整大小/缩放

1 个答案:

答案 0 :(得分:5)

如果我理解正确的话,给定X轴上的缩放范围,您想要在Y轴上也缩放相同的范围。如果是这样,你可以这样做:

void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
    // Given the visible (zoomed) range on X, 
    // it zooms the same relative range on Y:
    // i.e. if on the X axis, the range 5% - 30% is zoomed, 
    // it zooms the same range on Y.
    var axisY = this.chart1.ChartAreas[0].AxisY;

    var totalXRange = e.Axis.Maximum - e.Axis.Minimum;
    var totalYRange = axisY.Maximum - axisY.Minimum;

    var ySelectionStart = (e.Axis.ScaleView.ViewMinimum - e.Axis.Minimum) *
                          totalYRange / totalXRange;
    var ySelectionEnd = (e.Axis.ScaleView.ViewMaximum - e.Axis.Minimum) * 
                         totalYRange / totalXRange;

    axisY.ScaleView.Zoom(ySelectionStart,ySelectionEnd);
}

正如你所说,事件AxisValueChanged不存在;您链接的帖子可能意味着(现有)事件AxisViewChanged

显然,您也可以将AxisViewChanged用于您的目的,并且调整我的代码以利用该事件不应该那么难。

随时问你是否需要帮助;)

编辑:

我修改了我的代码以说明您的目标。以下代码计算与缩放X范围内的点的最大值和最小值对应的Y范围:

void chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
    var axisY = this.chart1.ChartAreas[0].AxisY;

    var xRangeStart = e.Axis.ScaleView.ViewMinimum;
    var xRangeEnd = e.Axis.ScaleView.ViewMaximum;

    // compute the Y values for the points crossing the range edges
    double? yRangeStart = null;
    var pointBeforeRangeStart = this.chart1.Series[0].Points.FirstOrDefault(x => x.XValue <= xRangeStart);
    var pointAfterRangeStart = this.chart1.Series[0].Points.FirstOrDefault(x => x.XValue > xRangeStart);
    if (pointBeforeRangeStart != null && pointAfterRangeStart != null)
        yRangeStart = Interpolate2Points(pointBeforeRangeStart, pointAfterRangeStart, xRangeStart);

    double? yRangeEnd = null;
    var pointBeforeRangeEnd = this.chart1.Series[0].Points.FirstOrDefault(x => x.XValue <= xRangeEnd);
    var pointAfterRangeEnd = this.chart1.Series[0].Points.FirstOrDefault(x => x.XValue > xRangeEnd);
    if (pointBeforeRangeEnd != null && pointAfterRangeEnd != null)
        yRangeEnd = Interpolate2Points(pointBeforeRangeEnd, pointAfterRangeEnd, xRangeEnd);

    var edgeValues = new[] { yRangeStart, yRangeEnd }.Where(x => x.HasValue).Select(x => x.Value);

    // find the points inside the range
    var valuesInRange = this.chart1.Series[0].Points
    .Where(p => p.XValue >= xRangeStart && p.XValue <= xRangeEnd)
    .Select(x => x.YValues[0]);

    // find the minimum and maximum Y values
    var values = valuesInRange.Concat(edgeValues);
    double yMin;
    double yMax;
    if (values.Any())
    {
        yMin = values.Min();
        yMax = values.Max();
    }
    else
    {
        yMin = this.chart1.Series[0].Points.Min(x => x.YValues[0]);
        yMax = this.chart1.Series[0].Points.Max(x => x.YValues[0]);
    }

    // zoom Y-axis to [yMin - yMax]
    axisY.ScaleView.Zoom(yMin, yMax);
}

// see: http://en.wikipedia.org/wiki/Linear_interpolation#Linear_interpolation_between_two_known_points
public static double Interpolate2Points(DataPoint p1, DataPoint p2, double x)
{
    var x0 = p1.XValue;
    var y0 = p1.YValues[0];
    var x1 = p2.XValue;
    var y1 = p2.YValues[0];
    return y0 + ((x - x0) * y1 - (x - x0) * y0) / (x1 - x0);
}