放大 MS Forms 图表的行为不符合预期...或如何对图表进行位置缩放?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(min2, max2-min2);
Try this
chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoom(min2, max2-min2);