You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms柱状图如何为dataGridView1和dataGridView2的数据设置不同颜色

解决方案

你当前将两个DataGridView的产出数据都写入了同一个图表序列Series["Wydajnosc"],所以默认会使用统一颜色渲染。要区分两类数据的颜色,有两种常用实现方式:

方案1:拆分独立序列(推荐)

这种方式维护成本最低,且图例会自动生成对应颜色的分类说明,可读性更好:

  1. 提前在图表设计器中新增名为Wydajnosc2的柱状图序列,也可以直接在代码中动态添加
  2. 分别为两个序列设置不同的自定义颜色
  3. 将dataGridView2的数据写入新序列即可

修改后代码示例:

// 清空原有数据
chart1.Series["Wydajnosc"].Points.Clear();
chart1.Series["Srednia"].Points.Clear();

// 动态新增第二个产出数据序列(如果已在设计器添加可删除这段判断逻辑)
if (chart1.Series.IsUniqueName("Wydajnosc2"))
{
    chart1.Series.Add("Wydajnosc2");
    chart1.Series["Wydajnosc2"].ChartType = SeriesChartType.Column;
}

// 自定义各序列颜色
chart1.Series["Wydajnosc"].Color = Color.SteelBlue; // dataGridView1数据颜色
chart1.Series["Wydajnosc2"].Color = Color.OrangeRed; // dataGridView2数据颜色
chart1.Series["Srednia"].Color = Color.LightGray; // 平均值线颜色

// 写入dataGridView1数据
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    this.chart1.Series["Wydajnosc"].Points.AddXY(
        dataGridView1.Rows[i].Cells[0].Value.ToString(), 
        Convert.ToDouble(dataGridView1.Rows[i].Cells["WYDAJNOSC"].Value.ToString())
    );
    this.chart1.Series["Srednia"].Points.AddXY(
        dataGridView1.Rows[i].Cells[0].Value.ToString(), 
        Convert.ToDouble(textBox2.Text)
    );             
}

// 写入dataGridView2数据到新序列
for (int j = 0; j < dataGridView2.Rows.Count - 1; j++)
{
    this.chart1.Series["Wydajnosc2"].Points.AddXY(
        dataGridView2.Rows[j].Cells[0].Value.ToString(), 
        Convert.ToDouble(dataGridView2.Rows[j].Cells["WYDAJNOSCII"].Value.ToString())
    );
    this.chart1.Series["Srednia"].Points.AddXY(
        dataGridView2.Rows[j].Cells[0].Value.ToString(), 
        Convert.ToDouble(textBox6.Text)
    );
}

方案2:单序列内逐个设置数据点颜色

如果业务要求必须把两类数据放在同一个序列中,可以在添加数据点时直接为单个点指定颜色:

chart1.Series["Wydajnosc"].Points.Clear();
chart1.Series["Srednia"].Points.Clear();

// 写入dataGridView1数据,单独设置点颜色
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    var dataPoint = this.chart1.Series["Wydajnosc"].Points.AddXY(
        dataGridView1.Rows[i].Cells[0].Value.ToString(), 
        Convert.ToDouble(dataGridView1.Rows[i].Cells["WYDAJNOSC"].Value.ToString())
    );
    dataPoint.Color = Color.SteelBlue;
    
    this.chart1.Series["Srednia"].Points.AddXY(
        dataGridView1.Rows[i].Cells[0].Value.ToString(), 
        Convert.ToDouble(textBox2.Text)
    );             
}

// 写入dataGridView2数据,设置不同的点颜色
for (int j = 0; j < dataGridView2.Rows.Count - 1; j++)
{
    var dataPoint = this.chart1.Series["Wydajnosc"].Points.AddXY(
        dataGridView2.Rows[j].Cells[0].Value.ToString(), 
        Convert.ToDouble(dataGridView2.Rows[j].Cells["WYDAJNOSCII"].Value.ToString())
    );
    dataPoint.Color = Color.OrangeRed;
    
    this.chart1.Series["Srednia"].Points.AddXY(
        dataGridView2.Rows[j].Cells[0].Value.ToString(), 
        Convert.ToDouble(textBox6.Text)
    );
}

如果需要对平均值序列Srednia的两类数据也做颜色区分,套用上述两种逻辑即可。


内容的提问来源于stack exchange,提问作者Mlody_pb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 22:48:01