Great extension for a very slick looking charting project.
I have a (hopefully?) quick question that I’m sure people have done. I’m still banging my head against it though, and in spite of tons of searching and reading and failed attempts, I can’t get it going.
Q: How do you get data from the database to a chart? I have a controller that returns a JSON-encoded string but I can’t get the Highchart example to use any data other than if it’s hard-coded just like the example. I’ve even created a local variable in PHP and tried to echo that to no avail.
I’ve also tried using the RequestData method on the ‘load’ event of the chart and can get data from my controller, but when I try to assign it to the widget ‘series’ it does nothing. I believe it’s because the chart itself isn’t declared as a global JavaScript variable, but I don’t see how the widget let’s me do that (in the Highcharts demo AJAX code it’s a simple assignment)
Anyone have a simple example of what the widget and controller action would look like?
Follow-up: I’ve gotten the categories part working, but can’t seem to get the series data populated. I’m trying to recreate the chart used in the wiki as an example. Thoughts on what I may be doing wrong? How do I tell the series to use the data from the controller in the same manner as I did for the categories?
Controller:
public function actionFruitCategories() {
$categories = array('Apples', 'Bananas', 'Oranges');
echo CJSON::encode($categories);
}
public function actionFruitData() {
$fruitData = array(
array('name' => 'Jane', 'data' => array(10, 0, 40)),
array('name' => 'John', 'data' => array(5, 70, 30))
);
echo CJSON::encode($fruitData);
}
I was able to get the charts working nicely from data extracted from a CActiveRecord. I was having some difficulty knowing exactly how to format the arrays used for the categories and the series, so I used the function
Print_r($arrayVariable)
to better understand the required format.
Since my data set was small (just the 12 months) and since I did not know another way, I just looped through the array of CActiveRecords and built up two arrays in a format that was required by the widget.
The widget is wrapped in a jquery function. How can I access the chart variable from a button on the page in order to change some option using the Highcharts API?
Hi Milo, i use ajax request in event click with Highchart. Its work in Chrome, but didnt work when i run in firefox and IE. What can i do for fix this problem?
Suggestion to enhance the use of the extension especially with working with the chart in javascript.
The chart creation allows for a callback in it’s creation so you could add another option besides ‘option’ say ‘callback’ that lets us specify some javascript to be called in the browser once the chart has been created.
As to someone else question a while back about getting the chart variable you set it to be ‘chart’ in javascript. It would be better if with another option you allow use to be able to set the variable we would like to have the created chart set to. This would help with multiple charts on the page and allow us to access them easier in javascript.
Here’s the suggested changes:
additional vars for the options.
public $chartCreationCallback = 'function(){}';
public $chartJSVariable = 'chart';
the line of code that needs to change to support them.