This Question has been languishing un-answered on the MSChart forum for over a year.
I'm continually getting an overflow exception on a chart. I'm setting up my chart as follows:
InstrChart.Legends.Clear(); dataArea = InstrChart.ChartAreas.Add("Instr1"); dataArea.AxisX.MajorGrid.Enabled = false; dataArea.AxisY.MajorGrid.Enabled = false; dataArea.CursorX.IsUserSelectionEnabled = true;
I'm then adding 12 series with about 10000 points each.
The exception occurs when I zoom down to show only 3 or 4 point per series. Immediately after I release the mouse button for a zoom I get the following exception:
System.OverflowException was caught Message="Overflow error." Source="System.Drawing" StackTrace: at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
(etc - see link above for full trace.)
I've removed all event handler for the chart with no luck in stopping zooming from eventuall 开发者_StackOverflowcausing this exception. I've set IsUserSelectionEnabled to false for the chart and done zooming from code with no luck.
Any help on this issue would be great. Cheers.
This exception appears to occur anytime you zoom down "too far" (the meaning of which may vary), regardless of how the rest of the chart is configured. Several people have reported this issue. The exception helper indicates that it's in System.Drawing.dll.
Anyone here have any clues or workarounds?
I encountered the same problem today when mistakenly setting the zoom with identical start and end values.
chartarea.AxisX.ScaleView.Zoom(chartarea.CursorX.SelectionStart, chartarea.CursorX.SelectionStart); // second argument should have been chartarea.CursorX.SelectionEnd
I then tried the following as an experiment:
chartarea.AxisX.ScaleView.Zoom(chart.CursorX.SelectionStart, chartarea.CursorX.SelectionStart + 0.00000001); // crash
chartarea.AxisX.ScaleView.Zoom(chart.CursorX.SelectionStart, chartarea.CursorX.SelectionStart + 0.0000001); // no crash
Is it possible that your data points are so close together that the distance between your start and end point is below the threshold observed above? I would recommend you try multiplying your time values by 100 or 1000 and see if the problem disappear.
Another way to eliminate this problem is to set the MinSize on ScaleView.
chartarea.AxisX.ScaleView.MinSize = 0.0001; // something bigger than 0.0000001 works for me
I setup a quick test app and cannot reproduce.
Here is my series init code
chart1.Legends.Clear();
Random r = new Random();
for(int i = 0; i < 12; i++)
{
Series series = new Series();
series.ChartType = SeriesChartType.FastLine;
for (int j = 0; j < 10000; j++)
{
series.Points.Add(r.NextDouble() + i + 3*Math.Sin((double)j/300.0f));
}
chart1.Series.Add(series);
}
and here is the chart init code
chartArea1.AxisX.MajorGrid.Enabled = false;
chartArea1.AxisY.MajorGrid.Enabled = false;
chartArea1.CursorX.IsUserSelectionEnabled = true;
chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(0, 0);
this.chart1.Name = "chart1";
this.chart1.Size = new System.Drawing.Size(616, 273);
this.chart1.TabIndex = 0;
this.chart1.Text = "chart1";
Is the exception data dependent? Are you providing values for X also? Do your series use values that are either very small or very large? Have you tried setting your series to a simple sin wave for example?
Also what versions of the controls and VS are you using? And what framework are you targetting?
Similar to what David T. Macknet said above in a comment:
You can add a handler to manage the zooming a little better:
AddHandler aChart.AxisViewChanged, AddressOf Chart_ViewChanged
Your function will look something like this:
Public Sub Chart_ViewChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataVisualization.Charting.ViewEventArgs)
'The Zoom event changes the view state twice, first for the XAxis then the YAxis.'
End Sub
HTH
I think the Overflow Exception occures when the MSChart computes the 'Actual Zoom' level. I was facing the same issue with custom zooming. I fixed this issue by wrapping the zooming into a try-catch block. However I did'nt tried it, Dominique Jacquel's solution seems more solid.
try
{
Double GraphSize = Math::Abs(Graph->AxisX->Minimum-Graph->AxisX->Maximum) +
Math::Abs(Graph->AxisY->Minimum-Graph->AxisY->Maximum);
Double ScaleViewSize = Math::Abs(NewMinX-NewMaxX) + Math::Abs(NewMinY-NewMaxY);
// if the difference of the two sizes are enormous, then Overflow exception occurs
ActualZoom = Convert::ToInt32((GraphSize/ScaleViewSize)*100.0);
// zoom
Graph->AxisX->ScaleView->Zoom(NewMinX, NewMaxX);
Graph->AxisY->ScaleView->Zoom(NewMinY, NewMaxY);
}
catch(OverflowException^){}
I am pretty sure that it is because of GDI+ limiation that is known as "GDI+ hard bounds of drawing coordinates". Unfortunetely we cannot do much with it because it is a problem in MSChart control implementation where some scaling occurs which causes the drawing values to go out of hard bounds for GDI+ drawing API. The workaround is to not use Zoom option or FastLine drawing.
精彩评论