缩放后MSChart未处理溢出异常

时间:2010-09-29 23:51:12

标签: .net system.drawing mschart

This Question在MSChart论坛上一直没有得到回答超过一年。

  

我不断在图表上获得溢出异常。我按如下方式设置了我的图表:

InstrChart.Legends.Clear();
dataArea = InstrChart.ChartAreas.Add("Instr1");
dataArea.AxisX.MajorGrid.Enabled = false;
dataArea.AxisY.MajorGrid.Enabled = false;
dataArea.CursorX.IsUserSelectionEnabled = true;
     

然后我添加了12个系列,每个系列大约10000点。

     

当我向下缩放以显示每个系列只有3或4个点时会发生异常。在我释放鼠标按钮进行缩放后,我立即得到以下异常:

System.OverflowException was caught   
  Message="Overflow error."   
  Source="System.Drawing"   
  StackTrace:   
     at System.Drawing.Graphics.CheckErrorStatus(Int32 status)   
     

(等 - 请参阅上面的链接以获取完整的跟踪。)

     

我已经删除了图表的所有事件处理程序而没有运气来阻止从eventuall缩放导致此异常。我已经为图表设置了IsUserSelectionEnabled为false,并且没有运气从代码中进行缩放。

     

对这个问题的任何帮助都会很棒。欢呼声。

当您向下缩放“太远”(其含义可能不同)时,无论图表的其余部分如何配置,此异常似乎都会发生。有几个人报告了这个问题。异常助手表示它在System.Drawing.dll中。

这里有人有任何线索或解决方法吗?

5 个答案:

答案 0 :(得分:1)

今天我错误地设置了相同的开始和结束值的缩放时遇到了同样的问题。

chartarea.AxisX.ScaleView.Zoom(chartarea.CursorX.SelectionStart, chartarea.CursorX.SelectionStart); // second argument should have been chartarea.CursorX.SelectionEnd

然后我尝试了以下作为实验:

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

您的数据点是否可能如此接近,以至于您的起点和终点之间的距离低于上面观察到的阈值?我建议您尝试将时间值乘以100或1000,然后查看问题是否消失。

消除此问题的另一种方法是在ScaleView上设置MinSize。

chartarea.AxisX.ScaleView.MinSize = 0.0001; // something bigger than 0.0000001 works for me

答案 1 :(得分:0)

我设置了一个快速测试应用程序但无法重现。

series unzoomed series fully zoomed

这是我的系列初始化代码

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);
  }

这是图表初始化代码

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";

异常数据是否依赖?你也为X提供价值吗?您的系列使用的值是非常小还是非常大?您是否尝试过将系列设置为简单的sin波?

您还在使用哪些版本的控件和VS?你在瞄准什么框架?

答案 2 :(得分:0)

与David T. Macknet在评论中所说的相似:

您可以添加处理程序来更好地管理缩放:

AddHandler aChart.AxisViewChanged, AddressOf Chart_ViewChanged

您的功能将如下所示:

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

答案 3 :(得分:0)

我认为当MSChart计算'实际缩放'级别时会发生溢出异常。我在自定义缩放方面遇到了同样的问题。我通过将缩放包装到try-catch块中来解决此问题。但是我没有尝试过,Dominique Jacquel的解决方案看起来更加稳固。

    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^){}

答案 4 :(得分:0)

我很确定这是因为GDI +限制被称为“GDI+ hard bounds of drawing coordinates”。遗憾的是,我们无法做很多事情,因为它是MSChart控件实现中的一个问题,其中发生一些缩放,导致绘图值超出GDI +绘图API的硬边界。解决方法是不使用缩放选项或FastLine绘图。

相关问题