原创|使用教程|编辑:王香|2018-08-06 14:57:31.000|阅读 949 次
概述:本文详细介绍了如何在TeeChart .NET使用函数
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
【下载TeeChart.Net最新版本】
TeeChart Pro功能是一个系列,几乎可以是任何系列类型,应用代数功能,数据源是另一个图表系列。所有函数都派生自Steema.TeeChart.Functions命名空间中的Function类,并继承Function的 Period属性。TeeChart Pro提供以下预定义功能列表:

多种功能类型仅支持一个输入系列。但是,可以链接链接函数,例如,将图表中多个系列的平均值创建为平均函数系列,然后使用平均函数作为趋势函数的输入来标识平均值的趋势。
使用TeeChart Editor,在First Chart页面上,选择Add按钮,就像将新系列添加到Chart中一样。在TeeChart Gallery中,选择Functions选项卡以选择所需的功能。每个功能都显示为一个系列,用户可以稍后通过选择第一个图表页面上的更改按钮来更改与该功能关联的系列类型。之后,在函数系列的“数据源”页面上可以轻松更改函数定义。在这里,同样容易,用户可以将已添加到Chart的正常Series的定义更改为Function的定义(Function实际上是数据源的定义,而不是Series Type的定义)。下图显示了编辑函数时的“数据源”页面。线系列(标题“line2”)已定义。数据源页面底部的左侧列表框显示了可供输入的图表中的其他系列(此处为“line1”)。

