【问题标题】:How to plot different colour for negative datapoint in line chart c#如何在折线图中为负数据点绘制不同的颜色c#
【发布时间】:2017-02-22 12:46:47
【问题描述】:

我想通过 datagridview 创建折线图,我需要红色绘图线用于负值,绿色用于正值。我编写了代码,但所有数据点都只有绿色。

foreach (DataGridViewRow row in dgvReport.Rows)
{ 
    decimal val = 0;
    val = Convert.ToDecimal(row.Cells[8].Value.ToString());

    if (val < 0)
    {
        Dchart.Series[0].Color = System.Drawing.Color.Red;
    }

    if (val > 0)
    {
        Dchart.Series[0].Color = System.Drawing.Color.Green;
    }

    Dchart.Series[0].Points.AddXY(row.Cells[0].Value.ToString(), row.Cells[8].Value.ToString());
    Dchart.ChartAreas[0].AxisX.Interval = 3;
}

【问题讨论】:

  • 什么是Dchart?你确定你数据
  • 是数据

标签: c# winforms datapoint


【解决方案1】:

您需要单独为每个DataPoint 着色:

int index = Dchart.Series[0].Points.AddXY(row.Cells[0].Value,
                                          row.Cells[8].Value);
DataPoint newPoint = Dchart.Series[0].Points[index];
newPoint.Color = newPoint.YValues[0] < 0 ? Color.Red : Color.Green;

注意颜色只进入一行!

另请注意,您的转换狂欢并不是真正需要的..

最后说明:您将所有值添加为strings。这是一个严重的错误!这样做会丢失所有 x 值,并导致 y 值的默认转换不受控制。

总是所有值添加为数字或DateTimes!!

如果您发现需要将单元格值 objects 转换为数字,请执行此操作并将 DataPoint 作为一个整体创建,最好在添加之前包含颜色 series.Add()

【讨论】:

  • 得到错误无法将 bool 隐式转换为 Drawing.color
  • 你的意思可能是newPoint.Color = newPoint.YValues[0] &lt; 0 ? Color.Red : Color.Green;
猜你喜欢
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
相关资源
最近更新 更多