Here is a complete example of the simplest real-time display data,
This article refers to the use of MSChart real-time dynamic display line chart, thank the original author's share.
Platform: Vs2013,c#windows program.
Source code Download: http://download.csdn.net/detail/lj22377/7713939
1. First, add the chart control to the window.
2, set chart-series main T attribute:
2.1 isxvalueindexed = true;
2.2 Xvaluetype = time;
3. Write initialization function:
private void Initchart ()
{
DateTime time = DateTime.Now;
Charttimer.interval = 1000;
Charttimer.tick + = Charttimer_tick;
Chartdemo.doubleclick + = Chartdemo_doubleclick;
Series Series = chartdemo.series[0];
Series. ChartType = Seriescharttype.spline;
Chartdemo.chartareas[0]. AxisX.LabelStyle.Format = "HH:mm:ss";
Chartdemo.chartareas[0]. AxisX.ScaleView.Size = 5;
Chartdemo.chartareas[0]. AxisX.ScrollBar.IsPositionedInside = true;
Chartdemo.chartareas[0]. AxisX.ScrollBar.Enabled = true;
Charttimer.start ();
}
4. Write Timer event:
void Charttimer_tick (object sender, EventArgs e)
{
Random ra = new Random ();
Series Series = chartdemo.series[0];
Series. POINTS.ADDXY (DateTime.Now, RA. Next (1));
Chartdemo.chartareas[0]. AxisX.ScaleView.Position = series. points.count-5;
throw new NotImplementedException ();
}
5, write chart double-click event. (Because the slider can be hidden, hidden after not knowing how to recover, so I wrote this double-click event to restore the slider)
void Chartdemo_doubleclick (object sender, EventArgs e)
{
chartdemo.chartareas[0]. AxisX.ScaleView.Size = 5;
Chartdemo.chartareas[0]. AxisX.ScrollBar.IsPositionedInside = true;
Chartdemo.chartareas[0]. AxisX.ScrollBar.Enabled = true;
throw new NotImplementedException ();
}
6, the complete code is as follows:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms; Using System.Windows.Forms.DataVisualization.Charting;
You need to add a namespace namespace Chartrealtimeshow {public partial class Form1:form {public Form1 () {
InitializeComponent ();
Initchart ();
} System.Windows.Forms.Timer Charttimer = new System.Windows.Forms.Timer ();
private void Initchart () {DateTime time = DateTime.Now;
Charttimer.interval = 1000;
Charttimer.tick + = Charttimer_tick;
Chartdemo.doubleclick + = Chartdemo_doubleclick;
Series Series = chartdemo.series[0]; Series.
ChartType = Seriescharttype.spline; Chartdemo.chartareas[0].
AxisX.LabelStyle.Format = "HH:mm:ss"; Chartdemo.chartareas[0]. Axisx.scAleview.size = 5; Chartdemo.chartareas[0].
AxisX.ScrollBar.IsPositionedInside = true; Chartdemo.chartareas[0].
AxisX.ScrollBar.Enabled = true;
Charttimer.start (); } void Chartdemo_doubleclick (object sender, EventArgs e) {chartdemo.chartareas[0].
AxisX.ScaleView.Size = 5; Chartdemo.chartareas[0].
AxisX.ScrollBar.IsPositionedInside = true; Chartdemo.chartareas[0].
AxisX.ScrollBar.Enabled = true;
throw new NotImplementedException ();
} void Charttimer_tick (object sender, EventArgs e) {Random RA = new Random ();
Series Series = chartdemo.series[0]; Series. POINTS.ADDXY (DateTime.Now, RA.
Next (1, 10)); Chartdemo.chartareas[0]. AxisX.ScaleView.Position = series.
points.count-5;
throw new NotImplementedException ();
}
}
}