Datta Sable

Datta Sable

  • NA
  • 27
  • 845

Unable to render XamChart into bitmap offscreen

Jun 24 2015 7:46 AM
I am trying to render XamChart offscreen
I tried same approach with standard WPF controls like Grid, ellipse, textbox, it is working fine.
But when I tried with XamChart control it is showing empty chart
My requirement is "Draw the chart in code behind and save it as image for further to use in reports."
Here is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infragistics.Windows.Chart;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Collections;
using System.IO;
using System.Windows;
namespace XamChartrendering
{
class Program
{
private const string imageFileName = @"d:\chart.png";
[STAThread]
static void Main(string[] args)
{
Canonical chart = new Canonical();
chart.runIt();
}
}
public class Canonical
{
private const string imageFileName = "imageCan.png";
public void runIt()
{
XamChart chart = getFixedChart(800, 500);
BitmapSource bmp = getBitmap(chart, 800, 500);
writeToFile(bmp);
}
private XamChart getFixedChart(int width, int height)
{
XamChart chart = new XamChart();
chart.BeginInit();
chart.Width = width;
chart.Height = height;
chart.View3D = false;
getDataSeries(chart);
chart.EndInit();
chart.Measure(new Size(width, height));
chart.Arrange(new Rect(0, 0, width, height));
chart.DataSourceRefresh();
chart.Refresh();
chart.UpdateLayout();
return chart;
}
private BitmapSource getBitmap(Visual testChart, int width, int height)
{
RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(testChart);
return bmp;
}
private void getDataSeries(XamChart chart)
{
var series = new Series();
series.DataSource = new ArrayList { new { number = 1, name = "Peter" }, new { number = 2, name = "Susan" }, new { number = 4.6, name = "Edmund" }, new { number = 14.6, name = "Lucy" } };
series.DataMapping = "Value=number; Label=name";
series.Label = "Test Data";
chart.Series.Add(series);
}
private void writeToFile(BitmapSource bmp)
{
File.Delete(imageFileName);
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
FileStream stream = new FileStream(imageFileName, FileMode.Create);
encoder.Save(stream);
stream.Flush();
stream.Close();
}
}
}
It gives empty chart. please help