The DBChart Component

The DBChart control is another of Delphi's hidden gems that is available to lend additional features to your application. The class is derived from the TChart class and is extended to utilize a dataset for the source of its data series. Using the columns of a dataset for its source, the chart becomes a dynamic display that is updated as the dataset is modified. You have a wide variety of chart type options to choose from and all of the parameters of the display can be controlled within an application. Through interface controls that you provide, the user can spend

Chapter 8-Tips and Techniques hours changing the look and feel of their charts rather than playing solitaire.

All of the above-mentioned things are true, if you can solve the design-time interface! Like QuickReport, this set of controls (TChart, TDBChart, and TQRChart) is provided to Delphi from an outside vendor and it takes a little getting used to in order to become proficient at , its use. Charting also requires the knowledge of a new set of termino!-* ; ogy that applies to the properties and methods. Figure 8.11 is a table of selected key properties sufficient to get you started with the charting tools.

Property

Default Value Purpose

AxisVisible

BufferedDisplay

ChartBounds

True

True

Hides the four Chart axis at once.

Determines whether or not the chart drawn to an internal canvas before being painted on the screen.

Hides the four Chart axis at once.

These settings determine the rectangle that contains the chart.

This indicates the total width in pixels between the top and bottom Chart axis positions.

This property determines the text and drawing attributes of Chart's textual representation of Series and Series values.

ChartHeight

This indicates the total height in pixels between the top and bottom Chart axis positions.

Chart Rect

This property determines the rectangular space defined by the four Chart axis.

ChartWidth

This indicates the total width in pixels between the top and bottom Chart axis positions.

Legend

MaxPointsPerPage

This property determines the text and drawing attributes of Chart's textual representation of Series and Series values.

The property controls the AutoPaging.

The property controls the AutoPaging.

The series are the collections of data values.

Monochrome

Allows charts to be drawn in monochrome.

Series

The series are the collections of data values.

SeriesList.

View3d

This array contains a list of all of series pointers.______

This determines whether or not the chart is drawn in three dimensions.

Figure 8.11 Selected properties of the TDBChart component

Figure 8.11 Selected properties of the TDBChart component

Figure 8J li?

The comp le ted DBChart project

Figure 8J li?

The comp le ted DBChart project

We'll go step by step to build this first chart before applying some of the different options,

Start a new application and add a DataSouroeJ a Query, and a DBGridl to the form. Create the chain pointing the DBGridj to the Data Source and the DataSouixel to the Query.

Set the Query's DatabaseName| property to DBDEMOS. The SQL statement used to build the da case t is:

SELECT Count (*) Customers, State FROM Customer WHERE State <> 1 1 GROUP BY State ORDER BY State

Add this to the SQL property and activate the query to ensure that it is functioning correctly.

Add a DBChart component to the form and set the Align property to al Client] There are two avenues to use when approaching the properties of the chart, though they both lead to the same place, the Chart Editor. The easiest route to get to the form shown in Figure 8.13 is to right-click on the DBChart.I

Figure 8.13

The DBChart properties sheet

Figure 8.13

The DBChart properties sheet

Click Add in the Series tab. A series is the set of data elements to be charted. Multiple series are used on a chart when comparing one set of figures against another. The first thing that you are asked to select is the representation, or type of chart to build. Select the Horizontal Bar chart from the TeeChart gallery and click OK.

The selection of a graph type is determined by the message that you are trying to convey and the data that is involved. A bar chart is excellent for conveying a sense of comparative amounts but would not tell the viewer how much of a whole each bar represents. We selected a horizontal bar chart simply to make the labels describing the states more legible. On the other hand, a pie chart quickly tells the viewer how much of the whole picture is made up by each segment. Line charts show a connected series of points, representing changes in a single data element over a period. The bar chart is selected for this data because we want to quickly ascertain where the bulk of our orders come from.

Double-click on the new series or select the Series tab to edit the source for Seriesl. Select the Data Source tab. Since we're going to use a datasetj object as the source for the series, select Dataset from the drop-down menu.

The tab is now filled with choices that are appropriate to the data source that you've selected. Start by setting the newly displayed Dataset] property to Query 1] The next three choices determine the way that the information will appear on the graph. The Labels property is used to draw labels for each of the bars in the graph, Select the State column for those values. The Bar property determines what value each bar is representing; for this we'll use the Customer column that contains the counts of customers by state.

272 ■ Part III—The Well-Rounded Application

Click Close and your chart will be generated. If any changes are made in the dataset, the chart will be automatically redrawn as long as the AutoRefresh property is True.

Let's view the data in a different format to see if the information is as clearly disseminated as it was with the bar chart. Start the Chart Editor and select the Chart tab. Series1 should be selected already so click Change. You'll see the TeeChart gallery again. Select the pie chart from the available choices. Notice that the types of charts have been limited by the original dataset decisions. Click OK to make the choice and your chart will be redrawn as a pie chart. When the data is viewed in this format, it is not as clear what you are viewing and it doesn't have the quick impact of the bar chart. Change the chart type back to Horizontal Bar chart.

The title of the chart is somewhat misleading when the default value is used. Click on the Titles tab, and in the edit region name the chart Customer Distribution by State. Click Close to complete the chart and view the newly painted control.

This completes the bar chart for this project. An alternative to building the report by hand is to use the TeeChart Wizard. This is accessed by selecting File | New from the Delphi menu and then selecting the Business tab of the repository. The wizard will start by asking you what type of chart you want to create, giving you a choice between the Database Chart and the Non Database Chart. Selecting the Database chart then asks you to select a table to use as the data source. Select the fields you want included on the chart and then the type of chart to display them on. A new form that contains the dataset and the DBChart is generated for you, completing the process.

Was this article helpful?

+1 -1

Responses

  • Stefan
    How i find TDBChart component?
    9 years ago
  • briffo
    How to create a tdbchart?
    9 years ago

Post a comment