AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1834&pId=-1
Crystal Reports Charting Fundamentals
page
by Jeff McWherter
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 35161/ 46

Introduction

Rendering data in a report can be a tricky task. There are many options, such as using a logical grouping to display similar data together, displaying the data in a cross tab, or even using a chart to represent the data as a visual rendition of your data. Visualizing data in a chart format is an excellent way to help simplify data. How the data is displayed in the report is often times dependent upon what type of data you are trying to display. Bar charts are handy to show comparisons and trends, where as pie charts can be used to show fractions of a whole or percentages. In this article we are going to explore the fundamentals of creating charts in Crystal Reports 2008. After we have covered the basics, we will dive a bit deeper and explore more advanced techniques that can be used if the charting logic that is built into Crystal Reports 2008 is not adequate for your charting needs.

Using the Crystal Reports Chart Expert

The Crystal Reports Chart Expert can be accessed from either the insert menu of the Crystal IDE or by right clicking on the Crystal Reports design surface and choosing "Chart Expert." The Chart Expert is the interface that allows you to create the charts that will help visualize your data.

Figure 1: The Crystal Reports Chart Expert

Choosing Your Chart Type

Crystal Reports 2008 provides 16 chart types that are very professional and will help you visualize your data. The information in Figure 2 contains the 16 different types of charts for Crystal Reports 2008 and the type of data each chart should represent.

Figure 2: Crystal Reports 2008 Chart Types

Chart Type

Usage

Bar

A chart with rectangular bars with a length proportional to the value that they represent. Useful to show relative comparisons between items or showing the change in a single value over time.

Line

A graph created by connecting a series of data points together with a line; useful for representing changes in multiple values over time.

Area

A line chart with the area between the line and the axis filled in. Volumetrically represents changes in single or multiple values over time.

Pie

A circular chart divided into pie-shaped sectors that represent percentages or relative quantities of the whole.

Doughnut

A pie chart with a blank space in the center; additional doughnut charts can be nested inside of each other that may or may not represent data that is related.

3-D riser

A three dimensional bar chart, useful for comparing multiple groups of values in a single chart.

3-D surface

A three dimensional chart; can be used to show how a particular value changes in relation to two different variables.

XY scatter

A chart that plots data points along two axes; useful for analyzing similarities between pairs or sets of values, or comparing large numbers of data points.

Radar

A circular chart with a radial line representing each variable; points closer to the center represent a lower value. Useful in comparing multiple variables all related to a single item.

Bubble

A chart similar to an XY Scatter chart that replaces data points with bubbles whose size is determined by the relative size of a third value.

Stock

A chart similar to a bar chart, however, the bottoms of the bars do not have to start at the axis. Useful in illustrating values that have a range with a minimum and maximum.

Numeric Axis

A chart that can be similar to a bar, line, or area chart that can use a time/date or a numeric value for the X-axis rather than a relative value.

Gauge

A chart that represents values with a “needle” pointing to the value on the circular chart, similar to the fuel gauge in a car. May have more than one needle representing different values on the same chart.

Gantt

A chart that represents a project schedule, with a bar for each task showing the beginning and ending date for each task.

Funnel

This chart is shaped like a funnel, with the largest value represented at the top in the widest part of the funnel, and progressively decreasing proportional values as the chart goes downwards. The sides of the funnel can “pinch” in to show a decrease within a subset set of values.

Histogram

This chart shows a data set broken down into (usually 7 or 8) bars that are sized based on how many times a value in each range occurred.

Specifying Chart Data

The "Data" tab of the Chart Expert allows you to select which data you would like visualized in the chart.  This tab allows you to perform summary options such as a "count" and "Sum" for the values that are shown on the report. The summary options can be evaluated (calculated) for all records, on change of a field or for each record in the data set.

Figure 3: The Chart Expert Data Tab

Working with Axes

After you have selected your chart type and the data that you would like to include in the chart, you have the ability to modify the axis settings. On charts where data is listed on the different axes, having the ability to set minimum/maximum values, format of the number and how the data is divided in the axes can help the look of how the data is rendered to be more simplistic.

Figure 4: The Chart Expert Axes Tab

The Charting Expert contains features that allow you to modify and format the chart legend and any text on the chart, such as the title, subtitle or footer. Options that are more specific to the chart type will also appear under the options tab. Figure 5 is an example of the options screen for a pie chart. You will notice options in the "Customize settings" region that allows you to set the size of the pie and also detach pie slices.

Figure 5: Chart options specific to the chart type

The Charting Expert is not the only place to create settings for a chart. If you right click on the chart and select "Chart Options," you will be presented with a dialog that allows you to set more options that are specific to the type of chart that is being rendered. Notice in Figure 6 that you are able to "Explode" the pie chart. This feature allows the pieces of the pie chart to be separated.

Figure 6: Chart Options Dialog

Chart Templates

Newer versions of Crystal Reports support the ability to use Chart Templates. Chart Templates are similar to the Report Templates feature included in Crystal Reports XI and 2008. Crystal Reports provides a set of built-in templates that you can use to format your report. The built in templates include options to solve many formatting issues. Situations, such as rendering data with grey scale colors, rendering data in larger fonts to adhere to accessibility standards, and also a template to organize a chart that has a large dataset, are included. The advantage of chart templates in Crystal Reports is that you can use a user defined template. User defined templates are great if you have worked hard to format a chart to meet style requirements or need to adhere to a corporate standard. After you create styling for the first chart, you can save the template (templates are saved as 3tf Chart Template Files) and then use the template to format other charts. Style components such as font, font size and colors are inherited from the selected template.

