Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
115
XamChart RenderTargetBitmap and DataBinding: Broken...
posted
I am writing an application where I must render a bitmap of a chart without in any way visually displaying it (so no adding it to the visual tree.) The only technique I have seen so far involves using a RenderTargetBitmap to do so.

In a previous post, [Infragistics] Andrew Smith posted code which purported to solve this problem. It worked, but only if you did no data binding. I have modified it to the following state to demonstrate the problem. (You should simply be able to paste this code into a class file, and add the appropriate references. The class extends window, so simply run the project to see the result.)

using System;

using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Infragistics.Windows.Chart;

public class VisualChildWindow : Window {
public VisualChildWindow() {
XamChart testChart = new XamChart();
testChart.DataBind += chart_DataBind;

testChart.BeginInit();
testChart.Width = 400;
testChart.Height = 200;
testChart.EndInit();

addSeriesAndData(testChart);

testChart.Measure(new Size(400, 200));

testChart.Refresh();
testChart.Arrange(new Rect(0, 0, 400, 200));
testChart.UpdateLayout();

RenderTargetBitmap bmp = new RenderTargetBitmap(400, 200, 96, 96, PixelFormats.Pbgra32);
bmp.Render(testChart);
Image image = new Image() { Source = bmp };
image.Stretch = Stretch.None;
// Content = testChart;
Content = image;
Panel panel;
}

void chart_DataBind(object sender, DataBindEventArgs e) {

}

private void addSeriesAndData(XamChart chart) {
var series = new Series();
series.DataSource = new ArrayList { new{number = 1}, new{number = 2}, new{number = 4.6} };
series.DataMapping = "Value=number";
chart.Series.Add(series);
chart.DataSourceRefresh();
}

[STAThread]
public static void Main() {
var window = new VisualChildWindow();
var app = new Application();
app.Run(window);
}

}

Near the bottom of the main method, you will see two different lines, one setting Content to the chart, the other the image. You can swap them back and forth. If you set the chart as the content, you can verify that it is rendering correctly. Swap them back to test the image.

Finally, you will see a call to addSeriesAndDatabind(). This method sets up the databinding. If you comment it out, you get the default rendering of the chart.

Now to the problem. Comment out the call to addSeriesAndDatabind(), and run. You will see the chart gets rendered correctly as an image. Restore the call to addSeriesAndDatabind() and run, and you will see the chart render itself, but with no data. If you put a breakpoint on chart_DataBind() (which I have wired up as a handler for DataBind()) you will see that it never gets called. Notice also that I am already calling chart.DataSourceRefresh(), which according to the documentation is supposed to force a refresh.

It looks for all the world as though if only I could force the chart to databind, I'd be home free. I can't seem to do that, however. Perhaps somebody at infragistics can give a little more insight as to the life of this control. Or better yet, take the above source code and make it work.

Thanks,
Jody