刷新图表而不重绘它

Refresh Chart Without Redraw it

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

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

这是我的代码:

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;

    }
}
}

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

这会将一些 Points 添加到 Series 中,并为每个颜色赋予略微不同的颜色:

  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);
  }