通过拖动滚动条使 DataVisualization Chart 对象缩放和滚动

发布于 2024-09-02 21:00:34 字数 186 浏览 6 评论 0原文

我正在使用 System.Windows.Forms.DataVisualization.Charting.Chart 控件,并将 ChartArea 的 AxisX 和 Y 都将 Zoomable 设置为 true。当图表放大时,我看到滚动条,但无法拖动它。是否可以拖动滚动条,并在拖动滚动条按钮时让图表显示移动?我想让它对用户来说直观且具有交互性。谢谢!

I'm using a System.Windows.Forms.DataVisualization.Charting.Chart control, and have the ChartArea's AxisX and Y both set Zoomable to true. When chart is zoomed in, I see the scrollbar, but cannot drag it. Is it possible to drag the scrollbar, and have the chart display move as I am dragging the scrollbar button? I want to make it intuitive and interactive for the user. Thanks!

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

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

发布评论

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

评论(1

决绝 2024-09-09 21:00:34

为了放大和缩小图表,我使用 MouseWheel 事件。我将在下面展示的解决方案远非完美,但它有效,至少对我来说:)。

  1. 应该为每个数据系列计算 maxChangeRange 和 minChangeRange 的值(21 和 -1 是我在项目中使用的值)。此外,假设两个轴具有相似的值范围,以防万一它们不同,则需要为其中之一添加某种缩放。

  2. 从 MouseEventArgs(eX 和 eY)接收的鼠标位置是图表控件内的鼠标位置,而不是图表内的位置:),因此此位置缩放功能有点错误。

  3. 为了使其工作,您需要定义图表区域和轴的图表控件

  4. 如果轴标签自动启用拟合后,图表在缩放时可能会有点“跳跃”。

  5. 我是 C# 和 winforms 世界的初学者,所以请记住,这可能不是最好的解决方案。

    //实际总缩放值
    int deltaScrollTotal;
    私人无效图表_MouseWheel(对象发送者,MouseEventArgs e)
    {
        int 最大变化范围 = 21;
        int minChangeRange = -1;
    
        int deltaScroll = e.Delta / Math.Abs​​(e.Delta);
        deltaScrollTotal += deltaScrollTotal + deltaScroll >最小变化范围
                         && deltaScrollTotal + deltaScroll <最大变化范围
                          ?增量滚动:0;
        // 额外计算以获得伪值
        //“位置缩放”功能
        double minXScale = (double)eX / (double)chart.Width;
        双 maxXScale = 1 - minXScale;
        double minYScale = (double)eY / (double)chart.Height;
        双 maxYScale = 1 - minYScale;
    
        // 轴需要缩放/缩放的最大和最小值
        双 maxX = Chart.ChartAreas[0].AxisX.Maximum 
                    - deltaScrollTotal * maxXScale;
        双minX =图表.ChartAreas[0].AxisX.Minimum 
                    + deltaScrollTotal * minXScale;
        双 maxY = Chart.ChartAreas[0].AxisY.Maximum 
                    - deltaScrollTotal * minYScale;
        双 minY = Chart.ChartAreas[0].AxisY.Minimum 
                    + deltaScrollTotal * maxYScale;
    
        Chart.ChartAreas[0].AxisX.ScaleView.Zoom( minX, maxX);
        Chart.ChartAreas[0].AxisY.ScaleView.Zoom( minY, maxY);
    }
    

该事件需要附加到图表控件:

    chart.MouseWheel += new MouseEventHandler(chart_MouseWheel);

In order to zoom in and out of the chart i am using MouseWheel event. Solution which i will show below is far from perfect but it works, at least for me :).

  1. Values of maxChangeRange and minChangeRange should be calculated for each data series (21 and -1 are values I have used in my project). Additionally it is assumed that both axes have similar value range in case if they are different some kind of scaling needs to be added for one of them.

  2. Mouse positions received from MouseEventArgs (e.X and e.Y) are positions of mouse inside the chart control not inside The Chart :) so this positional zoom feature is kind of faulty.

  3. In order to make it work you need to have chart control with chart area and axes defined

  4. If axis labels auto fit is enabled chart may be a little "jumpy" while zooming.

  5. I am beginner of C# and winforms world so pls bear it in mind that this is probably not the best solution.

    // Actual total zoom value
    int deltaScrollTotal;
    private void chart_MouseWheel(object sender, MouseEventArgs e)
    {
        int maxChangeRange = 21;
        int minChangeRange = -1;
    
        int deltaScroll = e.Delta / Math.Abs(e.Delta);
        deltaScrollTotal += deltaScrollTotal + deltaScroll > minChangeRange
                         && deltaScrollTotal + deltaScroll < maxChangeRange
                          ? deltaScroll : 0;
        // Additional calculation in order to obtain pseudo
        // "positional zoom" feature
        double minXScale = (double)e.X / (double)chart.Width;
        double maxXScale = 1 - minXScale;
        double minYScale = (double)e.Y / (double)chart.Height;
        double maxYScale = 1 - minYScale;
    
        // Max and min values into which axis need to be scaled/zoomed
        double maxX = chart.ChartAreas[0].AxisX.Maximum 
                    - deltaScrollTotal * maxXScale;
        double minX = chart.ChartAreas[0].AxisX.Minimum 
                    + deltaScrollTotal * minXScale;
        double maxY = chart.ChartAreas[0].AxisY.Maximum 
                    - deltaScrollTotal * minYScale;
        double minY = chart.ChartAreas[0].AxisY.Minimum 
                    + deltaScrollTotal * maxYScale;
    
        chart.ChartAreas[0].AxisX.ScaleView.Zoom( minX, maxX);
        chart.ChartAreas[0].AxisY.ScaleView.Zoom( minY, maxY);
    }
    

This event needs to be attached to the chart control:

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