C1Chart can be directly exported to a variety of image formats including Bmp, Png, Gif, Jpeg, Tiff and Wmp. It provides a SaveImage method to implement the same. A typical user scenario here can be of a TabControl containing chart objects in its Tabs and the user wishes to save all the chart images at once without viewing them. This blog talks about how one can achieve the same using C1WPFChart.
First of all populate the chart objects contained inside different tabs of the TabControl with some random data.
// create tabs with charts
for (int i = 0; i < cnt; i++)
{
charts[i] = new C1Chart() {ChartType = ChartType.Line};
var values1 = new double[npts];
var values2 = new double[npts];
var values3 = new double[npts];
for (int j = 0; j < npts; j++)
{
values1[j] = rnd.Next(100);
values2[j] = rnd.Next(100);
values3[j] = rnd.Next(100);
}
charts[i].Data.Children.Add(new DataSeries() {ValuesSource = values1, Label = "series1" });
charts[i].Data.Children.Add(new DataSeries() { ValuesSource = values2, Label = "series2" });
charts[i].Data.Children.Add(new DataSeries() { ValuesSource = values3, Label = "series3" });
charts[i].Palette = ColorGeneration.Solstice;
charts[i].Children.Add(new C1ChartLegend() { Title = "Chart " + i });
tabs.Items.Add(new TabItem()
{
Header = "Tab " + i,
Content = new Grid() { Children = { charts[i] } }
}
);
}
Then, save all the Chart images in different file(s) on button click. For generating chart images from inactive tabs as well, we need to make use of the Chart's Arrange and UpdateLayout method to perform the correct layout for the chart objects.
private void SaveBtn_Click(object sender, RoutedEventArgs e)
{
// use size of visible chart
var sz = new Size(charts[0].ActualWidth, charts[0].ActualHeight);
for (int i = 0; i < cnt; i++)
{
using (var file = File.Open("chart" + i + ".jpg", FileMode.Create))
{
// two arranges is neccesary to perform chart layout
charts[i].Arrange(new Rect(0, 0, sz.Width, sz.Height));
charts[i].Arrange(new Rect(0, 0, sz.Width, sz.Height));
charts[i].UpdateLayout();
charts[i].SaveImage(file, ImageFormat.Jpeg);
}
}
}
This comes to an end of one particular user scenario. There can be many others as well. We are open to your suggestions and implementation of other user scenarios. We would be happy to look into them :) Download Sample CS Download Sample VB