How to Use teechart. NET database charts

Source: Internet
Author: User
Teechart database chart Introduction

Article Reprinted from: huidu control network [http://www.evget.com/]
You can use a chart editor or a few lines of code to connect a database to a teechart (control download) control at runtime. You should use the tdbchart component. As the name suggests, this teechart component supports data access. Tdbchart is installed on the component palette component Panel of the Data Controls Data Control Section. Tdbchart has all tchart attributes and methods. The key difference is that it also includes access to the borland database engine, thus expanding the size of compilation of this component project. For this reason, we recommend that you use the teechart component unless you want to directly connect to the data source.

Teechart connects to all Borland tdata components, including tTable, tquery, and tclientdataset. When you connect to tables or queries, you do not need to use the tdataset component because teechart can be connected directly.

You can use the chart editor to connect any series to a table or query. Each series can be connected to the same or different tables or queries in the same or different databases.

Because you are not limited to a single database or a single table or query, this design will make the charts connected to the database more flexible. You can also perform data retrieval during design, allowing you to view the actual data during the development process.

Connect to the data source at design time

The following procedure assumes that you have put a tTable, tquery, or tclientdataset in the form. In this example, you can connect tTable to the teechart pro database and install it as an ODBC V3 DSN when installing teechart pro and selecting the employee table.

1. Place a tdbchart control in a form.

2. Double-click the tdbchart control to display the chart editor.

3. Add a series chart control (such as bar series) to the chart ).

4. Click the "series" tab and "datasource" tab.

5. Click the combo box and select "dataset ".

The following interface is displayed:

6. Select the "dataset..." drop-down list box to display the list of available data sources.

By selecting a project from the drop-down list box, you can connect series to the specified table or query.

7. In this example, we connect Table 1 to the "teechart pro Database" and "employee" tables, as shown in.

Define the search field in table or query and add it to series. This is done by selecting appropriate fields for each series part and then using the chart editor.

8. The following figure shows the chart editor datasource tag displayed by the fields field selected in the example.

Each series has a "label" field (which should be of the "string" or "char" type) and a value field (for the "bar" Field of bar series ), it should be an integer, floating, or date-time field type.

In this example, we select the "lastname" field in the "employee" table for the Series label and select the "salary" numeric field for the bar value.

The dataset combo box in the figure above shows the available tables or queries you have created. You can always select a different dataset in this dialog box, or click "Edit..." to edit the dataset.

Now we have allocated an appropriate field to see the result.

9. Click "close" to close the chart editor and return to the programming environment.

In this case, the tchart control will try to open the selected datasource database table or query, start to retrieve records, and assign values to the selected series.

In our example, you will see the following table at design:

If you run this project, the chart Retrieves all records from the datasource data source and adds the values to series. You will see the same chart as the runtime before running the project.

10. Run the application.

You can find an example under the "database Chart" node in the teechart demo.

Connect to the database at runtime

The above design steps can be reproduced programmatically by several lines of code. This gives you more freedom and control in database operations.

Execute the first three steps mentioned above in a new project and a new form.

Each series has the datasource attribute, which determines the Database Value (table or query) origin, "yvalues. valuesource" and "labelssource" attributes to specify the fields to be painted.

The syntax of labelssource and yvalues. valuesource attributes is:

Series1.yvalues. valuesource: = "salary ";

Series1.xlabelssource: = "lastname ";

The series datasource syntax is:

Series1.datasource: = Table1;

// Or

Series1.datasource: = query1;

We recommend that you set the attributes in the above sequence. First, set xlabelssource and yvalues. valuesource, and then set the datasource attribute.

If the database value is changed and you want to "refresh" the connection and perform another numerical search, you should do the following:

Series1.checkdatasource;

The checkdatasource method forces series to retrieve all records again.

All Series also have the "xvalues. valuesource" attribute. This attribute is only used for XY charts (when each point has an X coordinate ).

If teechart cannot find the data source or open the specified table or SQL query, an error is thrown. Remember to set your datasource data source to 'active '.

Single record

Single record data charts allow horizontal data retrieval from a data source record. Select the 'single record 'option from the drop-down list box of teechart editor datasource.

For example:

The above definition uses the first record of the example stock chart of teechart, which is the open, high, low and close values to divide the differences on a series.

Group Data Summary

The data links retrieved through the teechart dataset connection may be grouped by different fields in the dataset that created the selected summary field. Select 'summary 'from the drop-down list box of teechart editor datasource '.

For example:

The above selection divides the bar series of the 'amountpaid' field of the 'Orders 'table in Delphi/C ++ builder, Which is grouped by the customer ID "custno.

If the dataset contains a datetime field, You may select the Group standard as a datetimestep from the first 'group by' drop-down list box. The following is the three-month average amountpaid of the quarter year group using 'salesdate ".

For example:

The Code is as follows:

With Series1 do

Begin

Datasource: = Table1;

Yvalues. valuesource: = '# AVG # amountpaid ';

Xlabelssource: = '# quarter # saledate ';

End;

Database chart-specific events

The tchart control provides an event, which is called every time a record is retrieved and added to a series. This event is "onprocessrecord", and the syntax is:

Procedure tform1.dbchart1processrecord (Sender: tcustomdbchart;

Dataset: tdataset );

Begin

//

End;

You can use this event to generate a silence termination to stop data retrieval. For example, the "database Chart" node is demonstrated in teechart demo.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.