10

I'm trying to create a simple chart in a page using mysql data retrieved using a mysql script

I don't understand how to integrate the ajax call with the data required for the chart. I don;t know enough about the various charting plugins to make my life easy and am currently trialing highchart.

My php script returns the following json:

[{"name":"golfers"},{"data":[5.7879,6.6286,6.1724,5.3125,7.1481,6.1333,4.5769]}]

My chart script is:

$(function () {

visitorData(function(data) {
    console.info(data);
    $('#chart1').highcharts({
        chart: {
            type: 'column'
        },
        title: {
            text: 'Average Visitors'
        },
        xAxis: {
            categories: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        },
        yAxis: {
            title: {
                text: 'Number of visitors'
            }
        },
        series: data,
    });
});
});

my function to make the ajax call:

$.ajax({
        url: '/visitdata',
        type: 'GET',
        async: true,
        dataType: "json",
        success: function (data) {
            console.warn(data);
            return data;

        }
    });

But at the moment nothing is being displayed.

I'm not sure how to effectively make the ajax call and integrate it into the chart function. I decided on a callback based on earlier attempts and posts to ensure data is returned before creating the chart - is this bit correct?

I'm not 100% sure the json data is structured correctly

I'm not sure i;ve applied the data variable to the series correctly

Basically - need a tutorial on this so I can get it working and experiment

All help appreciated

Thanks

3 Answers 3

27

I think you cannot return values from the success call instead you would need to call a function instead. So set up your function that initializes your chart, and in the ajax success call that function with the data

With your code example

function visitorData (data) {
   $('#chart1').highcharts({
    chart: {
        type: 'column'
    },
    title: {
        text: 'Average Visitors'
    },
    xAxis: {
        categories: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    },
    yAxis: {
        title: {
            text: 'Number of visitors'
        }
    },
    series: data,
  });
}
$(document).ready(function() {
 $.ajax({
    url: '/visitdata',
    type: 'GET',
    async: true,
    dataType: "json",
    success: function (data) {
        visitorData(data);
    }
  });
 });
Sign up to request clarification or add additional context in comments.

1 Comment

How can i Achieve this: What if I want to build real time updating chart by multiple ajax call ?
2

In your ajax success function call your visitorData function with data[1].data (since that's how your json is formatted)

    $.ajax({
        url: '/visitdata',
        type: 'GET',
        async: true,
        dataType: "json",
        success: function (data) {
            visitorData(data[1].data);

        }
    });

also, your visitorData function def is odd.

vistorData = function(data) 

or

function vistorData(data)

2 Comments

I think @ryuutatsuo answer is proper one - in your case, author would need to change also structure when creating chart.
Thank you. This is the first attempt at using ajax with a callback and wasnt sure of correct approach. Your answer has helped alot
1
//parse json response
var chartSeriesData = [];
var chartCategory = [];

$.each(response, function() {

  if(this.name!="TOTAL" && this.no!="0") {

    var series_name = this.name;
    var series_data = this.no;

    var series = [
      series_name,
      parseFloat(series_data)
    ];
    chartSeriesData.push(series);   
  }
});

//initialize options for highchart
var options = {
  chart: {
    plotBackgroundColor: null,
    plotBorderWidth: null,
    plotShadow: false
  },
  title: {
    text: 'SalesOrder '
  },
  tooltip: {
    pointFormat: '{series.name}: <b>{point.y}</b>'
  },
  plotOptions: {
    pie: {
      allowPointSelect: true,
      cursor: 'pointer',
      center:['60%','60%'],
      size:150
      ,
      dataLabels: {
        enabled: true,
        color: '#000000',
        distance: 40,
        connectorColor: '#000000',
        format: '<b>{point.name}</b>: {point.y} '
      }
    }
  },
  series: [{
    type: 'pie',
    name: 'Browser share',
    data:chartSeriesData //load array created from json
  }]
}

//options.series[0].setData(datavaluejson);
var chart= $('#container').highcharts(options);

1 Comment

it's working for me specially for how to format data for chart chartSeriesData

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.