Reputation: 77
In my WPF application for Chart Building I'm trying to use WinForms System.Windows.Forms.DataVisualization.Charting;
I have the following code:
private void build()
{
for(int i = 0; i< boundData.Count; i++)
{
var chart = new Chart();
var ser = new Series();
ser.Name = "Series1";
ser.ChartType = SeriesChartType.Bar;
ser.Color = System.Drawing.Color.Blue;
chart.Series.Add(ser);
charts.Add(chart);
for (int j = 0; j < boundData[i].Count; j++)
{
charts[i].Series["Series1"].Points.AddXY(j, boundData[i][j]);
}
}
//Just for tesing Purposes
charts[1].Invalidate();
charts[1].SaveImage("met.png", ChartImageFormat.Png);
}
'boundData' has the following structure
List<List<int>>:
List<int> : [ 1,2,3,4,5,6,... ];
List<int> : [ 1,2,3,4,5,6, ... ];
...
According to debagger the 'Series' property has points.
What I want to do is to build a chart and save it as image. The image saved is just nothing (empty) Can anybody please point what I'm doing wrong or suggest another way of building charts?
Upvotes: 1
Views: 81
Reputation: 1118
You need to add a ChartArea before it will plot
private void build()
{
for (int i = 0; i < boundData.Count; i++)
{
var chart = new Chart();
var ser = new Series();
chart.ChartAreas.Add(new ChartArea("Area1"));
ser.ChartArea = "Area1";
ser.Name = "Series1";
ser.ChartType = SeriesChartType.Bar;
ser.Color = System.Drawing.Color.Blue; ;
chart.Series.Add(ser);
charts.Add(chart);
for (int j = 0; j < boundData[i].Count; j++)
{
charts[i].Series["Series1"].Points.AddXY(j, boundData[i][j]);
}
}
//Just for tesing Purposes
charts[1].Invalidate();
charts[1].SaveImage("met.png", ChartImageFormat.Png);
}
Upvotes: 1
Reputation: 2071
As you are using wpf use oxyplot instead of windows forms. Its an open source project. you can find its demo here. It also have support to save your chart as image.
Samples: https://github.com/oxyplot/oxyplot/tree/master/Source/Examples
Upvotes: 0