Tip 4: Using the Crystal Reports Library
Sometimes the best way to learn the capabilities of a library is to see code that demonstrates the library. Listing 2
contains sample code to demonstrate virtually every piece of functionality in the CGS.CrystalReportsTools library. The listing creates an instance of a report, and shows how I can use the library to preview, print, and export reports.
Tip 5: More Charting Capabilities
In the January-February 2005 issue, I built common charts such as pie, line, and bar charts. I'd like to look at some additional types of charts.
|Figure 12: Output of Scatter graph and data recap.|
Crystal Reports allows me to develop a wide variety of charts that I can easily integrate with other data bands into a report. Let's take a look at another chart typean XY scatter graph.
shows an example of an XY scatter graph for stock/investment information, along with a data summary recap of each stock at the bottom. The scatter graph plots observation points based on two variables: the X-axis represents the percentage of change in stock prices over the last thirty days, while the Y-axis shows the market cap in terms of millions of dollars. Scatter graphs help draw attention to possible correlations between two variables.
As I stated in the initial article, building a chart is about three things: data, data, and data [Big Grin]. I'll construct a supporting data table, tell Crystal Reports what type of chart I want, and fill in some information on what specific data elements I want Crystal Reports to plot. Here are the details for each step:
First, create a typed dataset called dsScatterGraph with columns for the following:
- A string column for the stock name (StockName)
- A decimal column for the percentage of price change over the last 30 days (PricePctChange)
- A decimal column for the stock's market cap dollars (MarketCapDollars)
- A decimal column for the stock's closing price (Price)
Second, create a new Crystal Report and do the following:
- Open the Crystal Reports Database Expert, and select dsScatterGraph as the data source.
- Right-click in the report body and select Insert
Chart. This will launch the Crystal Reports Chart Expert.
- Select the XY Scatter Graph as the Chart Type in the first tab of the Chart Expert.
Finally, I need to tell Crystal Reports which columns to plot:
- Click the Data tab of the Chart Expert to select the columns to plot (see Figure 13).
- Select StockName as the column to plot for the area called On Change of.
- Select PricePctChange and MarketCapDollars as the column for show values(s). These columns represent the variables for the X and Y axes respectively.
I can also right-click the chart to explore the numerous chart options for setting the legend, gridlines, axis fonts, titles, etc. The chart in Figure 13
shows a legend for each stock and sets the Y-axis to scale values in terms of millions of dollars. I can define a legend by right-clicking the graph, selecting chart options, and then clicking the Legend tab (see Figure 14
). To set the scale of the Y-axis, right-click the Y-axis options, and then select the option for Data (Y) axis options. This will load the Data (Y) axis settings screen (see Figure 15
), where I can go to the Numbers tab and set the scale accordingly.
Tip 6: Better Handling of Dynamic Images
|Figure 13: Selecting the appropriate data columns to chart/plot.|
|Figure 14: Chart legend and other chart options.||
I want to generate customer invoices and display the customer logo at the top. The images are stored on a shared network drive.
Depending on whether I'm using Crystal Reports XI, or previous versions of Crystal Reports.NET, there are two ways to handle this.
|Seek and use every information resource available to you to master Crystal Reportsfrom object discovery in IntelliSense to online examples to the list of available Crystal Reports functions in the Crystal Reports Formula Editor.|
In Crystal Reports.NET, the only way I could generate images dynamically was to define a DataColumn in my result set to store the image, insert the DataColumn on the report at design time as a BLOB field, and then populate the data column with the appropriate picture file at run time.
So first, define a new data column in the report DataTable. Since this is a client logo, call the column ClientLogo
. The DataType must be base64binary, which I can set in the XML Schema Editor for the typed dataset. Alternatively, I can define the data type as a byte array (System.Byte) in the standard DataSet Editor.
Second, after I define the binary column in the result set, I must insert the column in the report design area. Note that Crystal Reports will treat the column as a BLOB object, which I can then size and position as needed.
Finally, I need to populate the image at run time when I populate the rest of the result set contents. The sample below is the code for creating a binary reader and inserting the picture image into the dataset.
dsInvoices odsInvoices = new dsInvoices();
string cPicFileName = "c:\\MyLogo.JPG";
// Must create a stream and a reader, from the
// System.IO namespace
FileStream fsStream =
new FileStream(cPicFileName, FileMode.Open);
BinaryReader br = new BinaryReader(fsStream);
// create an instance of the new datarow
dsInvoices.dtInvoicesRow oRow =
// Set the logo, and any other columns
|Figure 16: The Crystal Reports XI Format Editor for Pictures.|
While this works, it does mean that I'll be creating a large dataset if I have many large images. Fortunately, Crystal Reports XI improves on this by allowing me to simply specify the name and location of an image, and then the report will load the image.
First, in the report DataTable, I simply create a string DataColumn to store the full name and location of the image. The location can be a local drive, network shared drive, or even a URL.
Second, in the Crystal Reports XI designer, I need to drop a picture/image control on the report body. Then right-click the picture and select "Format Object" on the shortcut menu. This will load the Format Editor. Go to the Picture tab, as shown in Figure 16
, and then click the formula icon to the right of the Graphic Location option. This will load the Formula Editor, where I can specify the following formula for the Graphic Location. The formula is simply the table name and column that stores the reference to the image (dtInvoiceHeader.ClientLogoName