Tip 10: The Baker's Dozen Spotlight: Print Multiple Charts and a Report Band on One Page
Let's take the concept of printing a chart one step further by building a report with two charts and a detail band on a single page. Figure 10
shows a Product Profile Report with a pie chart depicting sales by region, a line chart showing price trends over time, and a report band showing additional monthly product sales information. This report demonstrates Crystal Reports' ability to display multiple levels of data.
|Figure 10: A report can show both detail data and multiple charts.|
Once again, data drives the definition of the charts. The data definition for a pie chart needs two columns. The first column is the description column to annotate each slice (RegionName
); this column maps to the On Change of
data option in the Crystal Chart Export. The second column is the data column to be charted (SalesTY
); this column maps to the Show Value
The data definition for a line chart is similar to a bar chart. You need two columns to divide and subdivide the data (MonthEnding
for the X-Axis and DataType
for the line series, to be mapped to the On Change of
data option). You need one column for the actual data (RetailPrice
for the Show Values
In the previous example, you inserted the bar chart directly into the group header that you defined in the report. Once again, the report contains a group header (this time on the product number instead of the region number). But this time, you'll also need to insert two new sections within the Group Header. To do this, click on the Group Header section on the far right, right-click, and choosing Section Expert. Additionally, you'll need to add two subreports for the Line Chart and Pie Chart, by clicking Insert...SubReport.
Your report shows charts for two different products, so you'll also need to establish a subreport link between the product number in the main report body and the product number in the subreport. Without subreport links, the chart within the subreport cannot identify which group key to chart. Subreport links are established by right-clicking on the subreport and choosing Change Subreport Links. The subreport links screen allows you to define the common key between the main report and a subreport.
At this point you've built the basic report/chart. By following the tip from above to load data for previewing, and right-clicking on sections of each chart to set options, you can customize the appearance of the graphs to suit your needs.
Finally, you can integrate the report into the application by adding it to the project and then calling RunReport
with the DataSet and report object name. So long as the DataSet contains all of the datatables that are used in each report and subreport, RunReport
will loop through each report and subreport object and set the data sources accordingly.
Tip 11: Printing Dynamic Images
I've seen a few requests on different forums to print a different image for each row or set of rows. The image might be a scanned employee photo, a bitmap of a country flag, or some other type of image.
A technique exists that has been presented both on the Business Object's Developer Zone site at www.businessobjects.com/products/dev_zone
, and also in various books on Crystal Reports. In general, you want to create a data column of type Byte, store the image in the Byte column for each row to be represented, generate the XML or XSD file for Crystal Reports, and then drop the image column onto the report in the Crystal Designer.
You can load an image into the Byte column by creating a file stream and binary reader to load the image into a data column:
string cFileName = "MyPicture.JPG";
// must create a stream and a reader
FileStream fsStream = new FileStream(cFileName,
BinaryReader brReader = new
DrNewRow = MyTable.NewRow();
You'll probably read the images out of an array, collection, from a folder, etc. Likely, you will use some type of loop. However, you could also use a simple function that receives the name of the image as a parameter.
Tip 12: Printing Rich Text
In a recent application, I had to take rich text that a user entered and preserve any formatting (bold, underline, etc.) when I reported on the text. It turns out this is easy in Crystal Reports: any data element that could contain rich text attributes can be represented as such by right-clicking on the data element in the Crystal Designer, choosing Format Field, and selecting Text Interpretation...RTF Text on the Paragraph tab of the Format editor.
Tip 13: Deploying a Crystal Reports 10 Application
Business Objects provides documentation on deploying Crystal applications. You can find the documentation by searching for the file Deploying_Cr10_Net.PDF
on Business Object's support site (http:// support.businessobjects.com
). This document covers different Crystal deployment scenarios.
Deploying a Crystal application requires you to create a setup project in Visual Studio .NET. The setup project includes one or more merge modules from Business Objects. You can also find these merge modules on Business Object's support site
by searching for the file CR10_NET_MERGE_MODULES
The PDF file mentioned above covers the steps for creating the setup project. It's worth noting that a setup project cannot be built unless you provide a valid keycode. (Business Objects provides the keycode as part of the registration process when you purchase Crystal Reports).
You can download the complete source code and project for this article from my Web site at www.commongroundsolutions.net
. As this is an ongoing project, I offer a few enhancements from time to time. Check the release notes Word document for details. Sometimes seeing tips like these can spark new ideas for other applications, so maybe you have some new ideas for using other reporting applications.
The objective of this article was to help you become productive with Crystal Reports. You'll find a wealth of technical information on this great reporting tool. As I mentioned above, Business Objects' Developer Zone (http://www.businessobjects.com/products/dev_zone/
) contains a tremendous amount of information. These two books, Crystal Reports .NET Programming
by Brian Bischof, and Professional Crystal Reports for Visual Studio .NET
by David McAmis, are also excellent references.
I can think of a few possibilities to further develop this project that I'll leave in your hands. You may recall that each report had a common header and footer. You could take these and turn them into two subreports (as separate RPT files) that get read in to any report that uses them. You could then use those as template subreports every time you add a new report, as opposed to recreating them (or copy/pasting them) every time.
You might also want to modify the reusable Crystal Manager class to suit to your needs. Sometimes just the process of making one or two changes to an existing library can greatly increase your understanding of how it all works.
Yet another possibility might be modifying these classes to work with ASP.NET. Although the class library is geared toward Windows Forms, you can adapt several of the techniques with some modifications to work within a Web Forms environment.
I encourage you to pull down the code and test project, and to tweak it to your own needs and satisfaction. I plan to maintain and update this library on my Web site. If you have suggestions or ideas you'd like to share, I'd love to hear from you.