我正在尝试使用.NET图表来实现图表缩放,方法是将X轴上的值拖到右侧,然后将其拖放到新位置。它有点工作,但我有一些问题。这是我的代码:
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'是初始拖动值,'e.X'是当前鼠标x轴坐标。
它有效但我有一个问题:有时拖动动作不平滑,有时在图表/网格中有来回不希望的动作/闪烁(你可以通过慢慢地向右拖动鼠标来看到它)。
问题可能在于HandleZoom()和GetNewAxisMaximumValue()的实现。
任何帮助都会很棒。
由于
答案 0 :(得分:2)
<强>解决强>!
我的缩放计算基于X轴宽度(以像素为单位)是固定的假设,显然当InnerPlotPosition属性设置为TRUE时,像素中的X轴宽度可能会在运行时更改,例如在缩放时。
所以解决方案是将InnerPlotPosition属性设置为FALSE并手动设置内部绘图区域:
chart1.ChartAreas[0].InnerPlotPosition.Auto = false;
chart1.ChartAreas[0].InnerPlotPosition.Height = 80.29209F;
chart1.ChartAreas[0].InnerPlotPosition.Width = 85F;
chart1.ChartAreas[0].InnerPlotPosition.X = 15F;
chart1.ChartAreas[0].InnerPlotPosition.Y = 5.07246F;
答案 1 :(得分:2)
我可以看到你已经解决了缩放功能,但为什么不使用内置的.net图表缩放选项?
chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
答案 2 :(得分:0)
图形控件帮助中“DoubleBuffer = true”吗?