Advanced Report Design Techniques
With a little bit of basic report design under our belt, we need to have a look at some advanced report design features. These are the features and techniques that can take your report from simple columnar and grouping reports to the more complex analytical reports that turn raw data into valuable information. You may never need to use some of these features, but discussing them now will help us out when we start to look at integrating reports (and their various features) into our applications.
Charting and Graphing
Crystal Reports.NET utilizes a sophisticated graphing engine based on technology from ThreeD Graphics and can create just about any type of chart or graph you can imagine.Throughout this chapter you have been working on the International Sales report. There are groups for the Country, Region, and City fields in the report, and with all of the summary fields and other content the report runs over multiple pages, making it difficult to visualize the information contained within. In the following pages, we are going to add a chart to the first page of the International Sales report, which will provide the user with an overview of the information displayed.To begin, open the International Sales report we have been working with (C:\CrystalReports\Chapter02\Chapter02_Basic) in the Report Designer by double-clicking on the report in the Solution Explorer.
To add a chart to this report, right-click on the report and select Insert | Chart, which will open the Chart Expert dialog, shown below:

You may have noticed when working with the Standard Expert that there was a tab for Charts - the same Chart Expert is used in both places.
There are a number of different templates, including bar charts, pie charts, and 3-D charts, many of which won't be covered in too much detail here. A sample icon of how each will look has been included for you in the Chart Type to the left of the Chart Expert. Please keep in mind that each one of these graph types requires a specific set of information - for example, a pie chart may need only two values passed to it (for the names of the pie slices and their values) while a three-dimensional chart may require three or more values.When you select a graph type that does not match the data you have available and attempt to exit the Report Expert, a dialog will appear with three options:
Continue with Selected Data and Chart Type
Change Data or Chart Type Selection (Return to Chart Expert)
Let the Expert Choose the Most Appropriate Chart for Data Selected
If you select the first option and choose to ignore the warning message, your chart will be unpredictable at best. The charting engine will attempt to work with the values you have presented, but may present a blank graph or one in which the values are just plain wrong. It is better to use the second or third option to ensure that your graph format matches the data available in your report.
In any case, we know that our report has multiple groupings and summary fields, so we should be fine if we select the Pie Chart with the 3D effect, by clicking the icon for this graph:

Next, select the type of graph you want to create using the Data tab of the Chart Expert.

This dialog says there are four different types to choose from (Advanced, Group, Cross-Tab, and OLAP), but OLAP graphs rely on an underlying OLAP grid inserted onto your report, and that feature is not supported in Crystal Reports.NET (at the time of writing, it is supported in the full retail versions of Crystal Reports), so this option will remain permanently grayed out. The Cross-Tab button will also be grayed out if you haven't inserted a Cross-Tab into your report.
Here are the graph types available for use:
Advanced Graphs - Advanced graphs require two fields: an on change of field and a show values field. These are similar to the X and Y fields you would normally use if you were plotting a graph by hand. Just like a manual graph, you can have multiple on change of and show values fields. You can also determine the order for the On change of field and apply TopN/BottomN/Sort All to the same. For the Show field, you can set the summary operator, for example, sum or average, or choose not to summarize the values at all.
Group Graphs - These are based on at least one group and one summary field that appear in your report. Group graphs can be placed at the Report Header/Footer level, or on the Group Header/Footer level, where they will only show the data for that particular group.
Cross-Tab Graphs - These rely on an underlying Cross-Tab grid to provide the required data. These types of graphs can also appear on the Report Header/Footer level or on the Group Header/Footer level with the same filtering of data specifically for that group.
In this case, we have a group and a summary field inserted into our report, so you can select to place the chart Once per report specifying On Change Of Customer.Country, and showing Sum of Customer.Last Year's Sales.Once you have selected the graph and data type for your report, the only thing left in the Chart Expert is to set the text that will appear on your chart or graph, so select the Text tab. By default, Crystal Reports.NET will automatically enter some text for you or you can override the text and it's formatting if you choose. To enter your own text, uncheck the box beside any of the titles and enter your own title in the textbox provided:

When you click OK to exit the Chart Expert, your Chart will be added to your Report Header. When you see the report in the designer, a pie chart is now displayed in the Report Header. This isn't an accurate drawing of your graph, in fact it is nothing like your graph; it is just a placeholder to show where in the report the graph will be positioned. If you preview the graph in a Windows form, it will look something like the following:

You may be thinking that there is too much information on this graph. Crystal Reports.NET has a TopN function to cut your report down to just the top 10 or top 20 customers, if it makes the graph easier to read. To add TopN analysis to your report, right-click on your report and select Report | TopN/Sort Group Expert, which will open the Top N Expert:

To cut your report down to just the top 5 countries, change the sorting dropdown to TopN sorting on the Sum of Last Year's Sales field and enter 5 for the value of N. You will also want to check the box for include others, which creates one large group or pie slice with all of the other countries. To see how much this feature can improve a chart, click on OK, and run the report. With the information for all countries displayed, the chart is a huge improvement, compared to our unfiltered output.

