简体   繁体   中英

C# Chart Series not found

Hey I have a problem with chart series

This is in the form.designer:

System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();

chartArea1.Name = "ChartArea1";
this.chart1.ChartAreas.Add(chartArea1);
legend1.Name = "Legend1";
this.chart1.Legends.Add(legend1);
this.chart1.Location = new System.Drawing.Point(7, 17);
this.chart1.Name = "chart1";
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
series1.IsXValueIndexed = true;
series1.Legend = "Legend1";
series1.MarkerColor = System.Drawing.Color.Red;
series1.MarkerSize = 6;
series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Diamond;
series1.Name = "SeriesBatchPoint";
this.chart1.Series.Add(series1);
this.chart1.Size = new System.Drawing.Size(1141, 323);
this.chart1.TabIndex = 3;
this.chart1.Text = "chart1";

In the form I will make a point

chart1.Series["SeriesBatchPoint"].Points.AddXY(BatchCount, aTempWeightMesure);

I get this error:

ArgumentException was unhandled

Additional information: A chart element with the name 'SeriesBatchPoint' could not be found in the 'ChartAreaCollection'.

What am I do wrong?

Try this:

var series1 = chart1.Series.Add("SeriesBatchPoint");
series1.ChartArea = "ChartArea1";
series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
// etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM