I have a PHP array of objects, each object holds an associative array $key=>$value... each object holds info like:
object1: bread=>5, juice=>3
object2: bread=>3, meat=>4
this is the structure of each object:
class MyObject {
var $id = 0;
var $food = array();
public function setid($id) {
$this->id = $id;
}
public function getid() {
return $this->id;
}
public function setfood($key, $val) {
$this->food[$key] = $val;
}
public function getfood($key) {
return $this->food[$key];
}
}
I am using Chart.js to plot, and I am trying to figure out how to build a bar plot that shows, for each object I have, bars corresponding to the associative array values.
The point is that I don't know how many keys there are in the associative arrays, so it's difficult for me to understand how to prepare the datasets. If I know the number of bars before, I can do something like:
data: {
labels: somelabels,
datasets:[{
label: 'Bread',
data: breadData
},{
label: 'Meat',
data: meatData
},{
label: 'Juice',
data: juiceData
}]
}
Any hint on a solution with Chart.js?