我从数据库中得到了此结果:
year column1 column2
2018 11 7
2019 24 12
2020 15 13
我想将结果放入这样的数组中:
['2018','2019','2020']
[11, 24, 15 ]
[7, 12, 13]
我需要制作一个堆积的条形图,我正在使用Charts.js:
$chart01 = new BarChart;
$chart01 ->type('bar')
->labels(['2018','2019','2020']);
$chart01->dataset('Column1', 'bar', [11,24,15])
->options(['backgroundColor' => ['#58D3F7','#58D3F7','#58D3F7']]);
$chart01->dataset('Column2', 'bar', [7,12,13])
->options(['backgroundColor' => ['#5882FA','#5882FA','#5882FA']]);
$options = [];
$options['scales'] = [ 'xAxes' => [ ['stacked' => true] ], 'yAxes' => [ ['stacked' => true] ] ];
$chart01->options($options);
有什么想法吗?谢谢!
您可以使用pluck
方法实现所需的目标。
例如:
$models = App\Model::all()
$models->pluck('year')->toArray(); // [2018, 2019, 2020]
$models->pluck('column1')->toArray(); // [11, 24, 15]
$models->pluck('column2')->toArray(); // [7, 12, 13]
您可以在此处阅读有关pluck
的更多信息:https://laravel.com/docs/6.x/collections#method-pluck