【问题标题】:Change bar color of chart if the amount doesn't meet the condition如果金额不符合条件,则更改图表的条形颜色
【发布时间】:2016-07-22 12:36:40
【问题描述】:

如果总金额低于5000,如何更改chartbar的颜色?

这是我的代码..

        cmd = new MySqlCommand("SELECT DATE_FORMAT(date, '%m-%d-%y') date, SUM(totalamount) totalamt FROM tblsales WHERE month(date) = month(now()) GROUP BY date");
        cmd.Connection = dbConn;

        MySqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            this.chart1.Series["Month"].Points.AddXY(rdr["date"].ToString(), rdr.GetDouble("totalamt"));
        }

        rdr.Close();

【问题讨论】:

  • 你没有说你使用的是哪个图表库;答案将取决于它的功能。

标签: c# charts bar-chart


【解决方案1】:

您可以通过DataPointColor属性设置Bar的颜色:

chart1.Series[0].Points[i].Color = Color.Red;

但是Color 属性没有表达式,因此您必须在设置或更改数据时设置它..

您可以在添加 DataPoints 时执行此操作:

while (rdr.Read())
{
    int index = this.chart1.Series["Month"].Points.AddXY(rdr["date"].ToString(),
                                                         rdr.GetDouble("totalamt"));
    if (rdr.GetDouble("totalamt") < 5000) 
        this.chart1.Series["Month"].Points[index].Color = Color.Red;
}

或者你可以写一个函数来更新颜色:

void SetColors(Series s, Color color, double limit)
{
    foreach (DataPoint dp in s.Points)
        if (dp.YValues[0] < limit) dp.Color = color;
}

【讨论】:

    【解决方案2】:

    似乎您正在使用 ASP.NET 图表。同样,您可以执行类似

    的操作
    chart1.Series[0].Color = Color.Green;
    

    根据 If Else 条件中的值选择适合您的颜色。

    【讨论】:

      猜你喜欢
      • 2020-10-27
      • 2011-08-03
      • 2012-01-19
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多