У меня есть старая диаграмма jQuery, но я хочу перенести данные (эти категории-> toJson) в новую диаграмму. Я хочу изменить эти имена и числа (Африка, Азия, 2478,735 и т. Д.). Как я могу это сделать?
Вот мой старый график jQuery:
<script type="text/javascript">
window.onload = function () {
var options = {
animationEnabled: true,
title: {
text: "Number of posts : {{$countEvents}}"
},
data: [{
type: "doughnut",
innerRadius: "60%",
showInLegend: true,
legendText: "{label}",
dataPoints: {!!$categories->toJson()!!} ,
}]
};
$("#chartContainer").CanvasJSChart(options);
}
</script>
Вот мой новый график jQuery:
<script>
new Chart(document.getElementById("doughnut-chart"), {
type: 'doughnut',
data: {
labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
datasets: [
{
label: "Population (millions)",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: [2478,5267,734,784,433]
}
]
},
options: {
title: {
display: true,
text: 'Predicted world population (millions) in 2050'
}
}
});
</script>
Вот мой контроллер:
$categories = Event::join('categories', function($join) {
$join->on('events.category_id', '=', 'categories.id');
}
)
->groupBy('categories.id')
->selectRaw(
implode(',', [
'categories.category AS category',
'count(events.id) AS count'
])
)
->get()
->sortByDesc('count')->take (5)
->reduce(
function(\Illuminate\Support\Collection $carry, $item) {
$data = [
'y' => $item->count,
'label' => $item->category,
];
return $carry->push($data);
},
new \Illuminate\Support\Collection()
);