翻譯|使用教程|編輯:況魚杰|2019-08-02 09:54:42.617|閱讀 417 次
概述:本教程將會介紹TeeChart for PHP這款產(chǎn)品中函數(shù)的使用。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
TeeChart for PHP包含100%的PHP源代碼。它支持PHP5及更高的版本。它可作為一個調(diào)色板組件整合到針對PHP的Delphi編程環(huán)境中,從而讓其他人在運行時以創(chuàng)建組件的方式來引用。第一個版本提供17種圖表類型(2D和3D的多種組合),11個數(shù)學(xué)函數(shù)和一些圖表工具組件以擴展功能。
本教程是TeeChart for PHP教程中與系列一起工作這一節(jié)。本章節(jié)的內(nèi)容主要分為以下幾個部分:
功能類型
功能特點
功能的添加
定義數(shù)據(jù)源
功能期
功能期的對齊
派生自定義功能
功能類型
功能特點
TeeChart Pro功能是一個系列,幾乎可以是任何系列類型,可以應(yīng)用代數(shù)函數(shù),也可以是另一個圖表系列的數(shù)據(jù)源。所有函數(shù)都派生自函數(shù)組件并繼承函數(shù)的Period方法。
TeeChart Pro包含以下預(yù)定義功能列表。有關(guān)所有功能類型的完整列表,請參閱TeeChart Editor Gallery和Helpfile:
| 功能類型 | 投入數(shù)量 | 描述 |
| 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 |
多種函數(shù)類型僅支持一個輸入系列。但是,可以鏈接鏈接函數(shù)。例如,將圖表中多個系列的平均值創(chuàng)建為平均函數(shù)系列,然后使用平均函數(shù)作為趨勢函數(shù)的輸入來標(biāo)識平均值的趨勢。
功能的添加
每個功能都顯示為一個系列,您可以之后更改與該功能關(guān)聯(lián)的系列類型。
假設(shè)我們從一個完全空的Chart開始,這里是代碼中構(gòu)建一個簡單的Series-Function相關(guān)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();
}我們可以添加另一個函數(shù)來告訴我們有關(guān)前一個函數(shù)的信息。
//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
定義數(shù)據(jù)源
上面示例重點介紹了使用Datasource按代碼對函數(shù)進行內(nèi)容處理。Series使用Datasource定義Function的輸入或定義Series Dataset數(shù)據(jù)源。按代碼的數(shù)據(jù)源使用Series-> Datasource屬性。
假設(shè)我們在圖表中有2個數(shù)據(jù)系列。我們添加了一個由2系列的平均值組成的函數(shù):
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);
}我們?yōu)?系列添加點數(shù):
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();
可以在運行時更改函數(shù)定義,只需通過重新定義Series.DataSource方法將新函數(shù)分配給Series:
public function button3_click() {
$cumulative1 = Cumulative();
$tChart1->getSeries(2)->setFunction($cumulative1);功能期
Period是使用函數(shù)的重要方法,因為Period定義了循環(huán)應(yīng)用Function的點的范圍。
假如:我們有6個數(shù)據(jù)點(例如Bar系列的條形圖),其值為:3,8,6,2,9和12,我們定義一個具有周期0的函數(shù)系列(默認),繪制的平均值為:6.667,將Period設(shè)置為2,我們得到3個平均值作為函數(shù)的輸出:5.5,4和10.5。這些值將在其周期范圍內(nèi)集中繪制,即輸入系列的第1和第2欄之間的第1個值,第3和第4欄之間的第2個值等。
您可以使用FunctionType在運行時定義Period。
例如,系列2是功能系列:
$line1->getFunction()->setPeriod(2);
以下是2張圖表,能夠突出顯示應(yīng)用期間的效果。

功能期的對齊
期間可以定義為范圍。這在使用DateTime系列時非常有用,我們希望將函數(shù)的Period表示為TimeStep。屬性PeriodStyle控制如何表達Period。
例如,您現(xiàn)在可以使用日期時間源系列上的正常平均功能繪制月平均銷售額功能,并將功能周期設(shè)置為一個月:
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();
}這將產(chǎn)生幾個點,每個點顯示Bar系列中每個月數(shù)據(jù)的平均值。在計算日期時間段的函數(shù)時,源系列中的點應(yīng)按日期排序。該范圍也可用于非日期時間序列:
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區(qū)間內(nèi)每組點的平均值。(X> = 6,X
使用周期對齊屬性可以對齊系”范圍內(nèi)的功能點。以下將繪制月度期末的功能點:
$average1->setPeriodAlign(PeriodAligns::$FIRST); $average1->setPeriodStyle(PeriodStyles::$RANGE); $average1->setPeriod(DateTime::getDaysInMonth(year,month));
Period = Month.TotalDays和PeriodAligns.First
從下圖中可以看出,平均值是在月底繪制的:

Period = Month.TotalDays和PeriodAligns.Last
在這種情況下,平均值在月初繪制:

派生自定義功能
創(chuàng)建一個新的Function組件只是創(chuàng)建一個派生自Functions類的新組件(它也可以從現(xiàn)有函數(shù)派生)。在TTeeFunction中有兩個重要的虛擬方法可以被覆蓋以創(chuàng)建新的Function類型。
Function.Calculate:public virtual double Calculate((Series)Source,(int)First,(int)Last)
Function.CalculateMany:public virtual double CalculateMany((ArrayList)SourceSeries,(int)ValueIndex)
如果只有一個系列是數(shù)據(jù)源,則Calculate方法用于計算函數(shù)結(jié)果。如果多個系列可以是數(shù)據(jù)源,則CalculateMany用于計算函數(shù)結(jié)果。
示例:創(chuàng)建新的SquareSum功能。
假設(shè)我們需要一個SquareSum函數(shù)來返回平方和,此函數(shù)只能有一個數(shù)據(jù)源或多個數(shù)據(jù)源,因此我們將覆蓋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變量用于循環(huán)所有SourceSeries點以計算平方和。
ValueList方法用于提取必需的Steema.TeeChart.ValueList,以使該類適用于像HorizBarSeries這樣的Series類型,其中是XValues保存點值而不是YValues。
當(dāng)Series只有一個Series作為DataSource時,使用Calculate方法。當(dāng)Series有多個Series作為數(shù)據(jù)源時,將調(diào)用CalculateMany方法。
對于源系列中的每個點,CalculateMany將被調(diào)用一次,從零開始,以所有數(shù)據(jù)源的最小點數(shù)結(jié)束。
理解Calculate和CalculateMany之間的區(qū)別非常重要。當(dāng)Series只有一個Series作為DataSource時,使用Calculate方法。當(dāng)Series有多個Series作為數(shù)據(jù)源時,將調(diào)用CalculateMany方法(每個點一個)。
這一章節(jié)教程就是這樣了,下一節(jié)將會介紹TeeChart for PHP的縮放與滾動。
關(guān)注慧聚IT微信公眾號 ???,了解產(chǎn)品的最新動態(tài)及最新資訊。

本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: