Charting: Chart not visible
I have a simple programmatically designed test chart chart1 in my WinForm Form1 which displays fine.
When I then move the chart design method to a separate class and call it from main (to become chart2) it is present in Form1 as a blank chart, but the series do not show.
(Chart2 has no chartareas or series of its own).
All charts properties are in the called method: Chart area, series, points, etc. but the chart area remains empty.
Help will be appreciated.
When run in Form1: (displays OK)
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Charting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
drawChart drawchart = new drawChart(); //<<The other class
private void button1_Click(object sender, System.EventArgs e)
{
//this.chart2 = drawchart.draw(); //<< This is the call to the other class
ChartArea ChartArea1 = new ChartArea();
this.chart2.ChartAreas.Add("ChartArea1");
this.chart2.Series.Add("series");
this.chart2.Series[0].Points.AddXY(1, 5);
this.chart2.Series[0].Points.AddXY(2, 4);
this.chart2.Series[0].Points.AddXY(3, 3);
this.chart2.Series[0].Points.AddXY(4, 2);
this.chart2.Series[0].Points.AddXY(5, 1);
this.chart2.Show();
}
}
}
When run in class drawChart: (Displays Empty chart)
using System.Windows.Forms.DataVisualization.Charting;
namespace Charting
{
class drawChart
{
Chart chart1 = new Chart();
public Chart draw()
{
ChartArea ChartArea1 = new ChartArea();
this.chart1.ChartAreas.Add("ChartArea1");
this.chart1.Series.Add("series");
this.chart1.Series[0].Points.AddXY(1, 5);
this.chart1.Series[0].Points.AddXY(2, 4);
this.chart1.Series[0].Points.AddXY(3, 3);
this.chart1.Series[0].Points.AddXY(4, 2);
this.chart1.Series[0].Points.AddXY(5, 1);
return chart1;
}
}
}