PHP中使用GOOGCHART类进行饼状图、走势图数据统计

在后台的数据统计之中经常会使用到趋势图和数据统计图,下面就结合GOOGLE开发者上面的GoogChart类来整合了两种类型的统计图。

1、饼状图:

/** 
 * @todo 饼状图 
 * @param $title 饼状图标题 
 * @param $color 饼状图的颜色范围 
 * @param $data 饼状图展示数据 
 * @return $url 饼状图生成后的URL地址 
 * @example  
 * // Set graph data 
    $data = array( 
                ‘IE7‘ => 22, 
                ‘IE6‘ => 30.7, 
                ‘IE5‘ => 1.7, 
                ‘Firefox‘ => 36.5, 
                ‘Mozilla‘ => 1.1, 
                ‘Safari‘ => 2, 
                ‘Opera‘ => 1.4, 
            ); 
     
    // Set graph colors 
    $color = array( 
                ‘#99C754‘, 
                ‘#54C7C5‘, 
                ‘#999999‘, 
            ); 
    // Set graph title 
    $title=‘浏览器使用统计‘; 
 */  
public function pieChart($title,$color,$data){  
    $chartPath=Yii::app() -> request -> baseUrl.‘/protected/extensions/chart/‘;  
    include($chartPath.‘GoogChart.class.php‘);  
    $chart=new GoogChart();  
    $chart->setChartAttrs( array(  
        ‘type‘ => ‘pie‘,  
        ‘title‘ => $title,  
        ‘data‘ => $data,  
        ‘size‘ => array( 400, 300 ),  
        ‘color‘ => $color  
        )  
    );  
    return $chart;  
}  

2、走势图

/** 
 * @todo 走势图 
 * @param $title 走势图标题 
 * @param $color 走势图每条线的颜色范围 
 * @param $data 走势图的数据 
 * @return $url 走势图生成后的URL地址 
 * @example 
 * // Set graph data 
    $data = array(  
        ‘2007‘ => array( 
            ‘January‘ => 31.0, 
            ‘February‘ => 31.2, 
            ‘March‘ => 31.8, 
            ‘April‘ => 32.9, 
            ‘May‘ => 33.7, 
            ‘June‘ => 34.0, 
            ‘July‘ => 34.5, 
            ‘August‘ => 34.9, 
            ‘September‘ => 35.4, 
            ‘Oktober‘ => 36.0, 
            ‘November‘ => 36.3, 
            ‘December‘ => 36.3, 
            ), 
        ‘2006‘ => array( 
            ‘January‘ => 25.0, 
            ‘February‘ => 24.5, 
            ‘March‘ => 24.5, 
            ‘April‘ => 22.9, 
            ‘May‘ => 22.9, 
            ‘June‘ => 25.5, 
            ‘July‘ => 25.5, 
            ‘August‘ => 24.9, 
            ‘September‘ => 27.3, 
            ‘Oktober‘ => 27.3, 
            ‘November‘ => 29.9, 
            ‘December‘ => 29.9, 
            ), 
        ‘2005‘ => array( 
            ‘January‘ => 15.0, 
            ‘February‘ => 14.5, 
            ‘March‘ => 14.5, 
            ‘April‘ => 12.9, 
            ‘May‘ => 12.9, 
            ‘June‘ => 15.5, 
            ‘July‘ => 15.5, 
            ‘August‘ => 14.9, 
            ‘September‘ => 17.3, 
            ‘Oktober‘ => 17.3, 
            ‘November‘ => 19.9, 
            ‘December‘ => 19.9, 
            ), 
    ); 
    // Set graph colors 
    $color = array( 
                ‘#99C754‘, 
                ‘#54C7C5‘, 
                ‘#999999‘, 
            ); 
    // Set graph title 
    $title=‘浏览器使用统计‘; 
 */  
public function sparkLineChart($title, $color, $data){  
    $chartPath=Yii::app() -> request -> baseUrl.‘/protected/extensions/chart/‘;  
    include($chartPath.‘GoogChart.class.php‘);  
    $chart=new GoogChart();  
    $chart->setChartAttrs( array(  
        ‘type‘ => ‘sparkline‘,  
        ‘title‘ => $title,  
        ‘data‘ => $data,  
        ‘size‘ => array( 600, 200 ),  
        ‘color‘ => $color,  
        ‘labelsXY‘ => true,  
        ‘fill‘ => array( ‘#f0f0f0‘, ‘#cccccc‘ ),  
        )  
    );  
    return $chart;  
}  

简单的几段代码就可以实现后台数据统计。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。