0

我需要根据可以从我拥有的 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);
}
4

1 回答 1

5

尝试将 chart1 设置为 Chart 的新实例(我假设就是这样!)

所以它会是这样的:

 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); 
} 
于 2012-06-17T21:28:47.530 回答