By yangke on
I'm trying to display a flot graph based on json data (so i can refresh the graph without refreshing the entire page)
flot module: http://drupal.org/project/flot
Here's my js file:
var Flot={};
var data;
if(Drupal.jsEnabled) {
$(document).ready(
function(){ // do all this once the document is ready
data = Flot.fetch();
$.plot($("#placeholder"), [data]); // no graph displayed, replacing data with
// [[0, 3], [4, 8], [8, 5]] works
}
);
// ------------------------------------------------------------------------
// THE FUNCTIONS
Flot.prepare_data = function(fetched) {
// do some additional things
$("#flot-data").text('The data: ' + fetched + ' has been fetched'); // this works, so fetched is a string object!
return fetched;
}
////////////////
Flot.fetch = function() {
//The $.get() function takes two arguments: the URL that it should contact and a function to call when it is done communicating with the server.
$.get(Drupal.settings.flot.json_url, function(data) { //data is the data received from the server
myData = data;
if(!myData.status || myData.status == 0) {
Flot.prepare_data(myData);
}
}
); // End inline function
}
}
How can i turn the string object (fetched/data) back into an array object like [[0, 3], [4, 8], [8, 5]] ?
Comments
HELP needed
I get a string like this: '[[0, 3], [4, 8], [8, 5]]' , how can i parse this to an array like this: [[0, 3], [4, 8], [8, 5]] ?
json is created on the server with this php:
This gives a json output like this:
[[0,3],[4,8],[8,5],[9,13],[12,45]]In my .js file i have
myData is a string:
0,3,4,8,8,5,9,13,12,45, not a javascript array.How can i solve this? How to get a php array to a javascript array using JSON in drupal?
getJSON
Why don't you just use jQuery's getJSON method? http://docs.jquery.com/Ajax/jQuery.getJSON
i tried with jquery 's
i tried with jquery 's method:
But this gives the same result. It parses to a string:
0,3,4,8,8,5,9,13,12,45, not a javascript array.not a javascript array.
Maybe i need to change the JSON format?
any suggestions?
SOLVED
The problem wasn't in the format. I simplified some things (putting code inside functions, ...) and now it's working.
Next, polling data from the database.
I'll post an article once i've got it all working.
refresh a flot graph every x seconds
refreshing every 10 seconds
Nice, thanks for the feedback
Nice, thanks for the feedback and code examples.
SOLVED: internet explorer
The graph isn't showing in internet explorer. Anybody knows how to solve this?
You need to include the /flot/excanvas.min.js to get graphs working in IE7 (i only tested FF3.5 and IE7)
I do this in the theme function of my module like this:
and then it works again.
------------------------------------------
next issue: I can't seem to get zooming to work on either IE or FF (the graphs shows, but i can select a region)
Zooming
For zooming you need to add
Where to get live_flot module ?
in your example
module_path = drupal_get_path('module', 'live_flot');
Please let's me know where to get this module. i need to make live chart from flot.
live_flot is the module for the theme function
I believe that live_flot is the module he created to hold the themin function he io presented.
It looks specific to his website.