请DataVisualization图表对象缩放和滚动通过拖动滚动条 [英] Make DataVisualization Chart object zoom and scrollable by dragging the scroll bar

查看:206
本文介绍了请DataVisualization图表对象缩放和滚动通过拖动滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用System.Windows.Forms.DataVisualization.Charting.Chart控制,并有ChartArea的AxisX和Y两个可缩放设置为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!

推荐答案

在为放大和缩小我使用鼠标滚轮事件的图表。解决方案,我将在下面展示的是远非完美,但它工作,至少对我来说:)

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. maxChangeRange和minChangeRange的值应计算为每个数据系列(21和-1是我已经用我的项目价值)。此外,假设两个轴具有在壳体相似值范围,如果它们是不同的需要某种比例的其中之一被添加

  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.

鼠标从MouseEventArgs(EX和EY)收到的是鼠标的图表控件没有内部的结构图内的位置位置:)所以这个位置的变焦功能是一种错误的。

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.

为了使它工作,你需要有图表​​控制定义的图表区和轴

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

如果轴标签自动调整启用图表可能会有点神经质,而缩放。

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

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

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);

这篇关于请DataVisualization图表对象缩放和滚动通过拖动滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