1. Technology

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/od/usedbvcl/l/aa082101a.htm

was emailed to:

Thanks for sharing About.com with others!

Charting with Databases
Page 1: Introducing the TDBChart.
 More of this Feature
• Page 2: Connect ... Graph
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
 Related Resources
• free DB Course.TOC
• Coloring DBGrid
• Using Data Controls
• more Database articles
 Elsewhere on the Web
• Tee Chart

In most modern database applications some kind of graphical data representation is preferable or even required. For such purposes Delphi includes several data aware components: DBImage, DBChart, DecisionChart, etc. The DBImage is an extension to an Image component that displays a picture inside a BLOB field. Chapter 3 of this database course discussed displaying images (BMP, JPEG, etc.) inside an Access database with ADO and Delphi. The DBChart is a data aware graphic version of the TChart component.

Our goal in this chapter is to introduce the TDBChart by showing you how to integrate some basic charts into your Delphi ADO based application.

TeeChart
The DBChart component is a powerful tool for creating database charts and graphs. It is not only powerful, but also complex. We wont be exploring all of its properties and methods, so you'll have to experiment with it to discover all that it is capable of and how it can best suite your needs. By using the DBChart with the TeeChart charting engine you can quickly make graphs directly for the data in datasets without requiring any code. TDBChart connects to any Delphi DataSource. ADO recordsets are natively supported. No additional code is required - or just a little as you'll see. The Chart editor will guide you through the steps to connect to your data - you don't even need to go to the Object Inspector.
Runtime TeeChart libraries are included as part of Delphi Professional and Enterprise versions. TChart is also integrated with QuickReport with a custom TChart component on the QuickReport palette. Delphi Enterprise includes a DecisionChart control in the Decision Cube page of the Component palette.

  Let's chart! - Prepare
Our task will be to create a simple Delphi form with a chart filled with values from a database query. To follow along, create a Delphi form as follows:

1. Start a a new Delphi Application - one blank form is created by default.
2. Place the next set of components on the form: ADOConnection, ADOQuery, DataSource, DBGrid and a DBChart.
3. Use the Object Inspector to connect ADOQuery with ADOConnection, DBGrid with DataSource with ADOQuery.
4. Set up a link with our demo database (aboutdelphi.mdb) by using the ConnectionString of the ADOConnection component.
5. Select the ADOQuery component and assign the next string to the SQL property:

SELECT TOP 5 customer.Company,
SUM(orders.itemstotal) AS SumItems,
COUNT(orders.orderno) AS NumOrders
FROM customer, orders
WHERE customer.custno = orders.custno
GROUP BY customer.Company
ORDER BY SUM(orders.itemstotal) DESC

This query uses two tables: orders and customer. Both tables were imported from the (BDE/Paradox) DBDemos database to our demo (MS Access) database back in the chapter 11. This query results in a recordset with only 5 records. The first field is the Company name, the second (SumItems) is a sum of all the orders made by the company and the third field (NumOrders) represents the number of orders that were made by the company. Note that those two tables are linked in a master-detail relationship.

6. Create a persistent list of database fields. (To invoke the Fields Editor double click the ADOQuery component. By default, the list of fields is empty. Click Add to open a dialog box listing the fields retrieved by the query (Company, NumOrders, SumItems). By default, all fields are selected. Select OK.) Even though you don't need a persistent set of fields to work with a DBChart component - we'll create it now. The reasons will be explained later.
7. Set ADOQuery.Active to True in the Object Inspector to see the resulting set at design time.

The form should look something like:

Empty DB Chart at design time

Note that the DBChart is "empty". We have not connected the recordset with the chart, yet.

Next page > Data Charts with no code - or just a little > Page 1, 2

DB Course Next Chapter >>
>> Lookup!

©2014 About.com. All rights reserved.