刷新图表而不重绘它

时间:2015-02-12 14:48:24

标签: c# winforms charts bar-chart

我想创建一个条形图,就像扫描一些东西一样。另外我想根据当时的x轴间隔改变条形的颜色。

到目前为止,我可以管理扫描功能但是当我尝试更改条形图时,整个条形图都在变化。

这是我的代码:

namespace UDP
{
public partial class ScanGraph : Form
{
    int boardCounter = 0;
    int trackValue;

    public ScanGraph()
    {
        InitializeComponent();
    }

    private void ScanGraph_Load(object sender, EventArgs e)
    {
        System.Windows.Forms.DataVisualization.Charting.Chart chart1 = new Chart();
        ChartArea chartArea1 = new ChartArea("foobar");
        chartArea1.Name = "ChartArea1";

        Chart1.Series.Add("Head1");
        Chart1.Series[1].ChartType = SeriesChartType.Bar;
        Chart1.Series.Add("Head2");
        Chart1.Series[2].ChartType = SeriesChartType.Bar;
        Chart1.Series.Add("Head3");
        Chart1.Series[3].ChartType = SeriesChartType.Bar;

        Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;

        Chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 1;
        Chart1.ChartAreas[0].AxisY.MinorGrid.Enabled = true;
        Chart1.ChartAreas[0].AxisX.IsMarginVisible = false;

        Chart1.ChartAreas[0].AxisY.Maximum = 100;
        Chart1.ChartAreas[0].AxisY.Minimum = 0;

        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Chart1.Series.Clear();

        Chart1.Series.Add("Head1");
        Chart1.Series[0].ChartType = SeriesChartType.Bar;
        Chart1.Series.Add("Head2");
        Chart1.Series[1].ChartType = SeriesChartType.Bar;
        Chart1.Series.Add("Head3");
        Chart1.Series[2].ChartType = SeriesChartType.Bar;

        /*string black = "#000000";
        string red   = "#FF0000";
        string blue  = "#4981CE";
        Color _color = System.Drawing.ColorTranslator.FromHtml(red);

        if (boardCounter  > 0 && boardCounter  < 30)
        {
            _color = System.Drawing.ColorTranslator.FromHtml(black);
        }
        else if (boardCounter  > 30 && boardCounter  < 65)
        {
            _color = System.Drawing.ColorTranslator.FromHtml(red);
        }
        else if (boardCounter  > 65 && boardCounter  < 100)
        {
            _color = System.Drawing.ColorTranslator.FromHtml(blue);
        }*/

        Chart1.Series["Head1"].Points.AddY(boardCounter);
        //Chart1.Series["Series2"].Color = _color;
        Chart1.Series["Head2"].Points.AddY(boardCounter);
        //Chart1.Series["Series3"].Color = _color;
        Chart1.Series["Head3"].Points.AddY(boardCounter);
        //Chart1.Series["Series4"].Color = _color;

        boardCounter += 1;
        if (boardCounter > 100) boardCounter = 1;

    }
}
}

1 个答案:

答案 0 :(得分:0)

如果要更改Series所有条形/列的颜色,将更改为该新颜色。相反,您可能只想更改要更改的Point(s)的颜色!

这会为Points添加一些Series,并为每个{Color}提供略有不同的颜色:

  Series S1 = chart1.Series[0];
  S1.ChartType = SeriesChartType.Column;
  S1["PixelPointWidth"] = "3";
  ChartArea CA = chart1.ChartAreas[0];
  CA.AxisX.Minimum = 0;
  CA.AxisX.Maximum = 100;

  for (int d = 0; d <= 100; d ++)
  {
      S1.Points.AddXY(d, 100 + d * 3);
      S1.Points[d].Color = Color.FromArgb(255, d, d * 2, 255 - d * 2);
  }

enter image description here