C#中chart控件增加Series报错
2018-04-14 · 知道合伙人互联网行家
1)在Form1上,布置一个chart控件。然后,在chart1的属性中,点击Series后的 ... 按钮,如下图
2)在Series集合编辑器中,点添加,然后设置图的类型,如下图
3)修改Series1和Series2的颜色和宽度,如下图(图中以Series2为例,对Series1做同样的操作)
4)后台代码
1234567891011121314151617181920212223242526272829303132333435363738using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); chart1.ChartAreas[0].AxisX.Minimum = 1; chart1.ChartAreas[0].AxisX.Maximum = 8; // Series[0]就是上面设计器中的Series1 chart1.Series[0].Points.AddXY(2, 95); chart1.Series[0].Points.AddXY(3, 30); chart1.Series[0].Points.AddXY(4, 20); chart1.Series[0].Points.AddXY(5, 23); chart1.Series[0].Points.AddXY(6, 60); chart1.Series[0].Points.AddXY(7, 87); // Series[1]就是上面设计器中的Series2 chart1.Series[1].Points.AddXY(2, 60); chart1.Series[1].Points.AddXY(3, 30); chart1.Series[1].Points.AddXY(4, 16); chart1.Series[1].Points.AddXY(5, 50); chart1.Series[1].Points.AddXY(6, 70); chart1.Series[1].Points.AddXY(7, 20); // 隐藏Legend[0] chart1.Legends[0].Enabled = false; } }}
运行效果