2017-04-06 109 views
0

你好,我需要認真的幫助,因爲我試過所有的方式,沒有找到答案...所以我真的想顯示圖表,每月計數許多數據,例如1月2日數據,febuary 3數據等......請看看這個弟弟Lavachart顯示每月計數

public function lihatkeluhan(){ 
    $halaman="tindaklayanan"; 
    $keluhan_list=DB::table('keluhans') 
    ->select(DB::raw('id,tanggal,produk,username,area,masalah,status'))->get(); 

    $keluhan_group = keluhan::select(DB::raw('id,tanggal,produk,username,area,masalah,status')) 
    ->get()->groupBy(function($date) { 
    return Carbon::parse($date->tanggal)->format('m'); // grouping by months 
    }); 
    foreach ($keluhan_group as $group) { 
     $count[] = count($group); 
    } 

    $bulan = array("Jan","Feb","Mar","Apr","Mei","Jun","Jul","Agu","Sep","Okt","Nov","Des"); 
    $count = Count($keluhan_list); 

    $population = Lava::DataTable(); 
    $population->addDateColumn("Month") 
       ->addNumberColumn('Keluhan'); 
       foreach($keluhan_group as $group){ 
       $population->addRow(["jan",$count]); 
       } 

    Lava::LineChart('Population', $population, [ 
     'title' => 'Tahun : 2017', 
     'titleTextStyle' => [ 
      'color' => '#212F3C', 
      'fontSize' => 14 
     ] 
    ]); 

$ keluhan_group按月 $計數結果用於組是每個月 數據的個數,但IDK的是如何在圖表中顯示...

順便說一句$人口 - > addDateColumn(「Month」)不起作用,它不顯示月份但是年份T_T

回答

0

將代碼從$keluhan_group替換爲$population,代碼如下,並進行檢查。 在這裏你需要傳遞月份的陣列與當月算

$keluhan_group = keluhan::select(DB::raw("COUNT(*) as count , MONTHNAME(created_at) as month")) 
    ->orderBy("created_at") 
    ->groupBy(DB::raw("month(created_at)")) 
    ->get()->toArray(); 
$chart_array = array(); 
foreach($keluhan_group as $data){ 
    $n_data = []; 
    array_push($n_data,$data['month'],$data['count']); 
    array_push($chart_array,$n_data); 
} 

$population = Lava::DataTable(); 
$population->addDateColumn("Month") 
    ->addNumberColumn('Keluhan') 
    ->addRow($chart_array); 

希望這將有助於。

+0

再次問候,謝謝哥們。我發現這個錯誤btw,無效的行屬性,數組鍵值(字符串)與值[v | f | p]我應該改變什麼? –

+0

你在哪一行得到錯誤? –

+0

@Merra坦克在DataTable.php行841 idk它在哪裏,我想它是在$人口 –

0

看,我用我的「用戶」 TABEL, 我是沒有在我的項目lavachart執行圖表,所以我有使用http://itsolutionstuff.com/post/laravel-5-geo-chart-example-and-demo-using-lavacharts-packageexample.html

在這裏更新它是我的控制器代碼:

$users = Users::select(DB::raw("COUNT(*) as count , MONTHNAME(created_at) as month")) 
    ->orderBy("created_at") 
    ->groupBy(DB::raw("month(created_at)")) 
    ->get()->toArray(); 

    $chart_array = array(); 
    foreach($users as $data){ 
     $n_data = []; 
     array_push($n_data,$data['month'],$data['count']); 
     array_push($chart_array,$n_data); 
    } 
    $lava = new Lavacharts; 
    $popularity = $lava->DataTable(); 
    $popularity->addStringColumn('Country') 
       ->addNumberColumn('Popularity') 
       ->addRows($chart_array); 
    $lava->LineChart('demochart', $popularity, [ 
      'title' => 'Demo population count', 
      'animation' => [ 
      'startup' => true, 
      'easing' => 'inAndOut' 
     ], 
     'colors' => ['blue', '#F4C1D8'] 
    ]); 
return view('welcome', ['lava' => $lava]); 

和在我的.blade文件中:

<div id="temps_div"></div> 

<?= $lava->render('LineChart', 'demochart' , 'temps_div') ?> 

希望這會有所幫助。