This is a migrated thread and some comments may be shown as answers.

Problem with pie chart

4 Answers 93 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Cleanwatts
Top achievements
Rank 1
Cleanwatts asked on 08 Jun 2017, 11:51 AM

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();
}

4 Answers, 1 is accepted

Sort by
0
Cleanwatts
Top achievements
Rank 1
answered on 08 Jun 2017, 11:58 AM
I forgot about some charts I have created using RadPolarChart. I have tried to create some and these also appear blank.
0
Cleanwatts
Top achievements
Rank 1
answered on 08 Jun 2017, 11:59 AM
I also have some charts created using RadPolarChart. These are also rendering a blank image.
0
Martin Ivanov
Telerik team
answered on 13 Jun 2017, 10:46 AM
Hello,

I tested your code and the pie chart is exported to an image properly. Can you take a look at the attached project and let me know if I am missing something? Additionally, you can double check the referenced assemblies are NoXaml. In this case you will need to merge some styles as explained in the Setting a Theme help article.

Regards,
Martin Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Cleanwatts
Top achievements
Rank 1
answered on 26 Jun 2017, 03:27 PM

Hello,

I was using the NoXaml libraries by mistake. I have switched to the other libraries and the problem was gone.

Thanks for the advice.

Tags
ChartView
Asked by
Cleanwatts
Top achievements
Rank 1
Answers by
Cleanwatts
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or