I am using the Silverlight WriteableBitmap command to render a ‘Pie Chart’ using the following code.
Chart GetChart()
{
Chart newChart = new Chart() { Width = 100.0,
Height = 100.0 };
PieSeries pieSeries = new PieSeries();
pieSeries.SetBinding(PieSeries.ItemsSourceProperty, new Binding());
pieSeries.DependentValueBinding = new Binding("Value");
pieSeries.IndependentValueBinding = new Binding("Key");
pieSeries.AnimationSequence = AnimationSequence.FirstToLast;
pieSeries.IsSelectionEnabled = true;
newChart.Series.Add(pieSeries);
newChart.SetValue(Chart.DataContextProperty, new KeyValuePair<string, int>[]
{
new KeyValuePair<string, int>("Work", 9),
new KeyValuePair<string, int>("Driving", 2),
new KeyValuePair<string, int>("Family", 4),
new KeyValuePair<string, int>("Sleep", 8),
new KeyValuePair<string, int>("Friends", 1)
});
return newChart;
}
WriteableBitmap bmapPreviewCanvas = new WriteableBitmap(GetChart, null);
The result I was expecting is a Bitmap with a PieChart. What I got was a Bitmap with the background without any PieChart.
Question is : What should I do to get the Pie Chart rendered in the variable ‘bmapPreviewCanvas’?
Edit : Does this have something to do with the ANIMATIONSEQUENCE ?
I believe your problem is that the instance of the Chart control you are trying to create a bitmap of is not on the visual tree.
I have not actually tried this myself, but I believe that you have to explicitly use the Render() method of the WritableBitmap class if the UIElement is not part of the visual tree. You will also have to call InvalidateMeasure() on the element before calling Render(), and Invalidate() on the WritableBitmap afterward to actually render the bitmap.
From the MSDN documentation:
I would try the following instead: