user1335412
user1335412

Reputation: 1

Show a JSON array into a JQPLOT (Beginner)

I'm just a beginner and I want to show data from my database (MySQL) and display it in a line chart using JQPLOT. I have a PHP file that takes the data from the database and converts it into a JSON array which, as I understand, is what JQPLOT needs to display it properly. The problem I'm having is that I've never used AJAX before.

The JQPLOT website gives me this code:

$(document).ready(function(){

  var ajaxDataRenderer = function(url, plot, options) {
  var ret = null;
  $.ajax({

    async: false,
    url: url,
    dataType:"json",
    success: function(data) {
      ret = data;
    }
  });
  return ret;
 };

// The url for our json data
var jsonurl = "./jsondata.txt";


var plot2 = $.jqplot('chart2', jsonurl,{
  title: "AJAX JSON Data Renderer",
  dataRenderer: ajaxDataRenderer,
  dataRendererOptions: {
    unusedOptionalUrl: jsonurl
  }
});
});

I don't understand the second half of this code, but I want to know how I can incorporate my PHP file (that contains the json array) into this code and display a line chart. Or maybe if anyone has a simpler code where I can implement the PHP file and still able to show the line chart? Im just very new at this, please help.

Upvotes: 0

Views: 2103

Answers (1)

mprabhat
mprabhat

Reputation: 20323

The first part loads the data and the second part displays the data.

Now to explain further if you have downloaded jQPlot you will see there is one file called as jSonData.txt which contains an array, in the second part you use that array to display the chart.

You can do it differently, say in your PHP code you load the data from your database(MYSQL) and send it back as JSON, just pass this JSON retrieved to the jQPlot

the above code in that case will look like this:

var plot2 = $.jqplot('chart2', myJsonDataRetrievedFromDB,{

Upvotes: 1

Related Questions