Monday, December 30, 2019

Charting with Databases DB Course

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 youll have to experiment with it to discover all that it is capable of and how it can best suit 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 youll see. The Chart editor will guide you through the steps to connect to your data—you dont 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. Preparing to Chart 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 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 NumOrdersFROM customer, ordersWHERE customer.custno orders.custnoGROUP BY customer.CompanyORDER BY SUM(orders.itemstotal) DESCThis query uses two tables: orders and customer. Both tables were imported from the (BDE/Paradox) DBDemos database to our demo (MS Access) database. 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. Sel ect OK.) Even though you dont need a persistent set of fields to work with a DBChart component - well 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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.