0
编程的好日子,C#图表 - 具有不同Y值的两个图表区域
我有一个问题。我创建了一个代码,以chartarea1创建带有datagridview作为数据源的图表。
Chart chart1 = new Chart();
chart1.Size = new System.Drawing.Size(1024, 768);
ChartArea chartArea1 = new ChartArea();
chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisX.IntervalType = DateTimeIntervalType.Months;
chartArea1.AxisX.Interval = 1;
chart1.ChartAreas.Add(chartArea1);
chart1.Series.Add(new Series());
chart1.Series[0].XValueMember = dataGridView1.Columns[0].DataPropertyName;
chart1.Series[0].YValueMembers = dataGridView1.Columns[1].DataPropertyName;
chart1.DataSource = dataGridView1.DataSource;
chart1.Series[0].ChartType = SeriesChartType.Line;
现在我想创建一个相同的XValueMember但来自不同的DataGridView不同YValueMember,例如datagridview2 chart1内第二chartarea。可以做到吗?
在此先感谢。
好了,我想这:
Chart chart1 = new Chart();
ChartArea chartArea1 = new ChartArea();
Series series1 = new Series();
chart1.DataSource = dataGridView1.DataSource;
chartArea1 = chart1.ChartAreas.Add("ca1");
chartArea1.AxisX.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisY.MajorGrid.LineColor = Color.LightGray;
chartArea1.AxisX.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisY.LabelStyle.Font = new Font("Consolas", 8);
chartArea1.AxisX.IntervalType = DateTimeIntervalType.Months;
chartArea1.AxisX.Interval = 1;
series1 = chart1.Series.Add("s1");
series1.Points.DataBindXY(dataGridView1.Columns[0].DataPropertyName, dataGridView1.Columns[1].DataPropertyName);
series1.ChartType = SeriesChartType.Line;
chart1.SaveImage("chart.png", ChartImageFormat.Png);
现在收到此错误:
Y值不能绑定到字符串对象数据。 参数名称:y值