0

I am trying to convert a JSON string into an integer so that I can use this data in a google chart. As of right now I can only get one set of data to be displayed in my chart.

Here is my JQUERY code:

$("#shotsandbigcc").click(function(){
//alert("Button works");     

$("#shotsandbigcc_popup").toggle();

var integer = $("#shotsandbigcc").attr("name");
//alert("integer: " + integer);

$.ajax('includes/test.php', {
    type: 'POST',  // http method
    data: {myData: integer},// data to submit
    dataType: 'json',
    success: function(response){
        google.charts.load('current', {packages: ['corechart', 'bar']});
        google.charts.setOnLoadCallback(drawMultSeries);
        function drawMultSeries() {
            var len = response.length;
            for(var i=0; i<len; i++){
            var year = response[i].Year;
            var ontarget = parseInt(response[i].Shots_on_Target);
            var offtarget = parseInt(response[i].Shots_off_Target);
            alert(ontarget);
            }
            alert(year);
            var data = google.visualization.arrayToDataTable([
                ['Year', 'Shots on Target', 'Shots off Target'],
                [year, ontarget, offtarget],
                [year, ontarget, offtarget]
            ]);
            var options = {
                title: 'Shooting Accuracy',
                chartArea: {width: '50%'},
                hAxis: {
                    title: 'Amount of Shots',
                    minValue: 0
                    },
                vAxis: {
                    title: 'Year'
                }
            };

            var chart = new google.visualization.BarChart(document.getElementById('shotsandbigcc_chart'));
            chart.draw(data, options);
}
                        
}
});
}); 

The JSON data is in an array which has this format [{"Year":"2019/2020","Shots_on_Target":"302","Shots_off_Target":"578","Accuracy":"0.34"},{"Year":"2020/2021","Shots_on_Target":"74","Shots_off_Target":"93","Accuracy":"0.44"}]

If someone could tell me how I can display both 2019/2020 and 2020/2021 data to be displayed. I would be most grateful as right now only the 2020/2021 data is being displayed. Thank you.

1
  • 1
    You can try something like var ontarget = +response[i].Shots_on_Target Commented Dec 23, 2020 at 12:56

1 Answer 1

1

For integet value part:

var ontarget =  parseInt(response[i].Shots_on_Target);

For your data part:

var vizData = [];
vizData.push(['Year', 'Shots on Target', 'Shots off Target']);
for(var i=0; i<len; i++){
      var year = response[i].Year;
      var ontarget = parseInt(response[i].Shots_on_Target);
      var offtarget = parseInt(response[i].Shots_off_Target);
      vizData.push([year, ontarget, offtarget]);
      alert(ontarget);
     }
 alert(year);
 var data = google.visualization.arrayToDataTable(vizData);

explaination: since in the loop the values are getting updated in every iteration so, the 'year', 'ontarget' and 'offtarget' will have the latest values only. So on every iteration you have to store values so that they do not get overwritten. For that now this code is pushing in array in every iteration preserving the previous values. Which now you can use in the google.visualization function.

Happy Coding!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your help. This has sorted the error of it being a string. I am now faced with the issue of that only the last set of data is being displayed. Any idea on how I can get both sets to be displayed?
@Sam_West hey, I have updated the answer. Your values were getting overwritten in the loop which were causing it to only show the latest set and not the previous values.
Thank you so much. This has sorted my problem. I'm very grateful.

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.