我想創建一個帶有highcharts插件的圖表cakephp
,問題是我無法從數據庫獲取數據,因爲我不知道如何實現它。我使用谷歌搜索,但我找不到任何教程。如何從數據庫中獲取數據到cakephp中的Highcharts插件
所以我想每個月company
和jumbuy
數據顯示到圖表。
這是從數據庫查詢結果:
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'));
}
您是否在獲取數據或將數據傳遞給highchart時遇到問題? 您可以通過調試在視圖中顯示數據而不使用highcharts? 通常情況下,該解決方案有兩個步驟:以正確的格式獲取正確的數據,然後將其傳遞給highcharts。你卡在哪裏? –
你在哪裏放置你的插件?你有跟隨的教程嗎? –
您正在使用哪個版本的CakePHP? –