在窗口上添加Chart控件
chart控件属性设置:【图表】–【ChartAreas集合】。此处设置了2个ChartArea。
设置曲线:如果曲线较少的话,可以在【属性】-【图表】-【Series集合】中添加曲线,
曲线较多,可以自己写代码设置
这个例子中我用代码设置了3条曲线。
全部代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| private void Form1_Load(object sender, EventArgs e) { init(); timer1.Start(); } private void init() { chart1.Series.Clear(); for (int i = 0;i< 3;i++) { Series series = new Series(SERIES[i]); series.ChartType = SeriesChartType.Spline; series.MarkerBorderWidth = 2; series.MarkerSize = 4; series.MarkerStyle = MarkerStyle.Circle; series.ToolTip = SERIES[i] + " #VAL \r\n #AXISLABEL"; chart1.Series.Add(series); } }
private static string[] SERIES = { "曲线一","曲线二","曲线三" };
private void timer1_Tick(object sender, EventArgs e) { Random r1 = new Random(); int val1 = r1.Next(0, 100); Random r2 = new Random(); int val2 = r1.Next(0, 100); Random r3 = new Random(); int val3 = r1.Next(0, 100); for (int i = 0; i < 3; i++) { this.chart1.Series[0].ChartArea = "ChartArea1"; this.chart1.Series[1].ChartArea = "ChartArea1"; this.chart1.Series[2].ChartArea = "ChartArea2"; chart1.Series[0].Points.AddY(val1); chart1.Series[1].Points.AddY(val2); chart1.Series[2].Points.AddY(val3); } }
|
结果如图所示:
相关链接
- C# CHART控件多个CHARTAREA
=================我是分割线=================
欢迎到公众号来唠嗑: