除了redtuna在 ac# 图表中设置光标:
我使用“SelectionRangeChanging ”而不是“SelectionRangeChang ed ”来解决 NaN 问题对我有用:
初始化表单时
this.chart1.SelectionRangeChanging += chart1_SelectionRangeChanging;
和
chart1.ChartAreas[0].CursorX.IsUserEnabled = false; // red cursor at SelectionEnd
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false; // zoom into SelectedRange
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].CursorX.Interval = 0.01; // set "resolution" of CursorX
如果选择了范围/设置了光标,则执行什么
private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
{
double x1 = x1 = e.NewSelectionStart; // or: chart1.ChartAreas[0].CursorX.SelectionStart;
double x2 = e.NewSelectionEnd; // or: x2 = chart1.ChartAreas[0].CursorX.SelectionEnd;
double diffx1x2 = x2 - x1;
}
为了放大和缩小(x 轴),我刚刚添加了一个获取光标值的按钮。这样通过 mouseClick (ScaleView.Zoomable = false;) 进行缩放不会干扰我的光标定位:)
private void button_ZoomIn(object sender, EventArgs e)
{
double x1 = chart1.ChartAreas[0].CursorX.SelectionStart; // x1 = X1
double x2 = chart1.ChartAreas[0].CursorX.SelectionEnd; // x2 = X2
if (x2 > x1)
{
// hard setting: chart1.ChartAreas[0].AxisX.Minimum = x1;
// hard setting: chart1.ChartAreas[0].AxisX.Maximum = x2;
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(x1,x2); // dynamic approach with scrollbar
}
else
{
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(x2,x1);
}
}
缩小
private void button_ZoomOut(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(0);
}
缩放也可以通过 mouseWheel 实现:如何使用鼠标滚轮在 Microsoft 图表控件中启用缩放
如果您还想在图表中进行右键单击操作:如何获得右键单击鼠标事件?将 EventArgs 更改为 MouseEventArgs 会导致 Form1Designer 出现错误?