gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:16:41 26 4
gpt4 key购买 nike

我有一个 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!!!!!

最后一个值产生问题(红叉无一异常(exception))。因此,转换列表的最小值和最大值似乎不合适。有什么想法吗?

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

最佳答案

RecalculateAxesScale 中的错误(System.OverflowException:Value 对于 Decimal 而言太大或太小)被抛出,因为库中存在 Bug。它在图表函数的实现中使用了十进制值(尤其是在轴重新计算/缩放中),这导致了问题。

只检查每个图表点的值是低于还是高于

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

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

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

关于c# - Microsoft 图表控件 - 失败后重绘图表(红叉),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17210257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com