2015-04-22 17 views
2

我想創建一個帶有highcharts插件的圖表cakephp,問題是我無法從數據庫獲取數據,因爲我不知道如何實現它。我使用谷歌搜索,但我找不到任何教程。如何從數據庫中獲取數據到cakephp中的Highcharts插件

所以我想每個月companyjumbuy數據顯示到圖表。

這是從數據庫查詢結果:

array(
(int) 0 => array(
    'B' => array(
     'company' => 'KC ACEH' 
    ), 
    'User' => array(
     'company' => 'KCP ACEH DARUSSALAM' 
    ), 
    (int) 0 => array(
     'date_part' => '3', 
     'jumlah' => null, 
     'jumbuy' => '50990', 
     'admin' => '50010' 
    ), 
    (int) 1 => array(
     'date_part' => '4', 
     'jumlah' => null, 
     'jumbuy' => '98990', 
     'admin' => '2010' 
    ) 
), 
(int) 1 => array(
    'B' => array(
     'company' => 'KC LANGSA' 
    ), 
    'User' => array(
     'company' => 'KCP ACEH ULEE KARENG' 
    ), 
    (int) 0 => array(
     'date_part' => '3', 
     'jumlah' => null, 
     'jumbuy' => '65000', 
     'admin' => '5000' 
    ), 
    (int) 1 => array(
     'date_part' => '4', 
     'jumlah' => null, 
     'jumbuy' => '10000', 
     'admin' => '8000' 
    ) 
) 
) 

這是控制器:

public function graph_month(){ 
$this->Transaction->recursive = -1; 
    $report_posts = $this->Transaction->find('all', array(
     'conditions' => array_merge($conditions,array('Transaction.product_id=100')), 
     'joins' => array(
      array(
       'table' => 'users', 
       'alias' => 'User', 
       'type' => 'LEFT OUTER', 
       'conditions' => array('User.id = Transaction.user_id') 
      ), 
      array(
       'table' => 'users', 
       'alias' => 'B', 
       'type' => 'FULL OUTER', 
       'conditions' => array('B.id = User.bank') 
      ), 
      array(
       'table' => 'inboxes', 
       'alias' => 'Inbox', 
       'type' => 'LEFT', 
       'conditions' => array('Inbox.id = Transaction.inbox_id') 
      ), 
      array(
       'table' => 'pln_postpaids', 
       'alias' => 'PLNPostpaid', 
       'type' => 'LEFT', 
       'conditions' => array('Inbox.id = PLNPostpaid.inbox_id') 
      ) 
     ), 
     'fields' => array(
      'B.company', 
      'User.company', 
      'extract(month from "Transaction"."create_date")', 
      'sum(cast("PLNPostpaid"."jumlahrek" as integer)) as jumlah', 
      'sum(Transaction.price_buy) as jumbuy', 
      'sum(("Transaction"."price_sell")-("Transaction"."price_buy")) as admin' 
     ), 
     'group' => array('extract(month from "Transaction"."create_date")','B.company', 'User.company') 
    )); 
    $this->set('report_posts',$report_posts); 

    $chartName = 'Summary Transaksi'; 
    $mychart = $this->HighCharts->create($chartName, 'line'); 
    $this->HighCharts->setChartParams($chartName, array(
      'renderTo' => 'linewrapper', // div to display chart inside 
      'chartWidth' => 800, 
      'chartHeight' => 600, 
      'chartMarginTop' => 60, 
      'chartMarginLeft' => 90, 
      'chartMarginRight' => 30, 
      'chartMarginBottom' => 110, 
      'chartSpacingRight' => 10, 
      'chartSpacingBottom' => 15, 
      'chartSpacingLeft' => 0, 
      'chartAlignTicks' => FALSE, 
      'chartBackgroundColorLinearGradient' => array(0, 0, 0, 300), 
      'chartBackgroundColorStops' => array(array(0, 'rgb(217, 217, 217)'), array(1, 'rgb(255, 255, 255)')), 
      'title' => 'Summary Transaksi Jumlah Lembar Per Bulan', 
      'titleAlign' => 'center', 
      'titleFloating' => TRUE, 
      'titleStyleFont' => '18px Metrophobic, Arial, sans-serif', 
      'titleStyleColor' => '#0099ff', 
      'titleX' => 20, 
      'titleY' => 20, 
      'legendEnabled' => TRUE, 
      'legendLayout' => 'horizontal', 
      'legendAlign' => 'center', 
      'legendVerticalAlign ' => 'bottom', 
      'legendItemStyle' => array('color' => '#222'), 
      'legendBackgroundColorLinearGradient' => array(0, 0, 0, 25), 
      'legendBackgroundColorStops' => array(array(0, 'rgb(217, 217, 217)'), array(1, 'rgb(255, 255, 255)')), 
      'tooltipEnabled' => FALSE, 
      'xAxisLabelsEnabled' => TRUE, 
      'xAxisLabelsAlign' => 'center', 
      'xAxisLabelsStep' => 1, 
      'xAxislabelsX' => 5, 
      'xAxisLabelsY' => 20, 
      'xAxisCategories' => array('Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Ags', 'Sep', 'Okt', 'Nov', 'Des'), 
      'yAxisTitleText' => 'Position', 
      'enableAutoStep' => FALSE 
     ) 
    ); 

    $data = array(); 
    foreach($report_posts as $values) { 
     if(!isset($data[$values['B']['company']])) { 
      // initialize group 
      $data[$values['B']['company']] = $values; 

     } else { 

      $last_element = array_pop($values); // get last element 
      $data[$values['B']['company']][] = $last_element; // push 
     } 


    } 
    $data = array_values($data); 

    $Data= array(10,20,40,50,60,90,100); 
    $series = $this->HighCharts->addChartSeries(); 
    $series->addName("Tokyo") 
     ->addData($Data); 
    $mychart->addSeries($series); 

    $this->set(compact('chartName')); 
} 
+0

