I've been using ChartView for some time to create charts from a Windows service without problems. The chart control (RadCartesianChart or RadPieChart) is created and initialized in code, and then is rendered to a bitmap.
Ever since I updated the libraries to the latest version, when I try to create a pie chart, the result is always a blank image. The other chart types continue to work as expected.
The code I'm using is the following:
private
bool
CreatePieChartThread(MemoryStream stream,
int
width,
int
height)
{
RadPieChart chart =
new
RadPieChart
{
Palette = BuildColorPalette(m_chartColors),
SmartLabelsStrategy =
new
PieChartSmartLabelsStrategy
{
DisplayMode = PieChartLabelsDisplayMode.SpiderAlignedOutwards
}
};
var labelBoxFactory =
new
FrameworkElementFactory(
typeof
(TextBlock));
labelBoxFactory.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center);
labelBoxFactory.SetBinding(TextBlock.TextProperty,
new
Binding(
"Label"
));
var valueBoxFactory =
new
FrameworkElementFactory(
typeof
(TextBlock));
valueBoxFactory.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center);
valueBoxFactory.SetBinding(TextBlock.TextProperty,
new
Binding(
"Value"
));
var panelFactory =
new
FrameworkElementFactory(
typeof
(StackPanel));
panelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
panelFactory.AppendChild(labelBoxFactory);
if
(m_showValuesOnPie)
panelFactory.AppendChild(valueBoxFactory);
PieSeries series =
new
PieSeries
{
ShowLabels =
true
,
RadiusFactor = 0.75,
AngleRange =
new
AngleRange(-90, 360),
LabelConnectorsSettings =
new
ChartSeriesLabelConnectorsSettings()
};
series.LabelDefinitions.Add(
new
ChartSeriesLabelDefinition
{
Margin =
new
Thickness(-8, 0, 0, 0),
Template =
new
DataTemplate { VisualTree = panelFactory }
});
// valores ordenados por nome e sem zeros
foreach
(var value
in
m_data.Where(d => d.AlarmOccurrences > 0).OrderBy(d => d.Name))
{
series.DataPoints.Add(
new
PieDataPoint
{
Label = value.Name,
Value = value.AlarmOccurrences,
OffsetFromCenter = 0.05,
});
}
chart.Series.Add(series);
PrepareElementForExport(chart, width, height);
ExportExtensions.ExportToImage(chart, stream,
new
PngBitmapEncoder());
return
true
;
}
protected
void
PrepareElementForExport(FrameworkElement element,
int
width,
int
height)
{
if
(!element.IsInitialized)
{
element.BeginInit();
element.EndInit();
}
element.Measure(Size.Empty);
element.Measure(
new
Size(width, height));
element.Dispatcher.Invoke(() => { });
element.Arrange(
new
Rect(0, 0, width, height));
element.UpdateLayout();
}