MSChart - 在 X 轴缩放上自动缩放 Y 轴

发布于 2024-08-17 09:38:34 字数 96 浏览 5 评论 0原文

我正在使用 MSChart,我想在 X 轴上启用缩放,一旦缩放,我希望 Y 轴自动缩放到适合数据可见的范围。

任何有关问题的帮助将不胜感激!

谢谢

I'm using MSChart and I want to enable zoom on the X Axis and once this is zoomed I want the Y Axis to auto zoom into a range appropriate for the data viewable.

Any assistance with the problem would be greatly appreciated!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-08-24 09:38:34

我花了很长时间寻找解决方案。我发现这很有用。订阅 AxisValueChanged 事件并使用 ScaleView 修改视图。

private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
    {
        if (e.Axis.AxisName == AxisName.X)
        {
            int start = (int)e.Axis.ScaleView.ViewMinimum;
            int end = (int)e.Axis.ScaleView.ViewMaximum;

            double[] temp = chart1.Series[0].Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[0]).ToArray();
            double ymin = temp.Min();
            double ymax = temp.Max();

            chart1.ChartAreas[0].AxisY.ScaleView.Position = ymin;
            chart1.ChartAreas[0].AxisY.ScaleView.Size = ymax - ymin;
        }
    }

I was searching for a solution for a long time. I found this useful. Subscribe to AxisValueChanged event and use ScaleView to modify the view.

private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
    {
        if (e.Axis.AxisName == AxisName.X)
        {
            int start = (int)e.Axis.ScaleView.ViewMinimum;
            int end = (int)e.Axis.ScaleView.ViewMaximum;

            double[] temp = chart1.Series[0].Points.Where((x, i) => i >= start && i <= end).Select(x => x.YValues[0]).ToArray();
            double ymin = temp.Min();
            double ymax = temp.Max();

            chart1.ChartAreas[0].AxisY.ScaleView.Position = ymin;
            chart1.ChartAreas[0].AxisY.ScaleView.Size = ymax - ymin;
        }
    }
倾城°AllureLove 2024-08-24 09:38:34

MSChart 无法自动完成您想要执行的缩放操作。从用户那里检索到“放大”X 值范围后,您需要编写更多代码来适当重置 Y 轴缩放。

如果您使用的是线型数据系列并且该系列的源数据存储为 SortedList,则此方法最容易实现。

Dim firstXindex as Int32 = myDataSeries.IndexOfKey(firstXzoomValue)
Dim lastXindex as Int32 = myDataSeries.IndexOfKey(lastXzoomValue)    

Dim minY as Double = 1.7E+308
Dim maxY as Double = -1.7E+308  


For i = firstXindex To lastXindex
    If myDataSeries.GetByIndex(i) > maxY Then
        maxY = myDataSeries.GetByIndex(i)
    End If
    If myDataSeries.GetByIndex(i) < minY Then
        minY = myDataSeries.GetByIndex(i)
    End If
Next

一旦您使用类似上面的代码来获取 minY 和 maxY,您就可以使用这些值来重置 ChartArea 上的最小和最大 Y 轴值:

With myChartArea
  .AxisY.Maximum = maxY
  .AxisY.Minimum = minY
End With

The kind of zooming that you want to do cannot be automatically accomplished by MSChart. Once you have retrieved the 'Zoom-In' X-value range from the user, you need to write a little more code to reset the Y-axis scaling appropriately.

This works most easily if you are using a Line style of data series and your source data for that series is stored as a SortedList.

Dim firstXindex as Int32 = myDataSeries.IndexOfKey(firstXzoomValue)
Dim lastXindex as Int32 = myDataSeries.IndexOfKey(lastXzoomValue)    

Dim minY as Double = 1.7E+308
Dim maxY as Double = -1.7E+308  


For i = firstXindex To lastXindex
    If myDataSeries.GetByIndex(i) > maxY Then
        maxY = myDataSeries.GetByIndex(i)
    End If
    If myDataSeries.GetByIndex(i) < minY Then
        minY = myDataSeries.GetByIndex(i)
    End If
Next

Once you have used something like the code above to get your minY and maxY, you can then use those values to reset the min and max Y-axis values on the ChartArea:

With myChartArea
  .AxisY.Maximum = maxY
  .AxisY.Minimum = minY
End With
浅忆 2024-08-24 09:38:34

Microsoft 已提供全系列示例供下载。在示例应用程序中,有一个名为 Scrollable Appearance 的应用程序,它似乎可以满足您的需求。

可滚动外观 http://img502.imageshack.us/img502/5172/zoomablechart.png< /a>

用户可以选择图表的区域,图表将会放大。他们还可以使用滚动条四处移动。

下载中包含 C# 示例代码。

Microsoft have made available a whole range of samples for download. In the sample application there is one called Scrollable Appearance that seems to do what you want.

Scrollable Appearance http://img502.imageshack.us/img502/5172/zoomablechart.png

The user can select and area of the graph and it will zoom in. They can also move around using the scroll bars.

C# Sample code is included with the download.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文