IOSTutorial7

From Steema Software Reference Wiki
Jump to: navigation, search

Net iphone.png

Contents page
Previous | Next

Tutorial7 - Working with Functions


Function Types

Function characteristics

A TeeChart NET for iOS 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 Function Class in the Steema.TeeChart.Functions namespace and inherit Function's [#FunctionPeriod Period property]. TeeChart NET for iOS offers the following list of predefined functions:

FunctionsTable.png

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.

Adding a Function

Assuming we start with a completely empty Chart here are the steps in code to build a simple Series-Function related Chart.

[C#] 
public override ViewDidLoad()

               base.ViewDidLoad();

     //Add a data Series
     Line line1 = new Line(tChart1.Chart);

     //Populate it with data (here random)
     line1.FillSampleValues(10);

     //Add a series to be used for an Average Function
     Line line2 = new Line(tChart1.Chart);

     //Define the Function Type for the new Series
     Steema.TeeChart.Functions.Average average1 = new Steema.TeeChart.Functions.Average();
     line2.Function = average1;

     //Define the Datasource for the new Function Series
     line2.DataSource = line1;

     //*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
     line2.Function.Period = 2;
     line2.CheckDataSource();

We can add another Function to tell us something about the previous Function

[C#] 
this.button.TouchUpInside = (sender, e) =>

     //Let us change to 2D for visibility
     tChart1.Aspect.View3D = false;
     //Add another Series to be used for a 2nd Function  
     Line line3 = new Line(tChart1.Chart);
     //Define the Function Type for the new Series  
     Steema.TeeChart.Functions.High high1 = new Steema.TeeChart.Functions.High();
     line3.Function = high1;
     //Define the Datasource for the new Function Series  
     //Use the existing Function (Series2) as input  
     line3.DataSource = tChart1.Series[1];
     //Leave the Period at default 0 (No Period set) to draw  
     //A line at Highest of all points of the Average Function  ;

Function Period

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 (e.g. 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, i.e. 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 relevant Series and Function in the Datasource page and clicking the "Options" tab or you may modify Period at runtime using FunctionType.

e.g. Where line1 is the function series:

[C#] 
line1.Function.Period = 2;

Below are 2 Charts that highlight the effect of an applied Period

Period0.pngPeriod2.png

Period Style

Period can be defined to be a range. This is very useful when using DateTime series and we want to express the "Period" of the function as a TimeStep. The property "PeriodStyle" controls how is "Period" expressed.

For example you can now plot the "monthly average of sales" function just using a normal "Average" function on a date-time source series and setting the function period to "one month":

[C#] 
public override ViewDidLoad()

               base.ViewDidLoad();
     //Add in a BarSeries and Average Function at design-time.
     Random rnd = new Random();
     tChart1.Aspect.View3D = false;  
            
     TimeSpan month = new TimeSpan(30,0,0,0);
     DateTime today = DateTime.Today;

     bar1.Marks.Visible = false;
     bar1.XValues.DateTime = true;
     tChart1.Axes.Bottom.Labels.Angle = 90;

     for(int i = 0; i < 60; i)
        today = today.AddDays(5);
        bar1.Add(today, rnd.Next(100),"",UIColor.Red.CGColor);
    

     average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First;
     average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range;
     average1.Period = month.TotalDays;
     line1.DataSource = bar1;
     line1.CheckDataSource();

This will result in several points, each one showing the "average" of each month of data in the BarSeries.

It's mandatory that points in the source Series should be sorted by date when calculating functions on datetime periods.

The range can also be used for non-datetime series:

[C#] 
for(int i = 0; i < 60; i)
     bar1.Add(Convert.ToDouble(i), rnd.Next(100),"",UIColor.Red.CGColor);

average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First;
average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range;
average1.Period = 6;

This will calculate an average for each group of points inside every "6" interval.
(Points with X >=6, X<6 will be used to calculate the first average, points with X >=6, X<12 will be used to calculate the second average and so on... ).
Notice this is different than calculating an average for every 6 points.

Use the Period Alignment property to align the function Points within the Series range. The following will plot the Function point at the end of a monthly Period:

[C#] 
average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First;
average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range;
average1.Period = month.TotalDays;

Period = Month.TotalDays and PeriodAligns.First
As you can see from the picture below, the "average" is plotted at the end
of the month.

Periodal1.png

Period = Month.TotalDays and PeriodAligns.Last
In this case the "average" is plotted at the beginning of the month.

Periodal2.png

Deriving Custom Functions

Creating a new Function component is simply creating a new component derived from the Function class (it also can be derived from an existing function class). There are 2 important virtual methods in Function that can be overridden to create a new Function type.

1) Function.Calculate: public virtual double Calculate(Series Source,int First,int Last)

2) Function.CalculateMany:  public virtual double CalculateMany(ArrayList SourceSeries, int ValueIndex)

The Calculate method is used to calculate the function result if only one series serves as the datasource. CalculateMany is used to calculate the function result if multiple series can be used as a datasource.

Example : Creating new SquareSum Funtion.

Let's decide we need a SquareSum Function to return the "sum of squares".  

This function can have only one datasource or multiple datasources, so we'll override the Calculate and CalculateMany methods.

[C#] 
public class SquareSum : Steema.TeeChart.Functions.Function
        public SquareSum(): base()
        public SquareSum(Steema.TeeChart.Chart c): base(c)

        public override double Calculate(Series SourceSeries,int FirstIndex,int LastIndex)   
            ValueList v=ValueList(SourceSeries);
            if ( FirstIndex==-1 ) return v.Total;
            else
                double result=0;
                for (int t=FirstIndex; t<=LastIndex; t )   
                    result =Math.Sqrt(v[t]);
                return result;
            
        

        public override double CalculateMany(ArrayList SourceSeriesList,int ValueIndex)
            ValueList v;
            double result=0;

            for (int t=0; t<SourceSeriesList.Count; t )
                v=ValueList((Series)SourceSeriesList[t]);
                if ( v.Count>ValueIndex )   
                    result =Math.Sqrt(v[ValueIndex]);
            
            return result;
        

The FirstIndex and EndIndex variables are used to "loop" all SourceSeries points to calculate the sum of squares.

The "ValueList" method is used to extract the mandatory Steema.TeeChart.ValueList to make the class work with Series types like HorizBarSeries where "XValues" holds the point values and not "YValues".

The "Calculate" method is used when the Series has only one Series as DataSource. When Series have more than one Series as datasources, the "CalculateMany" method is called.

"CalculateMany" will get called once for each point in the source Series, starting from zero and ending with the minimum point count of all datasources.  

It is very important to understand the difference between Calculate and CalculateMany. "Calculate" is called when there only one datasource and it's called only once. "CalculateMany" is called several times (one for each point) when there are more than one Series as datasources.



PREVIOUS

NEXT


© 1998-2017 Steema Software SL. All rights reserved.