I wanna combine data array and series array to another dataset like source array,
in Laravel,
to draw charts, data is the axis, series are the item contents,
I wanna reassemble these data so that they fit the data type of ECharts
https://echarts.apache.org/en/tutorial.html#Dataset, the ref is here,
can you show how to convert it,thanks
data: ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'] series: [ { name: '2015', data: [89.3, 92.1, 94.4, 85.4] }, { name: '2016', data: [95.8, 89.4, 91.2, 76.9] }, { name: '2017', data: [97.7, 83.1, 92.5, 78.1] } ] <!-- result --> source: [ ['name', '2015', '2016', '2017'], ['Mha Latte', 89.3, 95.8, 97.7], ['Milk Tea', 92.1, 89.4, 83.1], ['Cheese Cocoa', 94.4, 91.2, 92.5], ['Walnut Brownie', 85.4, 76.9, 78.1] ]
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
$data = ['Matcha Latte', 'Milk Tea', 'Cheese Cocoa', 'Walnut Brownie'];
$series = [
[
'name' => '2015',
'data' => [89.3, 92.1, 94.4, 85.4]
],
[
'name' => '2016',
'data' => [95.8, 89.4, 91.2, 76.9]
],
[
'name' => '2017',
'data' => [97.7, 83.1, 92.5, 78.1]
],
];
$first = array_merge(['name'], array_map(function ($a) {
return $a['name'];
}, $series));
$other = array_map(function ($a, $index) use ($series) {
return array_merge([$a], array_map(function ($b) use ($index) {
return $b['data'][$index];
}, $series));
}, $data, array_keys($data));
$result = array_merge([$first], $other);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0