我想将我的计数引入此GoogleChart。我想计算新闻/观点/事件等表格中的所有ID。我希望能够记录新闻/观点/事件等中所有记录的编号。如果在“新闻”表中我有10条新闻,我想对它们进行计数并将其介绍给['News','All news HERE'],
第二,我想将其添加到以下代码中:
<script>
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['News', 'All news HERE'],
['Events', No of events from db],
]);
var options = {
title: 'My Daily Activities',
pieHole: 0.4,
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
}
</script>
答案 0 :(得分:1)
您只需查询数据库即可获取所需的所有计数(如果您不查询很多表,那应该很快)。
public function myControllerAction() {
$dbData = DB::selectOne('
select
(select count(*) from news) as News,
(select count(*) from events) as Events,
(select count(*) from someothertable) as Someotherdata
');
return view('my-view', compact('dbData'));
}
此后,您可以使用 $ dbData-> News,$ dbData-> Events,$ dbData-> Someotherdata ,并将其放在代码中您喜欢的任何位置。
您甚至可以通过使用Laravel的 Collection 并为Google图表生成随时可用的数组来简化未来的开发:
public function myControllerAction() {
$dbData = DB::selectOne('
select
(select count(*) from news) as News,
(select count(*) from events) as Events,
(select count(*) from someothertable) as Someotherdata
');
$collection = new \Illuminate\Support\Collection($dbData);
$data = $collection->flatMap(function($value, $key) {
// We will use name of the key as the label for chart (News, Events, Someotherdata)
return [[$key, $value]];
})->toArray();
return view('my-view', compact('data'));
}
只需使用Blade的 @json 指令将json呈现在您需要的位置:
<script>
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
// Here is the changed part, notice the @json($data) here
var data = google.visualization.arrayToDataTable(@json($data));
var options = {
title: 'My Daily Activities',
pieHole: 0.4,
};
var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
chart.draw(data, options);
}
</script>
答案 1 :(得分:0)
我真的无法完全理解您要做什么,但我将向您展示整个概念
在web.php
路由文件中:
Route::get('/newsCount', 'NewsController@count');
在NewsController.php
控制器文件中:
public function count()
{
$CountOfNews = News::all()->count(); // Get count of all your news
// Now you can use one of these two ways
// Pass data to view way 1
return View::make("user/NewsCount", compact('CountOfNews'));
// Pass data to view way 2
return View::make("user/NewsCount")->with(array('Count'=>$CountOfNews));
}
并在balde.php
视图文件中,您应该这样:
<script>
alert({{$CountOfNews}});
</script>