I used the following in a console app to create and save Chart to png.
It works for LineSeries but the BarSeries is missing.
Please help.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Threading;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Processor processor = new Processor();
processor.Process();
}
}
public class Processor
{
private SaveChartProcessor _ruleProcessor = new SaveChartProcessor();
public void Process()
{
var processRuleThread = new Thread(workerThreadProcessRule);
processRuleThread.Start();
}
private void workerThreadProcessRule()
{
_ruleProcessor.SaveImage();
}
}
public class SaveChartProcessor
{
public void SaveImage()
{
var newThread = new Thread(processChartImageWorker);
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
}
public void processChartImageWorker()
{
var chart = new RadChart();
chart.BeginInit();
chart.Width = 640;
chart.Height = 480;
chart.DefaultView.ChartArea.EnableAnimations = false;
SeriesMapping sm1 = new SeriesMapping();
//sm1.SeriesDefinition = new LineSeriesDefinition(); //<-LineSeries works
sm1.SeriesDefinition = new BarSeriesDefinition(); //<-BarSeries doesn't work
sm1.LegendLabel = "Line Series 1";
sm1.CollectionIndex = 0;
ItemMapping im1 = new ItemMapping();
im1.DataPointMember = DataPointMember.YValue;
sm1.ItemMappings.Add(im1);
SeriesMapping sm2 = new SeriesMapping();
//sm2.SeriesDefinition = new BarSeriesDefinition();
sm2.SeriesDefinition = new LineSeriesDefinition();
sm2.LegendLabel = "Line Series 2";
sm2.CollectionIndex = 1;
ItemMapping im2 = new ItemMapping();
im2.DataPointMember = DataPointMember.YValue;
sm2.ItemMappings.Add(im2);
// Force chart to measure itseft so it provides the actual width and height to export
chart.Measure(new System.Windows.Size(chart.Width, chart.Height));
chart.Arrange(new System.Windows.Rect(chart.DesiredSize));
chart.SeriesMappings.Add(sm1);
chart.SeriesMappings.Add(sm2);
chart.EndInit();
var itemsSource = new List<double>[] { new List<double> { 9, 2, 3, 4 }, new List<double> { 5, 7, 3, 4 } };
chart.ItemsSource = itemsSource;
string path = "d:\\newpic.png";
chart.Dispatcher.Invoke(
new Action(() =>
{
chart.Save(path);
}),
DispatcherPriority.Normal);
}
}
}
It works for LineSeries but the BarSeries is missing.
Please help.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Threading;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Processor processor = new Processor();
processor.Process();
}
}
public class Processor
{
private SaveChartProcessor _ruleProcessor = new SaveChartProcessor();
public void Process()
{
var processRuleThread = new Thread(workerThreadProcessRule);
processRuleThread.Start();
}
private void workerThreadProcessRule()
{
_ruleProcessor.SaveImage();
}
}
public class SaveChartProcessor
{
public void SaveImage()
{
var newThread = new Thread(processChartImageWorker);
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
}
public void processChartImageWorker()
{
var chart = new RadChart();
chart.BeginInit();
chart.Width = 640;
chart.Height = 480;
chart.DefaultView.ChartArea.EnableAnimations = false;
SeriesMapping sm1 = new SeriesMapping();
//sm1.SeriesDefinition = new LineSeriesDefinition(); //<-LineSeries works
sm1.SeriesDefinition = new BarSeriesDefinition(); //<-BarSeries doesn't work
sm1.LegendLabel = "Line Series 1";
sm1.CollectionIndex = 0;
ItemMapping im1 = new ItemMapping();
im1.DataPointMember = DataPointMember.YValue;
sm1.ItemMappings.Add(im1);
SeriesMapping sm2 = new SeriesMapping();
//sm2.SeriesDefinition = new BarSeriesDefinition();
sm2.SeriesDefinition = new LineSeriesDefinition();
sm2.LegendLabel = "Line Series 2";
sm2.CollectionIndex = 1;
ItemMapping im2 = new ItemMapping();
im2.DataPointMember = DataPointMember.YValue;
sm2.ItemMappings.Add(im2);
// Force chart to measure itseft so it provides the actual width and height to export
chart.Measure(new System.Windows.Size(chart.Width, chart.Height));
chart.Arrange(new System.Windows.Rect(chart.DesiredSize));
chart.SeriesMappings.Add(sm1);
chart.SeriesMappings.Add(sm2);
chart.EndInit();
var itemsSource = new List<double>[] { new List<double> { 9, 2, 3, 4 }, new List<double> { 5, 7, 3, 4 } };
chart.ItemsSource = itemsSource;
string path = "d:\\newpic.png";
chart.Dispatcher.Invoke(
new Action(() =>
{
chart.Save(path);
}),
DispatcherPriority.Normal);
}
}
}