When I create a new chart of type System.Windows.Forms.DataVisualization.Charting.Chart
, the axes scale is set "Auto" by default, which means it will expands automatically.
Under normal circumstances, it behaves as it should. But if I invoke removeItem
like
chart1.Series[0].Points.AddXY(Control.MousePosition.X, -Control.MousePosition.Y);
if (chart1.Series[0].Points.Count > 200)
{
chart1.Series[0].Points.RemoveAt(0);
}
I remove the oldest point in order to ensure that the line will not be too long.
Then, from the first time removing a point, the axes of the chart never expand even if the points exceed the chartArea.
So I want to know is it a bug of System.Windows.Forms.DataVisualization.Charting.Chart
? Or is my operation incorrect?
PS: You can write a simple demo to illustrate the behavior. Using the several lines of code I write above. It draw spline according to your mouse position. Draw more than 200 points, the spline will fade away. And then move your mouse out of the border, you will found the axes didn't expand any more.
I have test it in .NET Framework 3.5 and 4.5.2 .The results are the same.
No, there is no bug in the Chart control; the error is in your logic. You expect the axis to grow but it never needs to grow beyond the maximum of your data. And since your data are limited by the pixel coordinates once you hit the limit the axes will stay where they are.
You also fail to convert the pixel coordinates to the value coordinates of your axes.
Here is an example of how to let you draw the spline with the mouse:
private void chart2_MouseMove(object sender, MouseEventArgs e)
{
Axis ax = chart1.ChartAreas[0].AxisX;
Axis ay = chart1.ChartAreas[0].AxisY;
if (e.Button.HasFlag(MouseButtons.Left)) // only draw when the button is pressed
{
// convert pixels to values!
chart1.Series[0].Points.AddXY(ax.PixelPositionToValue(e.X),
ay.PixelPositionToValue(e.Y));
if (chart1.Series[0].Points.Count > 200)
{
chart1.Series[0].Points.RemoveAt(0);
}
}
Update
Looks like I didn't quite understand your problem at first, esp. as I didn't see the Timer.Tick in your code. You ought to have posted it..
Here is the one tiny line you need to insert:
private void timer1_Tick(object sender, EventArgs e)
{
Point cp = Control.MousePosition;
chart1.Series[0].Points.AddXY(cp.X, -cp.Y);
if (chart1.Series[0].Points.Count > 200)
{
chart1.Series[0].Points.RemoveAt(0);
}
chart1.ChartAreas[0].RecalculateAxesScale(); // <<-------
}