从一个完全空的Chart开始,这里是代码中用于构建简单的Series-Function相关Chart的步骤。
[C#.Net]
private void Form1_Load(object sender, System.EventArgs e)
//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();
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Add a data Series
Dim Line1 As 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
Dim Line2 As New Steema.TeeChart.LineSeries(TChart1.Chart)
'Define the Function Type for the new Series
Dim Average1 As 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()
End Sub
添加另一个函数来获取有关前一个函数的信息
[C#.Net]
private void button1_Click(object sender, System.EventArgs e)
//Let's 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
[VB.Net]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Let's change to 2D for visibility
TChart1.Aspect.View3D = False
'Add another Series to be used for a 2nd Function
Dim Line3 As New Steema.TeeChart.LineSeries(TChart1.Chart)
'Define the Function Type for the new Series
Dim High1 As 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
End Sub
上一节中的示例重点介绍如何使用Datasource通过代码填充Function。Series使用datasource定义Function的输入或定义Series ADO.NET数据源。使用TeeChart编辑器,在添加函数后,函数系列的“Datasource”页面将显示包含在函数定义中的可用系列列表。可以更改要应用于系列的函数类型,并从左侧列表框“Available”中选择系列,并将它们添加到右侧列表框“Selected”,按代码的数据源使用Series.Datasource属性。
例;
假设在设计时通过TeeChart编辑器添加了2个数据系列,添加了一个由2系列的平均值组成的函数:
[C#.Net]
private void Form1_Load(object sender, System.EventArgs e)
tChart1.Aspect.View3D = false;
bar1.FillSampleValues(10);
bar2.FillSampleValues(10);
private void button1_Click(object sender, System.EventArgs e)
Steema.TeeChart.Styles.Line line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
Steema.TeeChart.Functions.Average average1 = new Steema.TeeChart.Functions.Average();
line1.DataSource = new object[] this.bar2,this.bar1;
line1.Function = average1;
line1.Marks.Visible = true;
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TChart1.Aspect.View3D = False
Bar1.FillSampleValues(10)
Bar2.FillSampleValues(10)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DataSource As New ArrayList()
DataSource.Add(Bar1)
DataSource.Add(Bar2)
Dim Line1 As New Steema.TeeChart.Styles.Line(TChart1.Chart)
Dim Average1 As New Steema.TeeChart.Functions.Average()
Line1.Function = Average1
Line1.DataSource = DataSource
End Sub
为2系列添加点数:
[C#.Net]
private void button2_Click(object sender, System.EventArgs e)
Random rnd = new Random();
for(int i = 0; i < 10; ++i)
bar1.Add(rnd.Next(500));
bar2.Add(rnd.Next(500));
[VB.Net]
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim rnd As New Random()
Dim i As Integer
For i = 0 To 10
Bar1.Add(rnd.Next(500))
Bar2.Add(rnd.Next(500))
Next
End Sub
请注意,该功能不会显示,需要在Button2_Click()事件中添加Series.CheckDatasource方法以读取Function的值。
[C#.Net]
tChart1.Series [2] .CheckDataSource();
[VB.Net]
TChart1.Series(2).CheckDataSource()
可以在运行时更改函数定义,只需重新定义Series.DataSource属性即可为系列分配新函数:
[C#.Net]
private void button3_Click(object sender, System.EventArgs e)
Steema.TeeChart.Functions.Cumulative cumulative1 = new Steema.TeeChart.Functions.Cumulative();
tChart1.Series[2].Function = cumulative1;
[VB.Net]
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim Cumulative1 As New Steema.TeeChart.Functions.Cumulative()
TChart1.Series(2).Function = Cumulative1
End Sub
周期是使用函数的重要属性,因为周期定义了循环应用函数的点的范围。
示例
有6个数据点(例如条形系列的条形),其值为: 3,8,6,2,9和12。定义一个具有周期0的函数系列(默认),绘制的平均值为:6.667。将周期设置为2我们得到3个平均值作为函数的输出:5.5,4和10.5。这些值将在它们的周期范围中集中绘制,即输入系列的第1和第2条之间的第1个值,第3和第4个条之间的第2个值等..通过在“Datasource”页面中选择相关的“Series and Function”并单击“Options”选项卡来定义“Period ”,也可以使用“FunctionType”在运行时修改“Period ”。
例如,line1是函数系列:
[C#.Net]
line1.Function.Period = 2;
[VB.Net]
Line1.Function.Period = 2
下面是2个图表突出显示应用的周期

周期的效果可以定义为范围,这在使用DateTime系列时非常有用,将函数的“Period”表示为TimeStep,属性“PeriodStyle”控制如何表达“Period”。例如,可以使用日期时间源系列上的常规“Average”功能绘制“monthly average of sales”功能,并将功能期间设置为“one month”:
[C#.Net]
private void Form1_Load(object sender, System.EventArgs e)
//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),"",Color.Red);
average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First;
average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range;
average1.Period = month.TotalDays;
line1.DataSource = bar1;
line1.CheckDataSource();
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Add in a BarSeries and Average Function at design-time.
TChart1.Aspect.View3D = False
Dim Month As New TimeSpan(30, 0, 0, 0)
Dim Today As DateTime = DateTime.Today
Dim i As Integer
Bar1.Marks.Visible = False
Bar1.XValues.DateTime = True
TChart1.Axes.Bottom.Labels.Angle = 90
For i = 0 To 60
Today = Today.AddDays(5)
Bar1.Add(Today, Rnd() * 100, "", Color.Red)
Next
Average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First
Average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range
Average1.Period = Month.TotalDays
Line1.DataSource = Bar1
Line1.CheckDataSource()
End Sub
这将产生几个点,每个点显示BarSeries中每个月数据的“average”,在计算日期时间段的函数时,必须按源日期对源系列中的点进行排序,该范围也可用于非日期时间序列:
[C#.Net]
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;
[VB.Net]
For i = 0 To 60 Bar1.Add(i, Rnd() * 100, "", Color.Red) Next Average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First Average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range Average1.Period = 6
这将计算每个“6”区间内每组点的平均值。(X > = 6,X < 6的点将用于计算第一个平均值,X> = 6的点,X < 12将用于计算第二个平均值,依此类推......),这与计算每6个点的平均值不同。使用“周期对齐”属性可以对齐“系列”范围内的功能点,以下将绘制每月结束时的功能点:
[C#.Net]
average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First; average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range; average1.Period = month.TotalDays;
[VB.Net]
Average1.PeriodAlign = Steema.TeeChart.Functions.PeriodAligns.First Average1.PeriodStyle = Steema.TeeChart.Functions.PeriodStyles.Range Average1.Period = Month.TotalDays
Period = Month.TotalDays and PeriodAligns.First,从下图中可以看出,“average”是在月末绘制的。

Period = Month.TotalDays and PeriodAligns.Last,在这种情况下,“average”是在月初绘制的。

创建新的Function组件只是创建一个派生自Function类的新组件(它也可以从现有的函数类派生),Function中有两个重要的虚拟方法可以重写以创建新的Function类型。 1)Function.Calculate:public virtual double Calculate(Series Source,int First,int Last) 2)Fu??nction.CalculateMany:public virtual double CalculateMany(ArrayList SourceSeries,int ValueIndex) 如果只有一个系列用作数据源,则Calculate方法用于计算函数结果。如果多个系列可用作数据源,则CalculateMany用于计算函数结果。
示例:创建新的SquareSum功能,需要一个SquareSum函数来返回“sum of squares(平方和)”,此函数只能有一个数据源或多个数据源,因此我们将覆盖Calculate和CalculateMany方法。
[C#.Net]
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; tValueIndex )
result+=Math.Sqrt(v[ValueIndex]);
return result;
[VB.Net]
Public Class SquareSum
Inherits Steema.TeeChart.Functions.Function
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal c As Steema.TeeChart.Chart)
MyBase.New(c)
End Sub
Public Overrides Function Calculate(ByVal Source As Steema.TeeChart.Series, ByVal First As Integer, ByVal Last As Integer) As Double
Dim v As Steema.TeeChart.ValueList
Dim t As Integer
v = ValueList(Source)
If First = -1 Then
Return v.Total
Else
Dim Result As Double = 0
For t = First To t < Last
Result += Math.Sqrt(v(t))
Next
Return Result
End If
End Function
Public Overrides Function CalculateMany(ByVal SourceSeries As System.Collections.ArrayList, ByVal ValueIndex As Integer) As Double
Dim v As Steema.TeeChart.ValueList
Dim Result As Double = 0
Dim t As Integer
For t = 0 To t < SourceSeries.Count
v = ValueList(CType(SourceSeries(t), Steema.TeeChart.Series))
If v.Count > ValueIndex Then
Result += Math.Sqrt(v(ValueIndex))
End If
Next
Return Result
End Function
End Class
FirstIndex和EndIndex变量用于“loop(循环)”所有SourceSeries点以计算平方和。“ValueList”方法用于提取必需的Steema.TeeChart.ValueList,以使这些类与HorizBarSeries等Series类型一起使用,其中“XValues”保存点值而不是“YValues”,当Series只有一个Series作为DataSource时,使用“Calculate”方法,当Series有多个Series作为数据源时,将调用“CalculateMany”方法。对于源系列中的每个点,“CalculateMany”将被调用一次,从零开始,以所有数据源的最小点数结束。理解Calculate和CalculateMany之间的区别非常重要,当只有一个数据源并且只调用一次时调用“Calculate”。当有多个Series作为数据源时,会多次调用“CalculateMany”(每个点一个)。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn