chart控件多个ChartArea
最近做的功能正好设计到Chart控件多个ChartArea的使用,就做了一个简单的小例子总结一下。
1、在窗口上添加Chart控件
2、chart控件属性设置:【图表】–【ChartAreas集合】。此处设置了2个ChartArea。
3、设置曲线:如果曲线较少的话,可以在【属性】-【图表】-【Series集合】中添加曲线,
曲线较多,可以自己写代码设置。
这个例子中我用代码设置了3条曲线。
全部代码如下:
private void Form1_Load(object sender, EventArgs e)
{
init();//曲线初始化
timer1.Start();//开启计时器
}
/// <summary>
/// 曲线初始化
/// </summary>
private void init()
{
//清除默认的series
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);
}
}
/// <summary>
/// 曲线
/// </summary>
private static string[] SERIES =
{
"曲线一","曲线二","曲线三"
};
/// <summary>
/// 计时器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
- 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
结果如图所示:
</div><div><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
</div>