Generating a RadCartesianChart without displaying it

1 Answer 121 Views
Chart
Frank
Top achievements
Rank 1
Frank asked on 28 Mar 2023, 01:46 PM

Hi,

I need to export a chart to pdf without displaying it as part of a background task. Right now I'm having an issue where the chart appears blank when passed through the image saving process I've seen on your demo's. Looking at the code, it has all the values it needs it just isn't being built since it isn't displayed. Is there any way around this?

Thanks,

Frank

1 Answer, 1 is accepted

Sort by
1
Accepted
Martin Ivanov
Telerik team
answered on 28 Mar 2023, 02:38 PM

Hi Frank,

You can see how to export the chart without adding it to the visual tree here: https://docs.telerik.com/devtools/wpf/knowledge-base/kb-chartview-export-to-image-in-code

In case you are not using SeriesProvider (as discussed in the article), you can measure and arrange only the chart before exporting it. For example:

var measureSize = new Size(500, 500); 
chart.Measure(measureSize); 
chart.Arrange(new Rect(measureSize)); 
chart.UpdateLayout(); 

using (Stream fileStream = File.Open("../../myChartPicture.png", FileMode.OpenOrCreate)) 
{ 
	Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(chart, fileStream, new PngBitmapEncoder()); 
} 

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Frank
Top achievements
Rank 1
commented on 28 Mar 2023, 09:46 PM

Thank you for reaching out so quickly!

I've been troubleshooting my code with these tips today but haven't had any luck. I'll share my code snippets to hopefully help figure this out.

for (int j = 0; j < diagram.Shapes.Count; j++)
{
    RadDiagramShape shape = (RadDiagramShape)diagram.Shapes[j];
                    
    if (shape.Name == "ActivityHourOfDayReport")
    {
        shape.ApplyTemplate();
                        
        HourOfDayChartView view = shape.Content as HourOfDayChartView;
                        
        CallCustomDiagramReportViewModel contentModel = (CallCustomDiagramReportViewModel)diagram.DataContext;

        Size chartSize = new Size(500, 180);
        view.Measure(chartSize);
        view.Arrange(new Rect(chartSize));
        view.UpdateLayout();

        view.Chart.BeginInit();
        view.Chart.Height = view.Chart.DesiredSize.Height;
        view.Chart.Width = view.Chart.DesiredSize.Width;
        view.Chart.DataContext = contentModel.HourOfDayReport;
        view.Chart.EndInit();

        //view.Chart.Measure(new Size(view.Chart.Width, view.Chart.Height));
        //view.Chart.Arrange(new Rect(view.Chart.DesiredSize));
                        
        //view.Chart.ApplyTemplate();
        view.Chart.UpdateLayout();
        //view.Chart.BringIntoView();


        //MemoryStream ms = new MemoryStream();
        using (Stream fileStream = File.Open("C:/Users/fegelhoff/Documents/myChartPicture.png", FileMode.OpenOrCreate))
        {
            Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(view, fileStream, new PngBitmapEncoder());
            //diagram.AddImageShape(fileStream, contentModel.HourOfDayReport.Position);
        }
                        
    }
}

In this case view is a UserControl that has a Grid with Text and a RadCartesianChart in it. I've tried a couple of other attempts as well with the commented out code that you see here. This attempt results in this image:

and trying this with view.Chart resulted in this image:

 

Sorry for the long post but hopefully this helps to explain my predicament.

Thanks in advance for all the help!

Frank

Martin Ivanov
Telerik team
commented on 29 Mar 2023, 10:08 AM

To make this work, you will need to assign the DataContext of the Chart object before the measure/arrange calls. I will guess the view model assigned to the DataContext contains the data needed by the chart to get loaded, so you should provide this to the chart before measure arrange its host (the HourOfDayChartView control in your case). I've attached a small sample based on your code. I hope that helps.
Frank
Top achievements
Rank 1
commented on 29 Mar 2023, 09:52 PM

That fixed it, Thank you for your help!
Tags
Chart
Asked by
Frank
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or