如何限制图表上的数据收集?

时间:2016-03-29 18:44:09

标签: c# charts

我有一个C#程序,每秒从Arduino Mega读取数据四次,并在两个图表中显示数据,每个图表有两组数据。经过几个小时的运行后,数据开始滞后。我可以看到程序随着时间的推移而增长。我相信问题是图表在每个图表上显示1分钟的快照,但程序只是不断收集数据并创建内存泄漏问题。有没有办法将数据收集限制在一两分钟并转储旧数据?

这是我写的第一个C#程序,我是一个老屁(62)并且患有CRS"无法记住Sh_t"。

非常感谢任何帮助。

布赖恩

Program data collection

// load the main form and sets up the charts
    private void TelemetryForm_Load(object sender, EventArgs e)
    {


        intensityChart.ChartAreas.Add("area");
        intensityChart.Legends.Add("INTENSITY");
        intensityChart.Legends.Add("I SENSE");
        intensityChart.Series.Add("INTENSITY");
        intensityChart.Series.Add("I SENSE");
        intensityChart.Legends["INTENSITY"].Position.Auto = false;
        intensityChart.Legends["INTENSITY"].Position.Height = 10;
        intensityChart.Legends["INTENSITY"].Position.Width = 50;
        intensityChart.Legends["INTENSITY"].Position.X = 20;
        intensityChart.Legends["INTENSITY"].Position.Y = 0;

        intensityChart.Legends["I SENSE"].Position.Auto = false;
        intensityChart.Legends["I SENSE"].Position.Height = 10;
        intensityChart.Legends["I SENSE"].Position.Width = 50;
        intensityChart.Legends["I SENSE"].Position.X = 20;
        intensityChart.Legends["I SENSE"].Position.Y = 0;

        toolCapChart.ChartAreas.Add("area2");

        toolCapChart.Series.Add("CAPACITOR VOLTAGE");
        toolCapChart.Series.Add("TOOL VOLTAGE");
        toolCapChart.Legends.Add("CAPACITOR VOLTAGE");
        toolCapChart.Legends.Add("TOOL VOLTAGE");
        toolCapChart.Legends["TOOL VOLTAGE"].Position.Auto = false;
        toolCapChart.Legends["TOOL VOLTAGE"].Position.Height = 10;
        toolCapChart.Legends["TOOL VOLTAGE"].Position.Width = 50;
        toolCapChart.Legends["TOOL VOLTAGE"].Position.X = 20;
        toolCapChart.Legends["TOOL VOLTAGE"].Position.Y = 0;
        toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Auto = false;
        toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Height = 10;
        toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Width = 50;
        toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.X = 20;
        toolCapChart.Legends["CAPACITOR VOLTAGE"].Position.Y = 0;

        timer1.Enabled = true;
        timer1.Start();

    }

    // adds the data to the charts
    public void chartRead(Double timeofday)
    {

        Double x = timeofday;
        Double z = 1 * timeofday;
        Double y = 6 * timeofday;
        int charttime = 240;


        FontHeight = -1;


        intensityChart.ChartAreas["area"].Position.Auto = false;
        intensityChart.ChartAreas["area"].Position.Y = 8;
        intensityChart.Series["INTENSITY"].Points.AddXY(DateTime.Now.ToLongTimeString(), mic_out);
        intensityChart.Series["I SENSE"].Points.AddXY(DateTime.Now.ToLongTimeString(), i_sense);
        intensityChart.Series["INTENSITY"].LegendText = "INTENSITY";

        intensityChart.Series["I SENSE"].LegendText = "I SENSE";
        intensityChart.Series["INTENSITY"].Color = Color.Blue;
        intensityChart.Series["I SENSE"].Color = Color.Orange;
        intensityChart.Series["INTENSITY"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        intensityChart.Series["I SENSE"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        intensityChart.Series["I SENSE"].YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
        intensityChart.ChartAreas["area"].AxisX.Minimum = intensityChart.ChartAreas["area"].AxisX.Maximum - charttime;
        intensityChart.ChartAreas["area"].AxisX.Interval =charttime/6;


        intensityChart.ChartAreas["area"].AxisY.Minimum = 0;
        intensityChart.ChartAreas["area"].AxisY.Maximum = 100;
        intensityChart.ChartAreas["area"].AxisY.Interval = 10;
        intensityChart.ChartAreas["area"].AxisY.MajorTickMark.Enabled = false;
        intensityChart.ChartAreas["area"].AxisY.MajorTickMark.Interval = 5;
        intensityChart.ChartAreas["area"].AxisY2.MajorTickMark.Enabled = false;
        intensityChart.ChartAreas["area"].AxisY2.MajorTickMark.Interval = 5;
        intensityChart.ChartAreas["area"].AxisX.MinorTickMark.Interval = 5;
        intensityChart.ChartAreas["area"].AxisX.MinorTickMark.Enabled = true;
        intensityChart.ChartAreas["area"].AxisY.MinorTickMark.Interval = 5;
        intensityChart.ChartAreas["area"].AxisY.MinorTickMark.Enabled = true;
        intensityChart.ChartAreas["area"].AxisY2.Minimum = 0;
        intensityChart.ChartAreas["area"].AxisY2.Maximum = isenseYscale;
        intensityChart.ChartAreas["area"].AxisY2.Interval = isenseYscale/10;
        intensityChart.ChartAreas["area"].AxisX2.MinorTickMark.Interval = 5;
        intensityChart.ChartAreas["area"].AxisX2.MinorTickMark.Enabled = true;
        intensityChart.ChartAreas["area"].AxisY2.MinorTickMark.Interval = 1;
        intensityChart.ChartAreas["area"].AxisY2.MinorTickMark.Enabled = true;
        intensityChart.ChartAreas["area"].AxisY.Title = "INTENSITY";
        intensityChart.ChartAreas["area"].AxisY2.Title = "I SENSE";


        toolCapChart.ChartAreas["area2"].Position.Auto = false;
        toolCapChart.ChartAreas["area2"].Position.Y = 8;
        toolCapChart.Series["TOOL VOLTAGE"].Color = Color.Red;
        toolCapChart.Series["CAPACITOR VOLTAGE"].Color = Color.Green;
        toolCapChart.Series["CAPACITOR VOLTAGE"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        toolCapChart.Series["TOOL VOLTAGE"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        toolCapChart.Series["TOOL VOLTAGE"].YAxisType = System.Windows.Forms.DataVisualization.Charting.AxisType.Secondary;
        toolCapChart.Series["TOOL VOLTAGE"].LegendText = "TOOL VOLTAGE";
        toolCapChart.Series["CAPACITOR VOLTAGE"].LegendText = "CAPACITOR VOLTAGE";
        toolCapChart.Series["CAPACITOR VOLTAGE"].Points.AddXY(DateTime.Now.ToLongTimeString(), hv_sense);
        toolCapChart.Series["TOOL VOLTAGE"].Points.AddXY(DateTime.Now.ToLongTimeString(), tool_vin);
        toolCapChart.ChartAreas["area2"].AxisX.Minimum = toolCapChart.ChartAreas["area2"].AxisX.Maximum - charttime;

        toolCapChart.ChartAreas["area2"].AxisY2.Minimum =  0;
        toolCapChart.ChartAreas["area2"].AxisY2.Maximum = toolVoltageChart;
        toolCapChart.ChartAreas["area2"].AxisY.Minimum = 0;
        toolCapChart.ChartAreas["area2"].AxisY.Maximum =hvSenseChartYScale;
        toolCapChart.ChartAreas["area2"].AxisY2.MinorTickMark.Interval = toolVoltageChart/25;
        toolCapChart.ChartAreas["area2"].AxisY2.MinorTickMark.Enabled = true;
        toolCapChart.ChartAreas["area2"].AxisY2.MajorTickMark.Interval = 100;
        toolCapChart.ChartAreas["area2"].AxisY2.MajorTickMark.Enabled = false;
        toolCapChart.ChartAreas["area2"].AxisX.Interval = charttime/6;
        toolCapChart.ChartAreas["area2"].AxisX.MinorTickMark.Interval = 5;
        toolCapChart.ChartAreas["area2"].AxisX.MinorTickMark.Enabled = true;
        toolCapChart.ChartAreas["area2"].AxisY.MinorTickMark.Interval = hvSenseChartYScale/25;
        toolCapChart.ChartAreas["area2"].AxisY.MinorTickMark.Enabled = true;
        toolCapChart.ChartAreas["area2"].AxisY.MajorTickMark.Interval = 1000;
        toolCapChart.ChartAreas["area2"].AxisY.MajorTickMark.Enabled = false;
        toolCapChart.ChartAreas["area2"].AxisY2.Title = "TOOL VOLTAGE";
        toolCapChart.ChartAreas["area2"].AxisY.Title = "CAPACITOR VOLTAGE";

        capVoltageAngularGauge.Value = hv_sense;
        pulseIntensityLinearGauge.Value = mic_ph_out;
        pulseIntensityLinearGauge.Max = mic_ph_scaling;

1 个答案:

答案 0 :(得分:1)

调用series.Points.AddXY(...)后,尝试调用series.Points.RemoveAt(0),如果系列的点数多于可显示的点数。每当新的进入时,这有效地弹出最旧的。

if(toolCapChart.Series["TOOL VOLTAGE"].Points.Count > maxSize)
    toolCapChart.Series["TOOL VOLTAGE"].Points.RemoveAt(0);
相关问题