简体   繁体   English

刷新图表而不重绘它

[英]Refresh Chart Without Redraw it

I want to create a bar graph, like it scans something. 我想创建一个条形图,就像扫描一些东西一样。 In addition I want to change the color of the bar according to the x-axis interval at that time. 另外,我想根据当时的x轴间隔更改条形的颜色。

So far, I can managed the scanning function but when I try to change the colour of the bar, the whole bar is changing. 到目前为止,我可以管理扫描功能但是当我尝试更改条形图时,整个条形图都在变化。

Here is my code : 这是我的代码:

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;

    }
}
}

If you are changing the Color of the Series all bars/columns will change to that new Color. 如果要更改Series的颜色,则所有条形/列都将更改为该新颜色。 Instead you probably want to change only the Color of the Point(s) you want to change! 相反,您可能只想更改要更改的Point(s)的颜色!

This will add a few Points to a Series and give each one a slightly different Color: 这将为Series添加几个Points ,并为每个Points添加略有不同的颜色:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM