我需要根据我可以从6个组合框中获取的数据创建图表。
组合框中的数据是数字。一旦我在组合框中选择了所需的数字,然后按下一个按钮,根据这些值创建一个图表。
我的问题是我只能做一次。一旦我更改了组合框中的值并再次单击该按钮,我会收到一个异常。
我知道每次按下按钮我都需要创建新图表的实例,但我不知道如何。
private void button1_Click(object sender, EventArgs e)
{
chart1.ChartAreas.Add("chart");
chart1.ChartAreas["chart"].AxisX.Minimum = 0;
chart1.ChartAreas["chart"].AxisX.Maximum = 20;
chart1.ChartAreas["chart"].AxisX.Interval = 1;
chart1.ChartAreas["chart"].AxisY.Minimum = 0;
chart1.ChartAreas["chart"].AxisY.Maximum = 100;
chart1.ChartAreas["chart"].AxisY.Interval = 5;
chart1.Series.Add("xxx");
chart1.Series.Add("yyy");
chart1.Series["xxx"].Color = Color.Black;
chart1.Series["yyy"].Color = Color.Red;
chart1.Series["xxx"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["yyy"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["xxx"].Points.AddXY(comboBox1.Text, comboBox4.Text);
chart1.Series["yyy"].Points.AddXY(comboBox1.Text, comboBox4.Text);
chart1.Series["xxx"].Points.AddXY(comboBox2.Text, comboBox5.Text);
chart1.Series["yyy"].Points.AddXY(comboBox1.Text, comboBox4.Text);
chart1.Series["xxx"].Points.AddXY(comboBox3.Text, comboBox6.Text);
chart1.Series["yyy"].Points.AddXY(comboBox1.Text, comboBox4.Text);
}
答案 0 :(得分:5)
尝试将图表1设置为图表的新实例(我假设它就是这样!)
所以它会是这样的:
private void button1_Click(object sender, EventArgs e)
{
chart1 = new Chart();
chart1.ChartAreas.Add("chart");
chart1.ChartAreas["chart"].AxisX.Minimum = 0;
chart1.ChartAreas["chart"].AxisX.Maximum = 20;
chart1.ChartAreas["chart"].AxisX.Interval = 1;
chart1.ChartAreas["chart"].AxisY.Minimum = 0;
chart1.ChartAreas["chart"].AxisY.Maximum = 100;
chart1.ChartAreas["chart"].AxisY.Interval = 5;
chart1.Series.Add("xxx");
chart1.Series.Add("yyy");
chart1.Series["xxx"].Color = Color.Black;
chart1.Series["yyy"].Color = Color.Red;
chart1.Series["xxx"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["yyy"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series["xxx"].Points.AddXY(comboBox1.Text, comboBox4.Text);
chart1.Series["yyy"].Points.AddXY(comboBox1.Text, comboBox4.Text);
chart1.Series["xxx"].Points.AddXY(comboBox2.Text, comboBox5.Text);
chart1.Series["yyy"].Points.AddXY(comboBox1.Text, comboBox4.Text);
chart1.Series["xxx"].Points.AddXY(comboBox3.Text, comboBox6.Text);
chart1.Series["yyy"].Points.AddXY(comboBox1.Text, comboBox4.Text);
}