您是否在獲取數據或將數據傳遞給highchart時遇到問題? 您可以通過調試在視圖中顯示數據而不使用highcharts? 通常情況下,該解決方案有兩個步驟:以正確的格式獲取正確的數據,然後將其傳遞給highcharts。你卡在哪裏? –

+0

你在哪裏放置你的插件?你有跟隨的教程嗎? –

+0

您正在使用哪個版本的CakePHP? –

回答

0

default highchart

這是highchart的默認和未經編輯

0

我剛剛檢查了我成功使用的一箇舊項目雖然它使用Cake 1.x,但可能我的例子可以幫助你。

我的供應商文件夾中有兩個文件夾。 highcharts文件夾本身和另一個文件夾highchartsphp。這有助於您將Highcharts庫與您的php日期連接起來。

我做什麼:

在控制器我收集我的數據,並把它傳遞給視圖:

function budgetplanhc($category_id,$anfang,$ende,$referrer) 
{ 
    $this->Session->write('returnTo', $referrer); 
    $this->layout='chart'; 
    $this->budgetplangrafik($category_id,$anfang,$ende, true); // Here I gather the data 
    $category = $this->category ; 
    $sum = $this->sum ; 
    $diff = $this->diff ; 
    $budget = $this->budget ; 
    $labels = $this->labels ; 
    $anfang = $this->anfang ; 
    $ende = $this->ende ; 
    $avg = $this->avg ; 
    $this->set(compact('category','sum','diff','budget','labels','anfang','ende', 'avg')); 
} 

只有在我使用Highcharts庫視圖本身。或者確切的說:在Highchartsphp庫,它看起來像這樣:

<?php 
$returnTo = $session->read('returnTo'); 


App::import('Vendor', 'Highchart', array('file' => 'highchartsphp'.DS.'Highchart.php')); 
App::import('Vendor', 'HighchartJsExpr', array('file' => 'highchartsphp'.DS.'HighchartJsExpr.php')); 
$chart = new Highchart(); 

$chart->chart = array('renderTo' => 'container', 'type' => 'line', 
         'marginRight' => 130, 'marginBottom' => 35); 

$chart->chart->type = 'spline'; 
$chart->title = array('text' => 'Budgeteinhaltung für Kategorie '.$category['Category']['name'], 'x' => -20); 
$chart->subtitle = array('text' => '', 'x' => -20); 

$chart->xAxis->categories = $labels; 

$chart->yAxis = array('title'  => array('text' => 'Euro €'), 
         'plotLines' => array(array('value' => 0, 
               'width' => 1, 
               'color' => '#808080'))); 

$chart->tooltip->crosshairs = 1; 
$chart->tooltip->shared = 1; 
$chart->plotOptions->spline->marker->radius = 4; 
$chart->plotOptions->spline->marker->lineColor = "#666666"; 
$chart->plotOptions->spline->marker->lineWidth = 1; 

$chart->legend = array('layout'  => 'vertical', 'align' => 'right', 
         'verticalAlign' => 'top', 'x' => -10, 
         'y'    => 100, 'borderWidth' => 0); 

$chart->series[] = array('name' => 'Ausgaben pro Monat', 
         'data' => $sum); 
$chart->series[] = array('name' => 'Planungsvorgabe', 
         'data' => $budget); 
$chart->series[] = array('name' => 'Durchschnitt', 
         'data' => $avg); 

$chart->tooltip->formatter = new HighchartJsExpr("function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y +'€';}"); 

foreach ($chart->getScripts() as $script) { 
    echo '<script type="text/javascript" src="'.$script.'"></script>'; 
} 
?> 
<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div> 
<script type="text/javascript"> 
    <?php 
     echo $chart->render("chart"); 
    ?> 
</script> 
<br> 
<?php 
echo $this->Html->link($this->Html->image('arrow_left.png', array('border' => '0')), 
    '/categories/'.$returnTo.'/', array('escape' => false)); 

>

這可能不是最好的解決辦法還是最現代化的解決方案,但它會產生很好的圖表?

我希望這個例子能幫助你解決你的問題。

+0

數據已經輸出,但僅用於用戶部分,我正在嘗試您的腳本。但它沒有效果,然後我嘗試使用我的腳本,然後成功。問題是我不能顯示數據jumbuy。你可以幫我嗎? –

+0

您試圖在一張圖中顯示兩行?但只有一行顯示? THis在一張圖中爲我創建了trhe行: $ chart-> series [] = array('name'=>'Ausgaben pro Monat', 'data'=> $ sum); $ chart-> series [] = array('name'=>'Planungsvorgabe', 'data'=> $ budget); $ chart-> series [] = array('name'=>'Durchschnitt', 'data'=> $ avg); –

0
$data = array(); 
    foreach($report_posts as $values) { 
     if(!isset($data[$values['User']['company']])) { 
      $data[$values['User']['company']] = $values; 

      $data1 = array(OUTPUT JUMBUY??); 
      $series = $this->HighCharts->addChartSeries(); 
      $series->addName($values['User']['company'])->addData($data1); 
      $mychart->addSeries($series); 

     } else { 
      $last_element = array_pop($values); 
      $data[$values['User']['company']][] = $last_element; 

     } 

    } 
相關問題