分享

Charting with Databases

 quasiceo 2013-11-13
Charting with Databases Page 2: Connect, Set, Code ... Graph
 More of this Feature
 Page 1: Intro to TDBChart

Printer friendly versionPrinter friendly version
 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

  Let's chart! - Connect
In most cases, connecting a recordset with a DBChart and setting various properties can be done using the specific Chart editor.

Using the fields of a recordset for the source, the chart becomes a dynamic display that is updated as the recordset if 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.

In general, you fill a chart with data designated in one or more series; each series consists of categories and values. For example, to fill a chart with data about customer orders, you could add a series that charts customer names as the categories and order amounts as the values.

The Chart editor is a one stop shop for all Chart and Series specific parameters. Once you have the DBChart component on a form, you should create one or more series. To accomplish this, open the Chart Component Editor: select the component, right-click it, and choose the Edit Chart menu item. Use the Chart tab to define all you general chart parameters. Select the Series tab and you may choose from your list of series to modify series specific features.

Edit Chart at design time

We'll add two chart series. One that presents the sum of all the orders per company and second that will plot the total number of orders.

Chart tab
Chart Gallery To add a data series press the Add button in the Series tab section of the Chart page. Now simply choose the graph you want to add from Gallery - we'll use the Horizontal Bar. Once we've added the Series to the Chart some random values are displayed, in the Chart at design-time, to easily follow any changes you are making.
Note that a chart can have multiple series; if they are all of the same type they will integrate better, as in the case of multiple bars.
Now add a second series - again use the Horizontal Bar.

Series tab
Selecting the Series tab (or double clicking the desired Series) allows you to edit your Series. Select Series1. Select the DataSource tab. Select "Dataset" from the drop down list. Select AdoQuery1 for the dataset. The next set of choices determine the way how the data form the datasource will appear on the graph (different kinds of charts have different properties and methods). The exact contents of the page will change depending on the Series type you have chosen. The Bar property determines what value each bar is representing - pick SumItems from the list. The Labels property is used to draw labels for each of the bars in the graph. Note that Lables drop down does not list the Company field from the recordset!

Editing Series

BUG! When working with ADO recordset the Chart Editor does not list fields of the WideString type (for the XLabels property). This bug is present in TeeChart v4.xx STANDARD that ships with Delphi 5 and Delphi 6. This is what I got as a reply from the authors of the TeeChart:

"Well, all Delphi versions use the same TeeChart version (v4 STANDARD, I think D4-D6 and CB4-CB5 use the same version). We discovered this bug only when Delphi 5 was released. We fixed it, but not for TeeChart v4 STANDARD version (only for new v5 PRO version).
The good news is user can still connect ADOQuery to specific series at runtime (via code):"

  Series1.DataSource := ADOQuery1;
  Series1.XLabelsSource := ADOQuery1WideStringField1.FieldName;
  Series1.XValues.ValueSource := ADOQuery1FloatField1.FieldName;
  Series1.YValues.ValueSource := ''; { no YValues }
  Series1.CheckDataSource;

I believe we fixed it in TeeChart v5.01 (v5.00) release...
Upgrading to the 5.01 release is not free."

  Let's chart! - Code
Ok, let's see what we can do about this bug. It seems that all the properties of the chart can be set with the Chart Editor except those related to recordset. We'll simply (as suggested) set all from code. Add the next code to the form's OnCreate even handler:

ADOQuery1.Close;
DBChart1.Legend.Visible:=False;
with DBChart1.Series[0] do begin
 DataSource := ADOQuery1;
 XLabelsSource := FieldName1;
 XValues.ValueSource := FieldName2;
 YValues.ValueSource := '';
 Marks.Style := smsXValue;
 CheckDataSource;
end; //with
with DBChart1.Series[1] do begin
 DataSource := ADOQuery1;
 XLabelsSource := '';
 XValues.ValueSource := ADOQuery1NumOrders.FieldName;
 YValues.ValueSource := '';
 CheckDataSource;
end; //with

ADOQuery1.Open;

Of course, I suggest you to browse through the Help system in order to find out about the commands, properties and methods used in this code.

Minor adjustments and notes
If you run the application, you'll notice that the chart is actually three-dimensional - set View3D to False - to display in two dimensions. You can prevent a legend from appearing in your chart by setting its Legend.Visible to False. The default value for Title is 'TDBChart' - change it as you like. Chart uses the current recordset order (ORDER BY keyword in the SQL statement) to populate Series points. This order can be overridden setting the Series values Order property.

Database charts

That's it. It's not to easy, it's not to hard. Explore the Project's Code to find and learn more.

   To the next chapter
If you need any kind of help so far, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

First page > Introducing the TDBChart component > Page 1, 2

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多