翻譯|使用教程|編輯:楊鵬連|2021-06-29 11:04:51.473|閱讀 323 次
概述:TeeChart Pro函數是一個系列,幾乎可以是任何系列類型,對其應用一個代數函數,其數據源是另一個圖表系列。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TeeChart for .NET 是優秀的 4.0 WinForm 圖表控件,官方獨家授權漢化,集功能全面、性能穩定、價格優惠等優勢。NET 的 TeeChart for .NET 中文版承諾讓您在使用和學習上沒有語言障礙,至少可以節省30%的開發時間。
功能類型
函數的特點
TeeChart Pro函數是一個系列,幾乎可以是任何系列類型,對其應用一個代數函數,其數據源是另一個圖表系列。
所有的函數都源自Steema.TeeChart.Functions命名空間中的Function類,并繼承Function的Period屬性。TeeChart Pro提供了以下預定義函數的列表。
 
 
添加一個函數
在TeeChart編輯器中,在 "第一個圖表 "頁面上,選擇 "添加 "按鈕,就像在圖表中添加一個新系列一樣。在TeeChart圖庫中選擇函數標簽,選擇你需要的函數。每個函數都是以線型系列的形式出現的,你可以通過在第一個圖表頁面上選擇 "改變 "按鈕來改變與該函數相關的系列類型。之后在函數系列的數據源頁面上可以很容易地改變函數的定義。在這里,你也可以很容易地把你添加到圖表中的普通系列的定義改為函數的定義(函數實際上是數據源的定義,而不是系列類型的定義)。
下面的圖片顯示了編輯函數時的數據源頁面。線性系列(標題為 "line2")已經定義。數據源頁面底部的左側列表框顯示了圖表中可供輸入的其他系列(這里是 "line1")。
 
 
[C#] 
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 
We can add another Function to tell us something about the previous Function 
[C#] 
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的輸入,或者定義一個系列的ADO.NET數據源(見關于訪問數據庫的教程)。
使用TeeChart編輯器,在添加一個函數后,函數系列的數據源頁面將顯示一個可用的系列列表,以納入函數定義中。在這里,你可以改變你希望應用于系列的函數類型,從左邊的列表框 "可用 "中選擇系列,并將它們添加到右邊的列表框 "選定 "中。
代碼中的數據源使用Series.Datasource屬性。
例子
假設我們在設計時通過TeeChart編輯器在圖表中添加了兩個數據系列。我們添加一個由兩個系列的平均值組成的函數。
[C#] 
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
We add points to the 2 Series: [C#] 
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 
注意,Function并沒有顯示。你需要在Button2_Click()事件中添加Series.CheckDatasource方法來為Function讀入數值。
[C#] tChart1.Series[2].CheckDataSource()。 [VB.Net] TChart1.Series(2).CheckDataSource()可以在運行時改變函數定義,只需重新定義Series.DataSource屬性就可以為Series分配一個新的函數。
[C#] 
private void button3_Click(object sender, System.EventArgs e) 
         
            Steema.TeeChart.Functions.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之間的第一個值,條形圖3和4之間的第二個值,等等。 
你可以在數據源頁面中選擇相關的系列和函數,然后點擊 "選項 "標簽來定義周期,也可以在運行時使用函數類型來修改周期。
例如:其中line1是函數系列。
[C#] line1.Function.Period = 2。 [VB.Net] Line1.Function.Period = 2下面是2張圖表,突出了應用周期的效果
 
 
 
 
	
周期樣式
Period可以被定義為一個范圍。當使用DateTime系列時,這是非常有用的,我們想把函數的 "Period "表達為一個TimeStep。屬性 "PeriodStyle "控制 "周期 "的表達方式。
例如,你現在只需在一個日期-時間源系列上使用普通的 "平均 "函數,并將函數周期設置為 "一個月",就可以繪制 "月平均銷售額 "函數。
[C#] 
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中每個月的數據的 "平均值"。
在計算日期時間段的函數時,源系列中的點應按日期排序,這是強制性的。
該范圍也可用于非日期時間系列。
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 "區間內的每一組點計算出一個平均值。使用周期對齊屬性,在系列范圍內對齊函數點。下面將把函數點繪制在每月周期的末尾。
[C#] 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.TotalDaysPeriod = Month.TotalDays and PeriodAligns.First
 
 
 
 
派生自定義函數
	創建一個新的Function組件就是簡單地創建一個派生自Function類的新組件(也可以派生自一個現有的函數類)。在Function中,有2個重要的虛擬方法可以被重寫,以創建一個新的Function類型。
1) Function.Calculate: public virtual double Calculate(Series Source,int First,int Last) 
2) Function.CalculateMany: public virtual double CalculateMany(ArrayList SourceSeries, int ValueIndex) 
如果只有一個系列作為數據源,Calculate方法用于計算函數結果。如果多個系列可以作為數據源,CalculateMany則用于計算函數結果。
示例:創建新的SquareSum函數。
讓我們決定我們需要一個SquareSum函數來返回 "平方之和"。 
這個函數可以只有一個數據源,也可以有多個數據源,所以我們將覆蓋Calculate和CalculateMany方法。 
fnnnt [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; 
         
[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變量用于 "循環 "所有SourceSeries點,以計算平方之和。
ValueList "方法用于提取強制性的Steema.TeeChart.ValueList,以使該類與HorizBarSeries類型一起工作,其中 "XValues "持有點值而不是 "YValues"。
當系列只有一個系列作為數據源時,"計算 "方法被使用。當系列有一個以上的系列作為數據源時,將調用 "CalculateMany "方法。
"CalculateMany "將為源系列中的每個點被調用一次,從零開始,以所有數據源的最小點數結束。
了解Calculate和CalculateMany之間的區別是非常重要的。當只有一個數據源時,"計算 "被調用,而且只調用一次。當有多個系列的數據源時,"CalculateMany "會被多次調用(每個點一次)。
現的TeeChart為.NET已加入在線訂購,現在搶購可立享優惠!
如果你看到了可愛的動物群,歡迎加入展示區QQ:740060302
關注慧聚IT微信公眾號???,了解產品的最新動態和最新資訊。
 
 
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: