XamarinAndroidTutorial7
Contents page Previous  Next

Tutorial 7  Working with Functions
Contents
Function Types
Function characteristics
A TeeChart for Xamarin.Android 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 for Xamarin.Android offers the following list of predefined functions:
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 SeriesFunction related Chart.
[C#]
private void InitializeChart()
{
tChart1 = new Steema.TeeChart.TChart(this);
//Add a data Series
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
//Populate it with data (here random)
line1.FillSampleValues(10);
//Add a series to be used for an Average Function
Steema.TeeChart.Styles.Line line2 = new Steema.TeeChart.Styles.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();
SetContentView(tChart1);
}
We can add another Function to tell us something about the previous Function
[C#]
void button_Click(object sender, EventArgs e)
{
//Let's change to 2D for visibility
tChart1.Aspect.View3D = false;
//Add another Series to be used for a 2nd Function
Steema.TeeChart.Styles.Line line3 = new Steema.TeeChart.Styles.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
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 datetime source series and setting the function period to "one month":
[C#]
private Steema.TeeChart.TChart tChart1;
private void InitializeChart()
{
tChart1 = new Steema.TeeChart.TChart(this);
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
Steema.TeeChart.Functions.Average average1 = new Steema.TeeChart.Functions.Average();
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), "", Color.Red);
}
average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First;
average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range;
average1.Period = month.TotalDays;
line1.Function = average1;
line1.DataSource = bar1;
line1.CheckDataSource();
SetContentView(tChart1);
}
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 nondatetime series:
[C#]
for(int i = 0; i < 60; i) {
bar1.Add(Convert.ToDouble(i), rnd.Next(100),"",Color.Red);
}
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.
Period = Month.TotalDays and PeriodAligns.Last
In this case the "average" is plotted at the beginning of the month.
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(Steema.TeeChart.Styles.Series SourceSeries, int FirstIndex, int LastIndex)
{
Steema.TeeChart.Styles.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(System.Collections.ArrayList SourceSeriesList, int ValueIndex)
{
Steema.TeeChart.Styles.ValueList v;
double result = 0;
for (int t = 0; t < SourceSeriesList.Count; t )
{
v = ValueList((Steema.TeeChart.Styles.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.


© 19982019 Steema Software SL. All rights reserved.