翻譯|使用教程|編輯:況魚杰|2019-08-02 09:54:42.617|閱讀 410 次
概述:本教程將會介紹TeeChart for PHP這款產品中函數的使用。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TeeChart for PHP包含100%的PHP源代碼。它支持PHP5及更高的版本。它可作為一個調色板組件整合到針對PHP的Delphi編程環境中,從而讓其他人在運行時以創建組件的方式來引用。第一個版本提供17種圖表類型(2D和3D的多種組合),11個數學函數和一些圖表工具組件以擴展功能。
本教程是TeeChart for PHP教程中與系列一起工作這一節。本章節的內容主要分為以下幾個部分:
功能類型
功能特點
功能的添加
定義數據源
功能期
功能期的對齊
派生自定義功能
功能類型
功能特點
TeeChart Pro功能是一個系列,幾乎可以是任何系列類型,可以應用代數函數,也可以是另一個圖表系列的數據源。所有函數都派生自函數組件并繼承函數的Period方法。
TeeChart Pro包含以下預定義功能列表。有關所有功能類型的完整列表,請參閱TeeChart Editor Gallery和Helpfile:
功能類型 | 投入數量 | 描述 |
Add | Unlimited | Plots sum of inputs |
ADX | Unlimited | |
Average | Unlimited | The Average Function will calculate the average of every group of Period points |
Count | Unlimited | |
Divide | Unlimited | The Divide Function plots inputs divided in descending order of inclusion |
High | Unlimited | The High Function plots high point of inputs |
Low | Unlimited | The Low Function plots low point of inputs |
Multiply | Unlimited | The Multiply Function plots value of inputs multiplied |
Subtract | Unlimited | Plots value of inputs subtracted in descending order of inclusion |
Mode | 1 | The mode function returns the source series value that is repeated more times. |
Median | 1 | Calculates the median value of the source series values. |
Count | 1 | Draws horizontal line at Y position that is defined by the number of points in underlying Series. |
Subset of Pro version only functions | ||
Bollinger | 1 | The Bollinger Function uses simple or exponential moving average to constructs Bollinger Trading Bands |
Curve Fitting | 1 | Draws fitted polynomial through data inputs using the TypeFitting formula |
Exponential Average | 1 | Exponential average based on Weight |
Exponential Moving Average | 1 | Exponential moving average based on Weight |
Exponential Trend | 1 | Draws best exponential trend line through points in input series |
MACD | 1 | Moving Average Convergence Divergence |
Momentum | 1 | Each Y value is the current point's Y value less the last Period point's Y value |
Momentum Division | 1 | Each Y values is the current point's Y value divided by the last Period point's YValue, expressed in percents |
Moving Average | 1 | The Moving Average Function will calculate the simple or weighted average of every group of Period points |
Root Mean Square | Unlimited | The Root Mean Square Function plots RMS value of inputs |
Relative Strength Index | 1 | RSI Function calculates a percent value based on financial data. Depending on TRSISyle type different formula will be used to calculate RSI value |
Standard Deviation | 1 | Maps the Standard Deviation (or Complete Standard Deviation) of every group of Period points |
Stochastic | 1 | |
Trend | 1 | Draws best trend line through points of input Series |
多種函數類型僅支持一個輸入系列。但是,可以鏈接鏈接函數。例如,將圖表中多個系列的平均值創建為平均函數系列,然后使用平均函數作為趨勢函數的輸入來標識平均值的趨勢。
功能的添加
每個功能都顯示為一個系列,您可以之后更改與該功能關聯的系列類型。
假設我們從一個完全空的Chart開始,這里是代碼中構建一個簡單的Series-Function相關Chart的步驟。
private function Load() { //Add a data Series $line1 = new Line($tChart1->getChart()); //Populate it with data (here random) $line1->fillSampleValues(10); //Add a series to be used for an Average Function $line2 = new Line($tChart1->getChart()); //Define the Function Type for the new Series $average1 = new Average(); $line2->setFunction($average1); //Define the Datasource for the new Function Series $line2->setDataSource($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->getFunction()->setPeriod(2); $line2->checkDataSource(); }
我們可以添加另一個函數來告訴我們有關前一個函數的信息。
//Let's change to 2D for visibility $tChart1->getAspect()->setView3D(false); //Add another Series to be used for a 2nd Function $line3 = new Line($tChart1->getChart()); //Define the Function Type for the new Series $high1 = High(); $line3->setFunction($high1); //Define the Datasource for the new Function Series //Use the existing Function (Series2) as input $line3->setDataSource($tChart1->getSeries(1)); //Leave the Period at default 0 (No Period set) to draw //A line at Highest of all points of the Average Function
定義數據源
上面示例重點介紹了使用Datasource按代碼對函數進行內容處理。Series使用Datasource定義Function的輸入或定義Series Dataset數據源。按代碼的數據源使用Series-> Datasource屬性。
假設我們在圖表中有2個數據系列。我們添加了一個由2系列的平均值組成的函數:
require_once "../sources/TChart.php"; $tChart1 = new TChart(600,450); private function Load() { $tChart1->getAspect()->setView3D(false); $bar1->fillSampleValues(10); $bar2->fillSampleValues(10); } public function button1_click() { $line1 = new Line($tChart1->getChart()); $average3 = new Average(); $tmpDataSource = Array($bar1,$bar2); $line1->setFunction($tmpDataSource); $line1->setDataSource($bar1); $line1->getMarks()->setVisible(true); }
我們為2系列添加點數:
public function button1_click() { for($i = 0; $i < 10; ++$i) $bar1->add(rand(0,500)); $bar2->add(rand(0,500)); } }
注意:該功能不會顯示,您需要使用checkDataSource方法讀入Function的值。
$tChart1->getSeries(2)->checkDataSource();
可以在運行時更改函數定義,只需通過重新定義Series.DataSource方法將新函數分配給Series:
public function button3_click() { $cumulative1 = Cumulative(); $tChart1->getSeries(2)->setFunction($cumulative1);
功能期
Period是使用函數的重要方法,因為Period定義了循環應用Function的點的范圍。
假如:我們有6個數據點(例如Bar系列的條形圖),其值為:3,8,6,2,9和12,我們定義一個具有周期0的函數系列(默認),繪制的平均值為:6.667,將Period設置為2,我們得到3個平均值作為函數的輸出:5.5,4和10.5。這些值將在其周期范圍內集中繪制,即輸入系列的第1和第2欄之間的第1個值,第3和第4欄之間的第2個值等。
您可以使用FunctionType在運行時定義Period。
例如,系列2是功能系列:
$line1->getFunction()->setPeriod(2);
以下是2張圖表,能夠突出顯示應用期間的效果。
功能期的對齊
期間可以定義為范圍。這在使用DateTime系列時非常有用,我們希望將函數的Period表示為TimeStep。屬性PeriodStyle控制如何表達Period。
例如,您現在可以使用日期時間源系列上的正常平均功能繪制月平均銷售額功能,并將功能周期設置為一個月:
require_once "../sources/TChart.php"; $tChart1 = new TChart(600,450); //Add in a Bar Series and Average Function . $bar = new Bar($tChart1->getChart()); //Populate it with data (here random) $bar->fillSampleValues(10); //Add a series to be used for an Average Function $line2 = new Line($tChart1->getChart()); $average1 = new Average(); $line2->setFunction($average1); private function Load() { $tChart1->getAspect()->setView3D(false); $today = date_time(‘U’); $bar1->getMarks()->setVisible(false); $bar1->getXValues()->setDateTime(true); $tChart1->getAxes()->getBottom()->getLabels()->setAngle(90); for($i = 0; $i < 60; ++$i) $days7 = 7 * 86400; $today = $today + $days7; $bar1->add($today, rand(0,100),"",Color::RED()); $average1->setPeriodAlign(PeriodAligns::$FIRST); $average1->setPeriodStyle(PeriodStyles::$RANGE); $average1->setPeriod(30); $line1->setDataSource($bar1); $line1->checkDataSource(); }
這將產生幾個點,每個點顯示Bar系列中每個月數據的平均值。在計算日期時間段的函數時,源系列中的點應按日期排序。該范圍也可用于非日期時間序列:
for($i = 0; $i < 60; ++$i) $bar1->add($i, rand(0,100),"",Color::RED()); $average1->setPeriodAlign(PeriodAligns::$FIRST); $average1->setPeriodStyle(PeriodStyles::$RANGE); $average1->setPeriod(6);
這將計算每個6區間內每組點的平均值。(X> = 6,X
使用周期對齊屬性可以對齊系”范圍內的功能點。以下將繪制月度期末的功能點:
$average1->setPeriodAlign(PeriodAligns::$FIRST); $average1->setPeriodStyle(PeriodStyles::$RANGE); $average1->setPeriod(DateTime::getDaysInMonth(year,month));
Period = Month.TotalDays和PeriodAligns.First
從下圖中可以看出,平均值是在月底繪制的:
Period = Month.TotalDays和PeriodAligns.Last
在這種情況下,平均值在月初繪制:
派生自定義功能
創建一個新的Function組件只是創建一個派生自Functions類的新組件(它也可以從現有函數派生)。在TTeeFunction中有兩個重要的虛擬方法可以被覆蓋以創建新的Function類型。
Function.Calculate:public virtual double Calculate((Series)Source,(int)First,(int)Last)
Function.CalculateMany:public virtual double CalculateMany((ArrayList)SourceSeries,(int)ValueIndex)
如果只有一個系列是數據源,則Calculate方法用于計算函數結果。如果多個系列可以是數據源,則CalculateMany用于計算函數結果。
示例:創建新的SquareSum功能。
假設我們需要一個SquareSum函數來返回平方和,此函數只能有一個數據源或多個數據源,因此我們將覆蓋Calculate和CalculateMany方法。
public class quareSum extends Functions { public function quareSum($c=null) { parent::__constructor($c); } public calculate($sourceSeries, $firstIndex, $lastIndex) { $v = $this->valueList($sourceSeries); if ($firstIndex == -1) { return $v->getTotal(); } else { result = 0; for ($t = $firstIndex; $t getValue($t)); } return (Double)$result; } } public function calculateMany($sourceSeriesList, $valueIndex) { $result = 0; for ($t = 0; $t < sizeof($sourceSeriesList); $t++) { $v = $this->valueList($sourceSeriesList[$t]); if ($v->count > $valueIndex) { $result+=sqrt($v->getValue($valueIndex)); } } return $result; }
FirstIndex和EndIndex變量用于循環所有SourceSeries點以計算平方和。
ValueList方法用于提取必需的Steema.TeeChart.ValueList,以使該類適用于像HorizBarSeries這樣的Series類型,其中是XValues保存點值而不是YValues。
當Series只有一個Series作為DataSource時,使用Calculate方法。當Series有多個Series作為數據源時,將調用CalculateMany方法。
對于源系列中的每個點,CalculateMany將被調用一次,從零開始,以所有數據源的最小點數結束。
理解Calculate和CalculateMany之間的區別非常重要。當Series只有一個Series作為DataSource時,使用Calculate方法。當Series有多個Series作為數據源時,將調用CalculateMany方法(每個點一個)。
這一章節教程就是這樣了,下一節將會介紹TeeChart for PHP的縮放與滾動。
關注慧聚IT微信公眾號 ???,了解產品的最新動態及最新資訊。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: