TeeChart for PHP教程(七):函数的使用

    TeeChart for PHP包含100%的PHP源代码。它支持PHP5及更高的版本。它可作为一个调色板组件整合到针对PHP的Delphi编程环境中,从而让其他人在运行时以创建组件的方式来引用。第一个版本提供17种图表类型(2D和3D的多种组合),11个数学函数和一些图表工具组件以扩展功能。

点击下载TeeChart for PHP试用版


    本教程是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
l i n e 1 = n e w L i n e ( line1 = new Line( line1=newLine(tChart1->getChart());

//Populate it with data (here random)
$line1->fillSampleValues(10);

//Add a series to be used for an Average Function
l i n e 2 = n e w L i n e ( line2 = new Line( line2=newLine(tChart1->getChart());

//Define the Function Type for the new Series
$average1 = new Average();
KaTeX parse error: Expected 'EOF', got '&' at position 7: line2-&̲gt;setFunction(average1);

//Define the Datasource for the new Function Series
KaTeX parse error: Expected 'EOF', got '&' at position 7: line2-&̲gt;setDataSourc…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() {
l i n e 1 = n e w L i n e ( line1 = new Line( line1=newLine(tChart1->getChart());
$average3 = new Average();
t m p D a t a S o u r c e = A r r a y ( tmpDataSource = Array( tmpDataSource=Array(bar1,$bar2);
KaTeX parse error: Expected 'EOF', got '&' at position 7: line1-&̲gt;setFunction(tmpDataSource);
KaTeX parse error: Expected 'EOF', got '&' at position 7: line1-&̲gt;setDataSourc…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张图表,能够突出显示应用期间的效果。

截图未命名.jpg

功能期的对齐

    期间可以定义为范围。这在使用DateTime系列时非常有用,我们希望将函数的Period表示为TimeStep。属性PeriodStyle控制如何表达Period。

    例如,您现在可以使用日期时间源系列上的正常平均功能绘制月平均销售额功能,并将功能周期设置为一个月:

require_once "../sources/TChart.php"; $tChart1 = new TChart(600,450); 

//Add in a Bar Series and Average Function .
b a r = n e w B a r ( bar = new Bar( bar=newBar(tChart1->getChart());
//Populate it with data (here random) $bar->fillSampleValues(10); //Add a series to be used for an Average Function l i n e 2 = n e w L i n e ( line2 = new Line( line2=newLine(tChart1->getChart()); $average1 = new Average();
KaTeX parse error: Expected 'EOF', got '&' at position 7: line2-&̲gt;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-&gt;setPeriodAlign(PeriodAligns::$FIRST); 
    $average1-&gt;setPeriodStyle(PeriodStyles::$RANGE); 
    $average1-&gt;setPeriod(6);</pre>

    这将计算每个6区间内每组点的平均值。(X> = 6,X

    使用周期对齐属性可以对齐系”范围内的功能点。以下将绘制月度期末的功能点:

$average1->setPeriodAlign(PeriodAligns::$FIRST); 
      $average1->setPeriodStyle(PeriodStyles::$RANGE); 
      $average1->setPeriod(DateTime::getDaysInMonth(year,month));

Period = Month.TotalDays和PeriodAligns.First

从下图中可以看出,平均值是在月底绘制的:

截图未命名2.jpg

Period = Month.TotalDays和PeriodAligns.Last

在这种情况下,平均值在月初绘制:

截图未命名4.jpg

派生自定义功能

    创建一个新的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-&gt;valueList($sourceSeries);
    if ($firstIndex == -1) {
        return $v-&gt;getTotal();
    } else {
        result = 0;
        for ($t = $firstIndex; $t getValue($t));
        }
        return (Double)$result;
    }
}


public function calculateMany($sourceSeriesList, $valueIndex) {
    $result = 0;

    for ($t = 0; $t &lt; sizeof($sourceSeriesList); $t++) {
        $v = $this-&gt;valueList($sourceSeriesList[$t]);
        if ($v-&gt;count &gt; $valueIndex) {
            $result+=sqrt($v-&gt;getValue($valueIndex));
        }
    }
    return $result;
}</pre>

    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的缩放与滚动。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值