I am using the 2011 Reporting CTP to try anb write code that will take a XamWebChart instance, stick it in a report, then print it. This is all in SL4 and, ideally, no XAML would be involved. I cannot seem to find any useful documentation or samples regarding the relevant classes (Report, Section).
This was a trivial task with Telerik's libraries, but I have not been able to determine how this can be accomplished using Infragistics' libraries.
Hi,
The way to add have a chart printed with the Reporting tool is to create a report in Visual Studio, select a DataSource, drop a chart control on it, attach it to the datasource, set the chart type, etc.
It seems that what you are trying to do is to print a XamWebChart instance. Am I understanding it correctly?
Thanks,
Andres
Hello, Andres: thanks for the quick reply.
Printing is one of the things I want to do with XamWebChart instances, yes. I will also be trying to export them to a variety of formats, but my first experiments have been with simple printing.
In my case I have no DataSource as such, and cannot find documentation on how to construct one that contains a XamWebChart instance. I have no chart control to drop on anything, as the XamWebChart instances are all created dynamically at runtime.
It seems to me like I ought to be able to write code that looks something like this:
var report = new Report();
report.AddSection(myChart)
report.Print(stream);
But it seems it is not that simple. I cannot find any documentatoin on the AddSection() member, so it is not clear that this is even remotely the correct approach. All of the available documentation seems to be focused on databound solutions.
You need to use the Infragistics.Documents.Reports.Report.Report class. In that class the AddSection method does not have any parameters.
Try adding the following Usings
using Infragistics.Documents.Reports.Graphics;
using Infragistics.Documents.Reports.Report;
In the Reporting tool, we use this library to export to PDF and the usual SL4 printing APIs to print it, which basically implies doing the same thing you'd do to print the chart (creating the bitmap and print it).
The Reporting tool does support creating a report and print/export to excel/pdf, but you'll need to follow the 'create a report with VS' route.
Regards,
I apologize if I am being dense, but those namespaces don't seem to exist in any of the InfragisticsSL4 assemblies. Where should I be looking?
They are in C:\Program Files (x86)\Infragistics\NetAdvantage 2011.2\Reporting\Bin\InfragisticsSL4.Documents.Reports.v11.2.dll
I apologize for not being clear ;), I thought you were already using it.
Thank you. With that last bit of information I have been able to achieve at least some success. I now have two routines: one that exports a XamWebChart to an image of some format, and one that attempts to export it as a document of some format.In the case of images, the only formats I could find Infragistics encoders for were JPEG and PNG. In the case of documents, the only formats that I could find support for were PDF and XPS.
In my tests, the JPEG and PDF paths work; PNG and XPS fail. The PNG path fails with a "not implemented" exception inside the IImageEncoder.Encode() method, and the XPS path generates a document, but the XPS Viewer cannot display the resulting page.
My code is as follows:private static Report MakeReportFromAnalytic(XamWebChart analytic, int height, int width){ var report = new Report(); var section = report.AddSection(); var bitmap = new WriteableBitmap(analytic, null); using (var encodedBitmap = new MemoryStream()) { new JpegImageEncoder().Encode(bitmap, new List<EncodingProperty> {new gQualityProperty{Quality = 100}}, encodedBitmap); var nativeImage = System.Drawing.Image.FromStream(encodedBitmap); var infragisticsImage = new Image(nativeImage, false); section.AddImage(infragisticsImage); } return report;}private static void ExportImage(XamWebChart analytic, string defaultExt, string defaultFilter, IImageEncoder encoder, IList<EncodingProperty> encodingProperties){ var dialog = new SaveFileDialog { DefaultExt = defaultExt, Filter = defaultFilter }; if (dialog.ShowDialog() != true) return; using (var stream = dialog.OpenFile()) { var bitmap = new WriteableBitmap(analytic, null); using (var encodedBitmap = new MemoryStream()) { encoder.Encode(bitmap, encodingProperties, encodedBitmap); var byteArray = encodedBitmap.ToArray();
stream.Write(byteArray, 0, byteArray.Length); } }}// These two invocations work...private static void ExportAsPdf(XamWebChart analytic){ ExportDocument(analytic, @"*.pdf", @"Adobe PDF Document (*.pdf)|*.pdf", FileFormat.PDF);}private static void ExportAsJpg(XamWebChart analytic){ ExportImage(analytic, @"*.jpg", @"JPEG Image (*.jpg)|*.jpg", new JpegImageEncoder(), new List<EncodingProperty> { new JpegQualityProperty { Quality = 100 } } );}// These two fail as detailed aboveprivate static void ExportAsXps(XamWebChart analytic){ ExportDocument(analytic, @"*.xps", @"Microsoft XPS Document (*.xps)|*.xps", FileFormat.XPS);}private static void ExportAsPng(XamWebChart analytic){ ExportImage(analytic, @"*.png", @"PNG Image (*.png)|*.png", new PngImageEncoder(), new List<EncodingProperty>());}I see no obvious reason for this, particularly for the "not implemented" exception. Beyond that, is there support (using these mechanisms) for any other document or image types beyond the ones I have listed above?
Yes, the PngEncoder and XPS are not working in the bits you have. We are not using PNG to encode our images, and we found some issues with XPS so it will probably not ship in Reporting either.
The .AddImage class receives and Image instance, so you could try finding any other encoding code in the web and use it to convert the WriteableBitmap into an Image. If you google for 'Joe Stegman’s PngEncoder' you'll find some code.
You basically had achieved all what you could with the bits you have.
Andres:
I was able to build an IImageEncoder implementation around the Stegman PngEncoder class you referred to and, now that I'm actually digging in the right mine, figure out how to use the Word and Excel document classes to get exports to those file formats. Except for the lack of XPS support, I've got all my target formats covered.
That said, PPT (PowerPoint) would be really nice; does Infragistics have anything for that?