In a winforms project, if I add a chart control at design time, the following code builds and displays a chart perfectly.
However, without a chart1
on my form (at design time), if I create the same chart1
at run-time:
Chart chart1 = new Chart();
add the series,
and then add it to my form:
this.Controls.Add(chart1);
an empty chart displays as if the series was not added.
I need to build my chart completely at run-time. Can anyone help me please?
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Test
{
public partial class Charts : Form
{
public Charts()
{
InitializeComponent();
//Chart chart1 = new Chart();
chart1.Series.Clear();
chart1.Titles.Add("My Chart");
chart1.Series.Add("Load");
chart1.Series["Load"].ChartType = SeriesChartType.Column;
chart1.Series["Load"].Points.AddXY(1, 40);
chart1.Series["Load"].Points.AddXY(2, 60);
chart1.Series["Load"].Points.AddXY(3, 80);
chart1.Series["Load"].Points.AddXY(4, 100);
chart1.Series["Load"].Points.AddXY(5, 110);
chart1.Series["Load"].Points.AddXY(6, 80);
chart1.Series["Load"].Points.AddXY(7, 70);
//this.Controls.Add(chart1);
}
}
}