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
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

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.

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.
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

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.

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.

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(....)