Figure 7: Selecting a chart template

Chart Expert Color Highlighting

The Charting Expert allows you to select which color will be used for the data that it represents. The "Color Highlight" tab found on the Chart Expert allows you to select a data point and set the exact color that you would like this data point represented in.

Figure 8: Setting the color of specific data

Beyond the Default Crystal Reports Charts

Crystal Reports 2008 is a robust tool that satisfies the reporting needs of many businesses. However, some companies may find the charting features included in Crystal Reports 2008 incomplete and may be looking for additional features. For years in the developer community there has been an outcry for better charting functionality. Crystal Reports 2008 has responded and extends upon the existing Crystal Reports functionality, but there are still gaps for some companies. Sometimes having the ability to add shadows or having the ability to add more intense image features to charts helps give a report the style that it needs.

There are many third party controls that will allow you to pass a set of data to the control, set a few options and the control will render a chart. Controls, such as .net Charting and Dundas, provide a very powerful API to create charts that are both complex and stylish. Most of these third party charting controls have the ability to save the chart as an image such as PDF, PNG or JPG. Since Crystal Reports XI, the Dynamic Image feature has allowed us to set the path of an image dynamically, and therefore, allows us to include charting images from 3rd party controls.

The chart in Figure 9 is a pie chart that was created in Crystal Reports 2008. Someone who is familiar with the business domain that this report was created for would be able to look at this chart and have a good idea of where their customers are located. This chart contains all data that is needed for someone to get the job done, and also looks aesthetically pleasing.

Figure 9: Pie chart created with Crystal Reports 2008

The chart in Figure 10 represents the exact same dataset, but has more pizzazz. The chart in Figure 10 was created using a 3rd party control for .NET called .NET Charting.

Figure 10: Pie chart created with 3rd party control .net Charting

It is not in the scope of this article to cover exactly how each of the third party charting controls creates the chart, it is merely important to be aware of this pattern. Figure 11 creates a chart image (a PNG file) from a set of data and then saves the chart as a PNG to the disk.

Figure 11: Code to create a pie chart with .NET Charting

public string GetChartPath(ref Chart chart)
{
  string tmpRtn = string.Empty;
  // set the title, legend box and label options
  chart.Title = "Customer Regions";
  chart.TitleBox.Padding = 10;
  chart.TitleBox.Background.Color = Color.LightBlue;
  chart.TitleBox.Background.GlassEffect = true;
  chart.TitleBox.Background.Bevel = true;
  chart.LegendBox.Background.Color = Color.LightBlue;
  chart.LegendBox.Background.GlassEffect = true;
  chart.LegendBox.Background.Bevel = true;
  chart.XAxis.Label.Text = "Regions";
  chart.YAxis.Label.Text = "Count of Customers";      
  chart.Width = 800;
  chart.Height = 600;
            
  // set the chart type options
  chart.Use3D = true;
  chart.Type = ChartType.Pie;
  chart.ChartArea.Background.Bevel = true;
  chart.ShadingEffectMode = ShadingEffectMode.Three;
  chart.DefaultSeries.DefaultElement.Transparency = 20;
  chart.DefaultSeries.DefaultElement.ExplodePieSlice = true;
  chart.DefaultSeries.DefaultElement.ShowValue = true;
  chart.DefaultSeries.DefaultElement.ShowValue = true;
 
  // set the charting data
  chart.SeriesCollection.Add(GetChartData());
 
  return chart.FileManager.SaveImage();
}

After you have rendered the chart image, you need to get the path where the image was saved to the report. The easiest way to do this is just pass the chart image path with the set of data you are using to render your report. When the chart image path is included in the dataset, you can easily grab the path to set the dynamic image.

Figure 12: Code to add the chart path to a dataset

DataSet chartData = new DataSet();
DataTable chartTable = new DataTable(); 
chartTable.Columns.Add("ChartImagePath");
            
// build the chart and set the image path to the ChartImagePath field
chartTable.Rows.Add(new object[] { 
GetChartPath(ref chartCustomers) });
 
chartData.Tables.Add(chartTable);
chartData.AcceptChanges();

Adding a dynamic image to your report

Follow the steps given below to add a dynamic image to your report.

·         Add an image to your report.

·         Right click on the image and selecte "Format Graphic."

·         On the "Picture Tab," Click the formula button (X+2 button).

·         Create a formula to pull the chart image path from the dataset.

Figure 13: Graphic location

Figure 14: Graphic location formula

http://aspalliance.com/ArticleFiles/1787/image011.jpg

Conclusion

The tips in this article are just the beginning when it comes to developing useful and professional looking charts for Crystal Reports. Choosing how the data is visualized can be a tricky task, but it is a good bet that the manager who looks at the report every day would be pleased to look at the set of data that is represented in a very simplistic manner.

Using the tips in this article and the knowledge of third party charting controls, you should be able to create very aesthetically pleasing charts that will please your boss and make your co-workers jealous.

 


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 8:24:06 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search