Reputation: 13
I am using
System.Windows.Forms.DataVisualization.Charting.Chart
reference and I wand to build a chart without chart element in my winform UI , I just want to create a chart with code and store it as an image. but after code runs I got and empty image saved. my code :
Chart ch = new Chart();
ch.Series.Add("tt");
ch.Series["tt"].Points.AddXY(1, 10);
ch.Series["tt"].Points[0].SetValueY(4);
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "savedImg.jpg");
ch.SaveImage(path, ChartImageFormat.Jpeg);
and here is the output : enter image description here
please help me .
Upvotes: 1
Views: 650
Reputation: 1598
You need to setup your chart manually when creating a chart control yourself. Things like Chart Areas legends and titles etc are added in through designers in the background. When creating from code you must setup all the details yourself. I tested this code and it produces a image of the chart.
Chart ch = new Chart();
// Edit for your Chart Title
ch.Titles.Add(new Title("chart For Saving"));
// To display your tt series on the legend
ch.Legends.Add(new Legend());
ch.ChartAreas.Add(new ChartArea());
ch.Series.Add("tt");
ch.Series["tt"].Points.AddXY(1, 10);
ch.Series["tt"].Points[0].SetValueY(4);
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "savedImg.jpg");
ch.SaveImage(path, ChartImageFormat.Jpeg);
Upvotes: 1