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

Off-screen (In-Memory) RadCartesianChart Rendering

7 Answers 237 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Reilly
Top achievements
Rank 1
Veteran
Reilly asked on 12 Jun 2019, 12:56 PM

Is it possible to create a RadCartesianChart that is NOT visible and use the engine to draw a chart and then save the resulting image? Like drawing to an in-memory bitmap.

I use RadCartesianChart to great effect in interactive mode, but am planning a "batch" mode for my application where I need to build some number of charts and save them to files automatically. I don't really want to create a placeholder control and have it flashing at the user whilst the batch mode is running.

Thanks.

-John.

 

7 Answers, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 17 Jun 2019, 09:54 AM
Hello John,

Yes it is possible. What you can do is to create your chart in code behind. First you will need to measure and arrange it and call the UpdateLayout() method.
chart.Measure(new Size(400, 400));
chart.Arrange(new Rect(new Size(400, 400)));
chart.UpdateLayout();

Then you can use the ExportToImage() extension method to export it. 

Give this approach a try and let me know if further questions arise.

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Reilly
Top achievements
Rank 1
Veteran
answered on 01 Aug 2019, 01:43 PM

Hi Dinko,

This works.

For anyone who tries this himself, there is only one bit of extra magic. If you want to reuse the same RadCartesianChart control over and over again, you do as Dinko says after the first time you add all of the series:

chart.Measure(new Size(400, 400));
chart.Arrange(new Rect(new Size(400, 400)));
chart.UpdateLayout();

 

Then you clear the chart series, add your new ones, and call InvalidateVisual() after Arrange() and before UpdateLayout():

chart.Measure(new Size(400, 400));
chart.Arrange(new Rect(new Size(400, 400)));
chart.InvalidateVisual();
chart.UpdateLayout();

 

If you don't do the InvalidateVisual(), you don't get your new series. What you get is the axes and no series.

Thanks very much Dinko!

Regards,

-John Reilly.

0
Reilly
Top achievements
Rank 1
Veteran
answered on 16 Aug 2019, 05:53 PM

The above works fine, EXCEPT that it chops off any border on the bottom and right. That is, if I create my chart with a 1.0 border all around, the image that gets exported has a border on the left and top, but none on the right and bottom.

How can I fix that?

Thanks.

-John.

0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 21 Aug 2019, 10:30 AM
Hello Reilly,

I have tested the described behavior but wasn't able to reproduce it. I am attaching the sample project which I used for testing purposes. Can you take a look at it and let me know what I need to modify to reproduce this scenario.

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Reilly
Top achievements
Rank 1
Veteran
answered on 23 Aug 2019, 11:18 AM

Dinko,

You are totally right. It works. I had myself created a sample project. When I went back to revisit my sample, the border was there.

In my application, I must have done something different. I'll have to find that.

Thanks for your help. I should have been more careful!

Regards,

-Reilly.

 

0
Reilly
Top achievements
Rank 1
Veteran
answered on 23 Aug 2019, 12:47 PM

For anyone who gets here: my problem with the chopped border turned out to be because I was using a non-integral size for the chart. It was something like (795.57, 551.369). A 1 pixel border did not show up on the bottom or right sides. When I adjusted the size to be an even number of pixels (e.g. 795, 551), the border was perfect.

Oddly, it seemed that the exported image was clearer with an integral size. I didn't even notice the saved image was fuzzy until compared with an image with integral size.

This all works. I'm creating charts at runtime in a background thread. The secret is to create the background thread and set it to be STA before running it. Then all works.

Thanks to Dinko.

-reilly.

 

0
Jason D
Top achievements
Rank 1
Veteran
answered on 30 Mar 2021, 05:03 PM

For anyone else who winds up here, another tip. I'm not creating the chart directly. I have a usercontrol with a chart on it. The usercontrol is bound to a class and the chart also picks up this binding. The chart binds fine but the range for the axes does not get updated. I don't know if this is related to me using a usercontrol. The trick was to wait for the databinding to finish before calling Measure. In VB, you can do this like so:

UserControlWithChart.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Background, New Action(Function() True))
UserControlWithChart.Measure(....)

Tags
ChartView
Asked by
Reilly
Top achievements
Rank 1
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Reilly
Top achievements
Rank 1
Veteran
Jason D
Top achievements
Rank 1
Veteran
Share this question
or