Render charts on the server side

5 posts, 0 answers
  1. Petar
    Petar avatar
    2 posts
    Member since:
    Aug 2012

    Posted 12 Dec 2013 Link to this post

    Hi there,

    We use Telerik charts as part of our Silverlight application.

    We would now like add a feature that exports multiple charts to PowerPoint, PDF, etc. in an automated way, at certain time intervals.

    Is there some way to get the same Telerik charts generated /rendered on the server side?

    I’m not sure if it’s possible to get XAML working on the server, and whether there this can be done withTelerik.

    Thanks,
    Pete

  2. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 17 Dec 2013 Link to this post

    Hello Pete,

    RadCartesianChart can be rendered outside the visual tree with no problem. Take a look at this code:

    private RadCartesianChart CreateChart(Size size)
    {
        RadCartesianChart chart = new RadCartesianChart();
        chart.HorizontalAxis = new CategoricalAxis();
        chart.VerticalAxis = new LinearAxis();
        BarSeries series = new BarSeries();
        series.DataPoints.Add(new Telerik.Charting.CategoricalDataPoint() { Category = "A", Value = 1 });
        series.DataPoints.Add(new Telerik.Charting.CategoricalDataPoint() { Category = "B", Value = 2 });
        series.DataPoints.Add(new Telerik.Charting.CategoricalDataPoint() { Category = "C", Value = 3 });
        chart.Series.Add(series);
     
        chart.ApplyTemplate();
        chart.Measure(size);
        chart.Arrange(new Rect(size));
     
        return chart;
    }

    This will return a bar chart that can be exported as a picture easily. Here is how:

    private Stream Render(FrameworkElement element)
    {
        MemoryStream ms = new MemoryStream();
        Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(element, ms, new PngBitmapEncoder());
        return ms;
    }

    Using this code is as easy as:

    RadCartesianChart chart = this.CreateChart(new Size(300, 300));
    Stream ms = this.Render(chart);
    // Do something with this stream

    Hope this code gets you started!

    Regards,
    Yavor
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
  4. Sudipto
    Sudipto avatar
    2 posts
    Member since:
    Dec 2014

    Posted 09 May in reply to Yavor Link to this post

    Hi Yavor,

    Does this still work? I am on the current latest version 2016.1.217. I can get your example to compile in a [STAThread] console application with the appropriate dependencies. I can see the background color of the RadCartesianChart change when I set a different color for it, but I cannot see the series plotted out in the exported image.

    Thanks and regards.

    Sudipto

  5. Sudipto
    Sudipto avatar
    2 posts
    Member since:
    Dec 2014

    Posted 09 May in reply to Sudipto Link to this post

    Should have mentioned that I am trying to use the Telerik WPF library rather than the Silverlight one.
  6. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 10 May Link to this post

    Hi Sudipto,

    We have not made any changes to the chart that could negatively affect exporting it. Perhaps you need to do some additional preparation prior to exporting the chart into an image. I am attaching a small project that uses a console application to create and export a chart. You can examine the PrepareElementForExport method.

    We prefer to keep separate questions in separate threads, so if you have additional questions, please do open a new ticket.

    Regards,
    Petar Marchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
DevCraft banner