0

I'm in doubt about something with google charts, when i use addRows command, is it enough that my php variable be in the required format?

I'll put the code i'm working with:

<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">

        google.load("visualization", "1.0",{"packages":["corechart"]});

        google.setOnLoadCallback(drawChart);

        function drawChart(){
            //create the data table
            var data = new google.visualization.DataTable();
            data.addColumn("string","Fruits");
            data.addColumn("number","Amount");
            data.addRows([<?php echo "'$jsRows'";?>]);

            //set chart options
            var options = {"title":"Amount of different fruits",
                "width":400,
                "height":300};

            //instantiate and draw chart, passing in options
            var options = new google.visualization.PieChart(document.getElementById("chart_div"));
            chart.draw(data, options);
            } //end of drawchart function
            </script>

When i write: echo $jsRows; i get: ["0 as 6 h",0],["6 as 12h",0],["12 as 18h",8],["18 as 24h",0], which is a data format google chart can handle, but with this code, my chart doesn't appear. Does anyone have any idea why? Thanks in advance!

1 Answer 1

1

You should remove the quotes from around the $jsRows variable. Using quotes turns the array into a string, which is not compatible with the #addRows method. Do this:

data.addRows([<?php echo $jsRows;?>]);
Sign up to request clarification or add additional context in comments.

Comments

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.