我正在尝试通过将 X 轴上的值向右拖动然后将其放到新位置来使用 .NET 图表来实现图表缩放。它有点工作,但我有一些问题。这是我的代码:
public partial class ChartForm : Form
{
public ChartForm()
{
InitializeComponent();
}
private Axis AxisX { get; set; }
private double InitialAxisValue { get; set; }
protected override void OnLoad(EventArgs e)
{
chart1.Series.Add(CreateDataSeries());
AxisX = chart1.ChartAreas[0].AxisX;
InitialAxisValue = double.NaN;
chart1.MouseMove += OnChartMouseMove;
chart1.MouseDown += OnChartMouseDown;
chart1.MouseUp += OnChartMouseUp;
}
private static Series CreateDataSeries()
{
var dataSeries = new Series
{
ChartType = SeriesChartType.Line,
};
for (double spotPrice = 1100; spotPrice < 1300; spotPrice++)
{
dataSeries.Points.Add(new DataPoint(spotPrice, spotPrice));
}
return dataSeries;
}
private void OnChartMouseUp(object sender, MouseEventArgs e)
{
InitialAxisValue = double.NaN;
}
private void OnChartMouseDown(object sender, MouseEventArgs e)
{
if (!MouseOnXAxis(e.X, e.Y))
return;
double valueX = AxisX.PixelPositionToValue(e.X);
InitialAxisValue = valueX;
}
private void OnChartMouseMove(object sender, MouseEventArgs e)
{
try
{
Cursor = MouseOnXAxis(e.X, e.Y) ? Cursors.Hand : Cursors.Default;
HandleZoom(e);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private void HandleZoom(MouseEventArgs e)
{
if (double.IsNaN(InitialAxisValue))
return;
AxisX.Maximum = GetNewAxisMaximumValue(e.X, InitialAxisValue);
}
private double GetNewAxisMaximumValue(double currentAxisCoordinate,
double initialAxisZoomPointValue)
{
double axisMinimumValue = AxisX.Minimum;
double axisMaximumValue = AxisX.Maximum;
double axisMinimumCoordinate = AxisX.ValueToPixelPosition(axisMinimumValue);
double axisMaximumCoordinate = AxisX.ValueToPixelPosition(axisMaximumValue);
double relativePos = (currentAxisCoordinate - axisMinimumCoordinate) /
(axisMaximumCoordinate - axisMinimumCoordinate);
double newAxisWidth = ((initialAxisZoomPointValue - axisMinimumValue)/relativePos);
return axisMinimumValue + newAxisWidth;
}
private bool MouseOnXAxis(int xCoor, int yCoor)
{
HitTestResult hitTestResult = chart1.HitTest(xCoor, yCoor);
return hitTestResult.ChartElementType == ChartElementType.AxisLabels &&
hitTestResult.Axis == AxisX;
}
}
实际的缩放效果是通过在 HandleZoom() 中改变 AxisX 的最大值来实现的:
AxisX.Maximum = GetNewAxisMaximumValue(e.X, InitialAxisValue);
其中“InitialAxisValue”是初始拖动值,“eX”是当前鼠标 x 轴坐标。
它有效,但我有一个问题:有时拖动移动不顺畅,有时在图表/网格中出现来回不希望的移动/闪烁(您可以通过将鼠标缓慢向右拖动来轻松查看)。
问题可能出在 HandleZoom() 和 GetNewAxisMaximumValue() 的实现中。
任何帮助都会很棒。
谢谢