Tutorial 7 - Working with Functions |
Contents page Previous | Next |
Contents |
Function Types
Function characteristics |
A TeeChart Pro Function is a Series, that can be of almost any Series Type, to which an algebraic function is applied and for which the datasource is another Chart Series.
All Functions derive from the TeeFunction Component and inherit TeeFunction's Period property.
TeeChart Pro offers following list of predefined functions:
Function Type |
No. of inputs |
Description |
Copy | 1 | A direct copy of the input Series |
Trend | 1 | Draws best trend line through points of input Series |
Curve Fitting | 1 | Draws curve through data inputs using the TypeFitting formula |
Momentum | 1 | Each Y value is the current point's Y value less the last Period point's Y value |
Exponential Average | 1 | Exponential average based on Weight |
Moving Average | 1 | The Moving Average Function will calculate the simple or weighted average of every group of Period points |
Standard Deviation | 1 | Maps the Standard Deviation (or Complete Standard Deviation) of every group of Period points |
Average | Unlimited | The Average Function will calculate the average of every group of Period points |
Low | Unlimited | The Low Function plots low point of inputs |
High | Unlimited | The High Function plots high point of inputs |
Divide | Unlimited | The Divide Function plots inputs divided in descending order of inclusion |
Multiply | Unlimited | The Multiply Function plots value of inputs multiplied |
Subtract | Unlimited | Plots value of inputs subtracted in descending order of inclusion |
Add | Unlimited | Plots sum of inputs |
Several Function types support only one input Series. However it is possible to chain link Functions, thus, for example, taking the average of several Series in your Chart to create an Average Function Series, then identify the Trend of the average by using the Average Function as the input to the Trend Function.
With the Chart Editor, on the First Chart page, select the Add button as if to add a new Series to the Chart. In the TeeChart Gallery choose the Functions tab to select the Function you require. Each Function is presented as a Line Series, you may change the Series Type associated with the Function later by choosing the Change button on the first Chart Page. Function definitions are easily changed afterwards on the Datasource page of the Function Series. Here, just as easily, you may change the definition of a normal Series that you have added to the Chart to that of a Function (Function is really a definition of datasource, not a definition of Series Type).
The image below shows the Datasource page when editing a Function. The Line Series (Name "Series2", Title "Average") is defined. The left listbox at the bottom of the Datasource page shows other Series in the Chart available for input (here "Series1").
Assuming we start with a completely empty Chart here are the steps in code to build a simple Series-Function related Chart.
procedure TForm1.BitBtn5Click(Sender: TObject); var tmpBarSeries1,tmpBarSeries2:TBarSeries; tmpLineSeries:TLineSeries; begin With Chart1 do begin //Add 2 data Series tmpBarSeries1:=TBarSeries.Create(self); tmpBarSeries2:=TBarSeries.Create(self); AddSeries(tmpBarSeries1); AddSeries(tmpBarSeries2); //Populate them with data (here random) tmpBarSeries1.FillSampleValues(10); tmpBarSeries2.FillSampleValues(10); //Add a series to be used for an Average Function tmpLineSeries:=TLineSeries.Create(self); AddSeries(tmpLineSeries); //Define the Function Type for the new Series tmpLineSeries.SetFunction(TAverageTeeFunction.Create(self)); //Define the Datasource for the new Function Series //Datasource accepts the Series titles of the other 2 Series tmpLineSeries.DataSources.Clear; tmpLineSeries.DataSources.Add( tmpBarSeries1 ); tmpLineSeries.DataSources.Add( tmpBarSeries2 ); // *Note - When populating your input Series manually you will need to // use the Checkdatasource method // - See the section entitled 'Defining a Datasource' //Change the Period of the Function so that it groups averages //every 2 Points tmpLineSeries.FunctionType.Period := 2; End;
We can add another Function to tell us something about the previous Function
procedure TForm1.BitBtn6Click(Sender: TObject); var tmpHighLine:TLineSeries; begin With Chart1 do begin //Add another Series to be used for a 2nd Function tmpHighLine:=TLineSeries.Create(self); AddSeries(tmpHighLine); //Define the Function Type for the new Series tmpHighLine.SetFunction(THighTeeFunction.Create(self)); //Define the Datasource for the new Function Series //Use the existing Function (tmpLineSeries) as input //You should declare tmpLineSeries globally to the module //if you wish to use it between procedures tmpHighLine.DataSource := tmpLineSeries; //Leave the Period at default 0 (No Period set) to draw //A line at Highest of all points of the Average Function end; end;
The examples in the previous section highlight the use of Datasource for poulating a Function by code. Series use Datasource for defining the input for a Function or to define a Series TDataset datasource (see the Tutorial about accessing databases).
Using the Chart Editor, after adding a Function, the Function Series' Datasource page will show a list of available series for inclusion in the function definition. Here you may change the Function Type you wish to apply to the Series and select Series from the Left listBox "Available" and add them to the right Listbox,"Selected".
Datasource by code uses the Series.Datasource property.
Example
Suppose we have 2 data Series in a Chart. We Use the Chart Editor to add a Function composed of the average of the 2 Series:
We add points to the 2 Series:
Dim t As Integer With Chart1 do begin For t := 0 To 10 do begin Series1.Add(2 * t, '', clTeeColor); Series2.Add(3 * t, '', clTeeColor); end; end;
Notice that the Function doesn't display. You need to use the Series.CheckDatasource method to read in values for the Function.
Series3.CheckDataSource; //Read in data for Function
Function definition may be changed at runtime using the Setfunction method to allocate a new Function to the Series.
Chart1.Series3.Setfunction(TMovingAverageFunction.Create(self));
with the above line of code, Setfunction changes the Function for Series3 to a Moving Average.
Period is an important property for working with Functions as the Period defines the range of points across which a Function is cyclically applied.
Example
We have 6 data points (eg. bars of a Bar Series) with values:
3, 8, 6, 2, 9 and 12
We define a Function Series with Period 0 (default) the average drawn is:
6.667
With Period set to 2 we get 3 values of average as output from the function:
5.5, 4 and 10.5
These values will plot centrally in their period range, ie. The 1st value between bars 1 and 2 of the input series, 2nd value between bars 3 and 4, etc..
You may define Period by selecting the function in the Chart Editor or you may modify Period at runtime using FunctionType.
Eg. Where Series 2 is the function series:
Chart1.Series2.FunctionType.Period:=2;
Below are 2 Charts that highlight the effect of an applied Period
![]() | ![]() |
![]() |
![]() |