Generating data for Jquery JQplot plugin from text file

567 views Asked by At

I'm trying the get data out of a text file and generating a jqplot graph.

The problem is i cant make the plugin read the data even if i save it exactly as the demo version. Note: the demo data works well with my script, it's just something with formatting my data that i cant get a hang of.

Thanks alot

this is my script:

    $(document).ready(function() {

var chartdata = 0;
function parseData(data){
        //do something with the data
        chartdata =  '['+data+']';
       //alert("data is: " + chartdata);

      var line1=chartdata;


  var plot1 = $.jqplot('chartdiv', [line1], {
      title:'Active Users',
      axes:{
        xaxis:{
          renderer:$.jqplot.DateAxisRenderer,
          tickOptions:{
            formatString:'%b %#d'
          } 
        },
        yaxis:{
          tickOptions:{
            formatString:'$%.2f'
            }
        }
      },
      highlighter: {
        show: true,
        sizeAdjust: 7.5
      },
      cursor: {
        show: false
      }
  });

}

$.get("userschart.txt",parseData);


});

thats my code, line1 var is the problem. this is the original example code that works:

var line1=[['23-May-08', 578.55], ['20-Jun-08', 566.5], ['25-Jul-08', 480.88], ['22-Aug-08', 509.84],
['26-Sep-08', 454.13], ['24-Oct-08', 379.75], ['21-Nov-08', 303], ['26-Dec-08', 308.56],
['23-Jan-09', 299.14], ['20-Feb-09', 346.51], ['20-Mar-09', 325.99], ['24-Apr-09', 486.15]];

and this is to content of my textfile userschart.txt:

['11-Dec-13',6],['12-Dec-13',6],['13-Dec-13',6],['14-Dec-13',6],['15-Dec-13',6]
1

There are 1 answers

3
Captain John On BEST ANSWER

I see it now.

First off check that data is an array, how are you loading it? Suggest you use ajax to load the data array ensure the server returns this as a json array.

Second this line is at fault:

chartdata = '['+data+']';

This is turning chartdata into a string. You don't need it;

Simply replace this line

var plot1 = $.jqplot('chartdiv', [line1], {

with

var plot1 = $.jqplot('chartdiv', [data], {

And all should be well. If not:

1.) Check data is an array. 2.) Check that you have the array brackets around the variable.