放大 MS Forms 图表的行为不符合预期...或如何对图表进行位置缩放?

发布于 2024-12-04 08:02:15 字数 1288 浏览 0 评论 0原文

我有一个 DataVisualization.Charting.Chart,我想用鼠标滚轮缩放 ChartArea。到目前为止还不算太难。现在我想做一个位置缩放,即鼠标光标下的点是缩放的中心。好的,我做了一些简单的数学计算,并计算出如何计算轴的新左/右端。如果我将

chart1.ChartAreas["ChartArea1"].AxisX.Minimum

Maximum 设置为新值,它就像一个魅力。但是,如果我缩放到这些值,它就无法正常工作。这是我的鼠标滚轮监听器:

void Chart1MouseWheel(object sender, MouseEventArgs e)
    {
        var min = chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.ViewMinimum;
        var max = chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.ViewMaximum;
        var oldScale = max - min;

        var newScale = oldScale + (oldScale  * 0.001 * e.Delta);

        // calculate positional zoom
        var xAbs = chart1.ChartAreas["ChartArea1"].AxisX.PixelPositionToValue(e.X);
        var xRel = (xAbs - min)/(max - min);
        var min2 = min + oldScale*xRel - newScale*xRel;

        var max2 = min2 + newScale;

        // if i do that, it works perfectly
        //chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min2;
        //chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max2;

        // this does NOT work (at least not exactly)
        chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(min2, max2);
    }

我做错了什么?如何正确使用Zoom

我想使用缩放的原因是这样可以启用图表底部的滚动条。

I have a DataVisualization.Charting.Chart and i want to zoom a ChartArea with the mouse wheel. So far not too difficult. Now i want to do a positional zoom, i.e. the point under the mouse cursor is the center of the zoom. OK, i did some simple math and figured how calculate the new left/right end of my axis. If i set

chart1.ChartAreas["ChartArea1"].AxisX.Minimum

and Maximum to the new values, it works like a charm. If i, however, zoom to the values it does not work correctly. Here is my mouse wheel listener:

void Chart1MouseWheel(object sender, MouseEventArgs e)
    {
        var min = chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.ViewMinimum;
        var max = chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.ViewMaximum;
        var oldScale = max - min;

        var newScale = oldScale + (oldScale  * 0.001 * e.Delta);

        // calculate positional zoom
        var xAbs = chart1.ChartAreas["ChartArea1"].AxisX.PixelPositionToValue(e.X);
        var xRel = (xAbs - min)/(max - min);
        var min2 = min + oldScale*xRel - newScale*xRel;

        var max2 = min2 + newScale;

        // if i do that, it works perfectly
        //chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min2;
        //chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max2;

        // this does NOT work (at least not exactly)
        chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(min2, max2);
    }

What did i do wrong? How to use Zoom correctly?

The reason that i want to use the zoom is that this enables the scrolbar at the bottom of the chart.

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

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

发布评论

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

评论(1

转身以后 2024-12-11 08:02:15

试试这个

chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(min2, max2-min2);

Try this

chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(min2, max2-min2);

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