I have a javascript file that imports Google Visualization to draw a line graph:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">


    var inputData =  [[1990,157894],[1991,173725],[1992,181227],[1993,183315]];

      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {        
      var data = new google.visualization.DataTable();
            data.addColumn('number', 'Year');
            data.addColumn('number', 'January');
            data.addRows(inputData);


        var options = {
          title: 'Unemployed Statistics'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

After

<?php

if (isset($_POST['submitted'])) {

I have an element that draws the graph:

<div id="chart_div" style="width: 900px; height: 500px;"></div>

This works fine and the graph is drawn fine with this. However, when I put my json variable into var inputData, no graph is drawn. It would look like this:

var inputData = <?php echo json_encode($json['unemployment'), JSON_NUMERIC_CHECK) ?>; 

Why is nothing being drawn?

I run a print statement after I submit my form to check and see what <?php echo json_encode($json['unemployment') ?> is. Here is the output: [[1990,131337],[1991,160256],[1992,188129]] .

This output looks like it should easily work with the var inputData variable in my javascript code. However, nothing gets drawn. Am I doing something wrong?

有帮助吗?

解决方案

look here: you have syntax error

<?php echo json_encode($json['unemployment'), JSON_NUMERIC_CHECK) 

$json['unemployment')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top