Another option to increase readability would be to make the chart larger. The chart object inserted into your report is just like any other object in that it can be resized to fit your needs; you could even resize the graph to take up the entire first page if that suits the needs of the report.If you have a graph or chart inserted into your report, you can control the content by right-clicking directly on top of the chart and selecting an option from the menu. The Chart Expert can be used to alter the graph in the way that we have described so far in this section. You may have noticed that the Chart Expert includes an option on the first tab labeled Automatically Set Chart Options. If you uncheck this box, another two tabs will appear, allowing you to control other aspects of the report, including the formatting for the graph axes and the general settings (like color, marker size, and so on). Under Format Chart are the Template, General, and Titles options, which also allow you to customize the appearance of your chart. The options in all of these menus are context sensitive; for example, you can perform different types of formatting to a pie chart than you can to a bar chart. These options are all relatively simple, so we'll leave it to you to explore the almost endless possibilities.
Unfortunately, if you have used the retail version of Crystal Reports before, you will probably be wondering what happened to the Chart Analyzer, which allowed you to open the graph in another tabbed window. Crystal Reports.NET does not include the full capabilities of the Chart Analyzer, so if you really need to use some of the advanced formatting features for charts and graphs, you are going to have to buy a retail copy of Crystal Reports.
Important
Be warned that once you open a report within Crystal Reports.NET, you cannot go back and open it in a previous version, as it is not backwards compatible.
Cross-Tabs
Cross-tabs within Crystal Reports.NET can be used to display summarized data in rows and columns, similar to a spreadsheet. If you want to create a report with a cross-tab as its main feature, there is a cross-tab expert available that will guide you through the steps to do so.You can also insert a cross tab into an existing report, which we will walk through here. Create a copy of the International Sales Report we created earlier in this chapter (C:\CrystalReports\Chapter02\Chapter02_Basic), and call the directory Chapter02_CrossTab. Open the Chapter2.sln, and then open international_sales.rpt. Right-click on the report, and then select Insert | Cross-Tab from the menu.

This makes a box appear over your cursor, which you should position over your report, where you want it to appear. Place the cursor over the Group Header #1: Customer.Country - A section, next to the Group #1 Name label. Left-click the mouse, and the following dialog will open. When working with a cross-tab, there are three basic elements: rows, columns, and summarized fields and to start, you will need to select at least one of each using this dialog.

Select Customer.Country for the row, and Customer.Region for the column. Select Customer.Last Year's Sales so it is highlighted, and then click on the Add Summarized Field button, so Sum of Customer.Last Year's Sales appears in the summarized field. Click on OK to create this cross-tab.In the first section under each country your report now has a summary of the last year's sales by region, so in the screenshot below we have the sales by region for the USA. If you select England in the list to the left of the report, the same cross-tab will appear, with values for the regions in England.

By default, Crystal Reports.NET applies some standard formatting to your cross-tab and draws lines and boxes around the columns and rows. With the use of a style sheet, you can apply a pre-defined style to your cross-tab or simply change individual attributes to suit.Cross-tabs have come a long way since the early version of Crystal Reports, and now if the data runs off the page, it is continued on the next page and a margin is used so the data is not cut off, eliminating the old problem of taping multiple report pages back together.The only down side is that there are still some issues with page numbering. If your cross-tab runs across multiple pages, the page number will not work correctly (for instance, if it is three pages across and three down when printed, only the first and every third page will have a number and it will be 1..2..3). Still, those limitations are minor in comparison to the functionality provided.Another cross-tab report is available in the sample files for Chapter 2, which is called cross_tab.rpt. Go to C:\Crystal Reports\Chapter02 \Demo\, and open and run Chapter2.sln. Select Cross-Tab Report from the drop-down list in the top-right of the dialog to view the following report:

Subreports
Within Crystal Reports.NET, multiple subreports can be combined into one main report, which allows you to create information-rich reports from multiple sources and display this information side by side.Both linked and unlinked subreports are available in the code download, in the solution: C:\CrystalReports\Chapter02\Demo\Chapter2.sln. Open and run this solution from Visual Studio .NET and choose the report from the drop-down box at the top right of the window.
Important
To run the unlinked report, you will need access to the Northwind sample database.
Subreports come in two varieties - unlinked and linked. Unlinked subreports allow you to insert subreports that are totally unrelated to the main report content. In the example below, a sales graph has been inserted into an Employee Listing report (included in the sample files as employee_listing_unlinked.rpt). Both of these reports were developed independently and are from different tables and a different data source.

The second type of report is the linked subreport. Linked subreports allow the passing of parameters and variables between the main report and the subreport, which can be used to filter the subreport content. The report shown below has a main report that is the same Employee Listing report, only this time a linked subreport has been inserted showing a commission amount for each employee (included in the sample files as employee_listing_linked.rpt). The commission report is a separate report, but is inserted into the details section and linked on the employee ID field.

For each employee, the subreport is run again and the employee ID is used in record selection on the subreport. When the page is printed, each instance of the subreport is printed next to the corresponding employee, with only their details shown.