Microsoft图表控件 - 失败后重绘图表(红叉)

时间:2013-06-20 09:36:18

标签: c# charts repaint redraw overflowexception

我有一个treeView element,其中每个节点代表一个双重列表。

我使用DataVisualization.Charting控件来显示list中的值。

对于某些列表,我在RecalculateAxesScale (System.OverflowException: Value was either too large or too small for a Decimal).之后收到例外 我忽略了这个错误,因此图表显示了一个大红叉。

当我现在点击另一个节点时,我想显示这个双重列表的图表(有效),但我的图表没有重新绘制。它始终显示红色X.

我的代码:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{

//Refresh chart:
chart1.Series.Clear();
chart1.ResetAutoValues();
chart1.ResetText();

//plot new doublelist
var series = new Series
{
   Name = "name",
   Color = color,
   ChartType = SeriesChartType.Line,
   ChartArea = "chartName"
};

this.chart1.Series.Add(series);
    series.Points.DataBindY(doubleList);
    var chartArea = chart1.ChartAreas["chartName"];
    chartArea.RecalculateAxesScale();
    chartArea.AxisX.Minimum = 1;
    chartArea.AxisX.Maximum = doubleList.Count;
    chartArea.CursorX.AutoScroll = true;
    chartArea.CursorY.AutoScroll = true;

    // Allow user to select area for zooming
    chartArea.CursorX.IsUserEnabled = true;
    chartArea.CursorX.IsUserSelectionEnabled = true;

    // Set automatic zooming`<br>
    chartArea.AxisX.ScaleView.Zoomable = true;
    chartArea.AxisY.ScaleView.Zoomable = true;
    chartArea.AxisX.ScrollBar.IsPositionedInside = true;

    //reset zoom
    chartArea.AxisX.ScaleView.ZoomReset();
    chart1.Invalidate();
}

[编辑]

dblList的类型:

List<double> doubleList= (from s in myData select s.value).ToList(); 

完整的例外情况:

{System.OverflowException: Value was either too large or too small for a Decimal.
at System.Decimal.FCallMultiply(Decimal& d1, Decimal& d2)
at System.Decimal.op_Multiply(Decimal d1, Decimal d2)
at System.Windows.Forms.DataVisualization.Charting.Axis.RoundedValues(Double inter, Boolean shouldStartFromZero, Boolean autoMax, Boolean autoMin, Double& min, Double& max)
at System.Windows.Forms.DataVisualization.Charting.Axis.EstimateNumberAxis(Double& minimumValue, Double& maximumValue, Boolean shouldStartFromZero, Int32 preferredNumberOfIntervals, Boolean autoMaximum, Boolean autoMinimum)
at System.Windows.Forms.DataVisualization.Charting.Axis.EstimateAxis(Double& minimumValue, Double& maximumValue, Boolean autoMaximum, Boolean autoMinimum)
at System.Windows.Forms.DataVisualization.Charting.Axis.EstimateAxis()
at System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultAxesValues()
at System.Windows.Forms.DataVisualization.Charting.ChartArea.SetData(Boolean initializeAxes, Boolean checkIndexedAligned)

at System.Windows.Forms.DataVisualization.Charting.ChartArea.RecalculateAxesScale()

[编辑2]

示例列表:

   List<double> dblList = new List<double>();
   dblList.Add(0.0);
   dblList.Add(-7.4876421623346545E-36);
   dblList.Add(1.0);
   dblList.Add(-26697097281536.0);
   dblList.Add(-6.8163553952838136E+28); //problem!!!!!

最后一个值产生问题(红十字无例外)。因此,似乎转换列表的最小值和最大值是不合适的。有什么想法?

 double min = (double)Decimal.MinValue; //min = -7.9228162514264338E+28
 double max = (double)Decimal.MaxValue; //max =  7.9228162514264338E+28

2 个答案:

答案 0 :(得分:2)

由于库中存在Bug,因此抛出了RecalculateAxesScale中的错误(System.OverflowException:值太大或太小而不能使用Decimal)。它在图表函数的实现中使用十进制值(特别是在轴重新计算/缩放中),这会导致问题。

只检查每个图表点的值是否低于或大于

double min = (double)Decimal.MinValue;
double max = (double)Decimal.MaxValue;

并用这个值替换它并没有解决问题。

我选择-/+7.92E+27作为界限而不是上面的那些,现在一切正常。

答案 1 :(得分:1)

因此到目前为止提供的解决方案几乎是,但并不完全正确。如果它是开源的,我可以直接找到解决问题的方法,但是我只能猜测实际的实现。图表库似乎转换为Decimal,并在从double转换时超出值范围时抛出异常。检查所有超出范围值并设置为Decimal.MinValue和Decimal.MaxValue的明显解决方案失败,但具有相同的异常。事实上,图表引擎允许的实际最大值和最小值是由我自己通过一些实验发现的,大约要少一个数量级。

足够的讨论,这是我的TVQ图表的工作源代码,有时超出双倍值。最大和最小图表值是为效率预先计算的。用SafeChartDouble替换所有对AddXY的调用。根据应用需要更改AxisX数据类型。

    private static readonly double SCALE_FACTOR = 10;
    private static readonly double MIN_CHART_VALUE
        = Convert.ToDouble(Decimal.MinValue) / SCALE_FACTOR;
    private static readonly double MAX_CHART_VALUE
        = Convert.ToDouble(Decimal.MaxValue) / SCALE_FACTOR;

    private void SafeChartDouble(Series cs, DateTime ts, double dv)
    {
        // microsoft chart component breaks on very large/small values
        double chartv;
        if (dv < MIN_CHART_VALUE)
        {
            chartv = MIN_CHART_VALUE;
        }
        else if (dv > MAX_CHART_VALUE)
        {
            chartv = MAX_CHART_VALUE;
        }
        else
        {
            chartv = dv;
        }
        cs.Points.AddXY(ts, chartv);
    }