How can I set the labels for chart_pie() with a dyanmically generate array?

How can I set the labels for chart_pie() with a dyanmically generate array?

by Tom Tom -
Number of replies: 4

The set_labels() for chart_pie() works just fine if I hard code the labels as $my_labels=['one','two','three'];

$mychart = new \core\chart_pie();

$mychart->set_labels($my_labels);


However, if I try to dynamically generate the labels with a foreach loop . . .

$my_labels = array();

foreach($something as $data){

$my_labels[] = $data->name;

}

$mychart = new \core\chart_pie();

$mychart->set_labels($my_labels);


where I build the $my_labels array . . . the chart does not show up . . . with no error. I've tried encode_json/decode_json and I've tried created a "string" that mimics the square bracketed array but I get errors saying that "set_labels()" requires an array, not a string. So it obviously knows I am using an array but just doesn't like the particular format of the array.

I need to create the labels dynamically (as well as the variables).

Any thoughts?

Average of ratings: -
In reply to Tom Tom

Re: How can I set the labels for chart_pie() with a dyanmically generate array?

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
"but I get errors saying that "set_labels()" requires an array, not a string. So it obviously knows I am using an array"
No that error means that $my_labels is a string, not an array.
Have you tried doing an echo or print_r on $my_labels, just to see what it is just before you call $mychart->set_labels()?

Also, are you sure there's something in your foreach loop? i.e. is $something, something? And do the number of labels match the data points?
In reply to Mark Sharp

Re: How can I set the labels for chart_pie() with a dyanmically generate array?

by Tom Tom -
You were so correct Mark when you said "And do the number of labels match the data points?". I had only wanted to test that the labels could be automatically generated with the foreach loop ignoring the data points until later as that is where I am in my development.

Since there was one less data point in my hard coded array (new core\chart_series('Test Usage', ['2', '5', '6', '2']);) but 5 labels the pie chart would not render.

BUT it would not throw an error either which was not helpful.

But a simple fix to a dumb mistake on my part. Thanks for the pointer that helped me fix it though.
In reply to Tom Tom

Re: How can I set the labels for chart_pie() with a dyanmically generate array?

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Perhaps you have to clean or escape your $data->name values? Make sure there are no nulls and no strings containing characters like '"\/...

What is the output of print_object($my_labels)?
In reply to Renaat Debleu

Re: How can I set the labels for chart_pie() with a dyanmically generate array?

by Tom Tom -
Thanks Renaat. The array was fine. It was just the add_series had one less than it needed after I updated the labels.