我在c#中的windowsform有一个图表。当我第一次输入数据时,图表工作正常,当我点击清除时,一切都清除了。问题是当我第二次输入数据并且加载图形时,然后给出错误。
private void button2_Click(object sender, EventArgs e)
{
//Graph codes
if (p == 1)
{
if (t == 1)
{
if (txtFirstActual.Text == "" || txtSecondActual.Text == "" || txtThirdActual.Text == "" || txtFourthActual.Text == "")
{
MessageBox.Show("Enter All Value Of Actual Years");
}
else
{
int m = Convert.ToInt32(txtForecastingYear.Text);
int FirstYearValue = m - 4;
txtFirstYear.Text = FirstYearValue.ToString();
int SecondYearValue = m - 3;
txtSecondYear.Text = SecondYearValue.ToString();
int ThirdYearValue = m - 2;
txtThirdYear.Text = ThirdYearValue.ToString();
int FourthYearValue = m - 1;
txtFourthYear.Text = FourthYearValue.ToString();
double FirstActual = Convert.ToDouble(txtFirstActual.Text);
double SecondActual = Convert.ToDouble(txtSecondActual.Text);
double ThirdActual = Convert.ToDouble(txtThirdActual.Text);
double FourthActual = Convert.ToDouble(txtFourthActual.Text);
if (FirstActual == 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0) //if the user entered 3 years instead of 4
{
double sum = (SecondActual + ThirdActual + FourthActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual == 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 3 years instead of 4
{
double sum = (FirstActual + ThirdActual + FourthActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual == 0 && FourthActual != 0)//if the user entered 3 years instead of 4
{
double sum = (FirstActual + SecondActual + FourthActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual == 0)//if the user entered 3 years instead of 4
{
double sum = (FirstActual + SecondActual + ThirdActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 4 years
{
double sum = (FirstActual + SecondActual + ThirdActual + FourthActual) / 4;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 4 years
{
double sum = (FirstActual + SecondActual + ThirdActual + FourthActual) / 4;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
MessageBox.Show(" Can't drow graph because there are no sales");
}
}
if (t == 0)
{
MessageBox.Show("Enter Actual for Years");
}
}
if (p == 0)
{
MessageBox.Show("Enter Wanted Year");
}
那是我清晰的按钮: private void button1_Click(object sender,EventArgs e) { //清除按钮
listBox1.Items.Clear();
txtForecastingYear.Clear();
txtFirstYear.Clear();
txtSecondYear.Clear();
txtThirdYear.Clear();
txtFourthYear.Clear();
chart1.Series.Clear();
txtFirstActual.Clear();
txtSecondActual.Clear();
txtThirdActual.Clear();
txtFourthActual.Clear();
if (!String.IsNullOrEmpty(txtFirstActual.Text)) //if there is entry then make clear button available, if not the button will do nothing
{
// clear data
}
答案 0 :(得分:1)
看起来您的问题是,当您调用chart1.Series.Clear()
时,它会清除chart1
的所有索引,然后当您再次单击以填充图表时,您再也不会创建“销售”索引。您需要再次将“销售”图表添加到Series
,然后才能调用其中的方法。
答案 1 :(得分:1)
您的代码取决于“销售”系列,如下例所示:
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
但是,当您清除图表时,可能,您也清除图表系列,因此,第二次,图表无法识别系列“销售”
要纠正此问题,您需要检查该系列是否存在,如果该系列为空,则创建该系列。像这样:
if (this.chart1.Series.Count == 0)
{
// Here you need to properly create the series
this.chart1.Series.Add(new Series("Sales"));
}
这是一个通用示例,可能无法编译,因为我不知道您使用的系列类型或图表控件。所以一定要相应改变。
如果您不知道如何调用本系列的构造函数,请在Designer.cs文件中获取一个loog并复制此代码中的构造函数和属性,这样就可以了。