demo:用代码动态创建C1Chart实例,然后动态设定线条数据,同样的数据,每设定一次,线条颜色都会变化(循环有规律),线条的颜色是随机的吧,使用了random,所以随机值循环有规律?
1、demo截图
2、代码如下:
public partial class Form1 : Form
{
private List<List<PointF>> lstpot = new List<List<PointF>>();
private C1Chart chart;
public Form1()
{
InitializeComponent();
lstpot.Clear();
List<PointF> lst = new List<PointF>();
lst.Add(new PointF(1, 20));
lst.Add(new PointF(2, 22));
lst.Add(new PointF(3, 19));
lst.Add(new PointF(4, 24));
lst.Add(new PointF(5, 25));
lstpot.Add(lst);
lst = new List<PointF>();
lst.Add(new PointF(1, 8));
lst.Add(new PointF(2, 12));
lst.Add(new PointF(3, 10));
lst.Add(new PointF(4, 12));
lst.Add(new PointF(5, 15));
lstpot.Add(lst);
lst = new List<PointF>();
lst.Add(new PointF(1, 10));
lst.Add(new PointF(2, 16));
lst.Add(new PointF(3, 17));
lst.Add(new PointF(4, 15));
lst.Add(new PointF(5, 23));
lstpot.Add(lst);
lst = new List<PointF>();
lst.Add(new PointF(1, 16));
lst.Add(new PointF(2, 19));
lst.Add(new PointF(3, 15));
lst.Add(new PointF(4, 22));
lst.Add(new PointF(5, 18));
lstpot.Add(lst);
}
private void btnShow_Click(object sender, EventArgs e)
{
ChartDataSeriesCollection cdsc;
ChartDataSeries series;
cdsc = chart.ChartGroups.Group0.ChartData.SeriesList;
cdsc.Clear();
for (int i = 0; i < lstpot.Count; i++)
{
series = new ChartDataSeries();
List<PointF> lst = new List<PointF>();
for (int j = 0; j < lstpot.Count; j++)
{
series.PointData.Add(lstpot[j]);
}
cdsc.Add(series);
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
chart = new C1Chart();
chart.ColorGeneration = ColorGeneration.Office;
chart.Header.Visible = true;
chart.Header.Style.Font = new Font("Microsoft Sans Serif", 14, FontStyle.Bold);
chart.Dock = DockStyle.Fill;
chart.BackColor = Color.FromArgb(240, 240, 240);
chart.ChartArea.Style.BackColor = Color.White;
chart.ChartArea.Style.Border.BorderStyle = BorderStyleEnum.Solid;
chart.ChartArea.Style.Border.Color = Color.DarkGray;
chart.ChartArea.Style.Opaque = true;
chart.BackColor = Color.White;
chart.BorderStyle = BorderStyle.Fixed3D;
chart.ChartGroups.Group0.ChartData.SeriesList.Clear();
chart.ChartGroups.Group1.ChartData.SeriesList.Clear();
chart.Legend.Compass = CompassEnum.East;
chart.Legend.Visible = true;
chart.ChartArea.AxisX.GridMajor.Pattern = LinePatternEnum.Dash;
chart.ChartArea.AxisX.GridMajor.Visible = false;
chart.ChartArea.AxisY.GridMajor.Pattern = LinePatternEnum.Dash;
chart.ChartArea.AxisY.GridMajor.Visible = true;
chart.ChartArea.AxisY.GridMinor.Visible = true;
chart.ChartArea.AxisY.Visible = true;
chart.ChartArea.AxisY.Rotation = RotationEnum.Rotate270;
chart.ChartArea.AxisY.AutoMinor = true;
chart.ChartArea.AxisY.AutoMajor = true;
chart.ChartArea.AxisY.AutoOrigin = true;
chart.ChartArea.AxisY.AutoMax = true;
chart.ChartArea.AxisY.AutoMin = true;
chart.ChartArea.AxisY.TickMinor = TickMarksEnum.None;
chart.ChartArea.AxisY2.GridMajor.Pattern = LinePatternEnum.Dash;
chart.ChartArea.AxisY2.GridMajor.Visible = true;
chart.ChartArea.AxisY2.Visible = false;
chart.ChartArea.AxisY2.Rotation = RotationEnum.Rotate270;
chart.ChartArea.AxisY2.AutoMinor = true;
chart.ChartArea.AxisY2.AutoMajor = true;
chart.ChartArea.AxisY2.AutoOrigin = true;
chart.ChartArea.AxisY2.AutoMax = true;
chart.ChartArea.AxisY2.AutoMin = true;
chart.ChartArea.AxisY2.TickMinor = TickMarksEnum.None;
chart.ToolTip.Enabled = true;
chart.ChartArea.AxisY.AnnoMethod = AnnotationMethodEnum.Mixed;
C1.Win.C1Chart.Label lbl2 = chart.ChartLabels.LabelsCollection.AddNewLabel();
lbl2.Compass = LabelCompassEnum.SouthEast;
lbl2.Visible = true;
lbl2.Offset = 3;
chart.ChartArea.PlotArea.Boxed = true;
chart.ToolTip.PlotElement = PlotElementEnum.Points;
chart.ToolTip.SelectAction = SelectActionEnum.MouseOver;
panel3.Controls.Add(chart);
chart.Header.Text = "";
ChartGroup cg = chart.ChartGroups.Group0;
ChartDataSeriesCollection cdsc = cg.ChartData.SeriesList;
cdsc.Clear();
//
cg = chart.ChartGroups.Group1;
cdsc = cg.ChartData.SeriesList;
cdsc.Clear();
chart.ChartArea.AxisX.ValueLabels.Clear();
chart.ChartArea.AxisY.Text = "";
chart.ChartArea.AxisY2.Text = "";
chart.ChartLabels.LabelsCollection[0].Text = "";
chart.ChartArea.AxisY2.Visible = false;
chart.ChartArea.AxisX.AnnotationRotation = -90;
cg = chart.ChartGroups.Group0;
cg.ChartData.HighLight.Activation = HighlightActivationEnum.MouseOver;//当鼠标移上去时高亮显示节点效果
cg.ChartType = Chart2DTypeEnum.XYPlot;
cg.Stacked = false;
chart.Refresh();
}
} |