Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: 13 Productivity Tips for Crystal Reports and .NET : Page 6

These 13 tips show you how to build tools to address common reporting requirements for your business using Crystal Reports with .NET.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Tip 9: Building a Bar Chart...It's All About Data!
Now it's time for some cool stuff! Crystal Reports provides the ability to construct several types of charts to help provide a pictorial summary of your data. The old expression is true: a picture is worth a thousand words (so long as it's a well-designed picture).

In addition to the common types of charts (bar, line, pie), you can also build area charts, Gantt charts, scatter graphs, funnel Charts, stock charts, bubble charts, and a few others. The true appeal of charting in Crystal Reports is the ability to integrate it into the report just as you would a normal subreport.

The true appeal of charting in Crystal Reports is the ability to integrate it into the report just as you would a normal subreport.
Let's work backward (beginning with the end in mind) and take a look at the type of chart you want to build. Figure 8 shows a stacked bar chart that depicts an organization's costs over the last year and a half. The report displays the cost data by region, broken out by month, and subdivided by cost category. To show trends from the previous year, the report uses a single horizontal line to graph the total costs for the same time period a year ago.

Building a chart in Crystal Reports is all about three things: data, data, and data. I say that partly tongue-in-cheek, because Crystal Reports needs data not only for the pictorial representation, but also needs description data for legends, x-axis descriptions, etc. Basically, the datatable for the graph needs a column (which I've called DataType) to define the cost categories, a column (XaxisLabel) to define the labels along the X-Axis (the months), and a column to store the data itself (graphData).

Building a chart in Crystal Reports is about three things: data, data, and data.
Designing a graph is straightforward in Crystal Reports, although there are a few steps and tweaks to get the chart the way you want it. After designing the data schema, open Crystal Reports, create a new report, and point to the schema. Then create a new chart by choosing Insert...Chart. Crystal Reports presents you with a three-tab Chart Wizard dialog box.

The first tab allows you to select the type of chart. Choose "Bar," and on the right side of the screen, choose the specific type of bar chart (e.g. side-by-side, stacked bar, etc.).

On the second tab, you define the data for the graph (see Figure 9). You need to provide two definitions. For the first definition (the On change of the label), you need to define the division and subdivision. The division for a bar chart is the X-Axis definition (each month), and the subdivision includes any groupings within each month (the cost categories). So drag the two data columns that represent the month and cost categories into the On change of section. The second definition (show value) represents the actual data that Crystal Reports will chart, so drag the data column that contains the actual numeric data into the Show value(s) section.

Figure 9: Establishing data for a chart in the Crystal Reports' Chart Expert is easy too.
At this point, you could stop and save the graph and generate it at runtime against your data. But you have to make a few adjustments to generate a graph identical to the one in Figure 8.

  • To show a legend, right-click on the graph, choose
Chart Options...General, go to the Look tab, and check the checkbox for Show Legend. Note the options underneath the checkbox for the placement and appearance of the legend box.

  • To define a series as a line instead of a graph (the horizontal line depicting costs for a year prior), right-click the series of that bar, choose Chart Options...Series, and change the option for Show selected series as... to a Line.
  • To change the color/font/gradient for any series, label, etc., select the item, right-click, and choose Chart options...selected item.
You can check these options, and other graph options, by right-clicking on a portion of the graph. The short-cut menu provides many different options for customizing chart output.

One final point on charting: the Crystal Reports designer provides the ability to preview a report, so long as the designer has data to use. The schema definition you supplied to Crystal Reports did not contain any data; in that situation, the only way to preview a report is by a trial and error process of running it in the application. That's inefficient, so a workaround is to create an instance of the Typed DataSet in a sample program, populate the dataset with sample data, and write out the dataset to an XML file that includes the schema. You could use sample code like this:

DataSet Ds = new Ds_SimpleBarChart(); // populate Ds with data Ds.WriteXml("GraphSchema.XML", XmlWriteMode.WriteSchema);

When you build the chart, you can specify the XML file as the data source. That satisfies the requirements of providing a schema, and also gives Crystal Reports some data for building the graph in the designer Preview mode.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date