Google line Harts does not appear line

Asked

Viewed 167 times

2

I’m trying to generate a line chart like Google Charts but the lines don’t form and I don’t know why. The chart even appears with the subtitles but no longer works follows below my code (the button that calls the chart):

    $("#config2").on("click", function() {
$("#f").val("2");

google.charts.load('current', {'packages':['corechart']});

   function drawChart() {
        var i = $("#i2").val();
        var m = $("#in").val();
        var d = $("#te").val();
        var f = $("#f").val();

                // call ajax function to get sports data
                var jsonData = $.ajax({
                    url: "dadosestatistica.php",
                    type: 'GET',
                    dataType: "json",
                    data : {i: i, m: m, d: d, f: f},
                    async: false
                }).responseText;


                //The DataTable object is used to hold the data passed into a visualization.
                var data = new google.visualization.DataTable(jsonData);

                var options = {
                    chart: {
                      title: 'Gráfico de Crimes Estado Rio de Janeiro'
                    }
                  };
                    // To render the pie chart.
                var chart = new google.visualization.LineChart(document.getElementById('chart_div'));


                chart.draw(data, options);


            }


            // Set a callback to run when the Google Visualization API is loaded.
            google.charts.setOnLoadCallback(drawChart);


});

The statistical data that generates the array:

$data = "";
            $rows = array();
            $retorno = array();
            $retorno['cols'] = array(
            array('label' => 'DATA', 'type' => 'string'),
            array('label' => 'VALOR1', 'type' => 'number'),
            array('label' => 'VALOR2', 'type' => 'number'),
            array('label' => 'VALOR3', 'type' => 'number'),
            array('label' => 'VALOR4', 'type' => 'number'),
            array('label' => 'VALOR5', 'type' => 'number'),
            array('label' => 'VALOR6', 'type' => 'number'),
            array('label' => 'VALOR7', 'type' => 'number'),
            array('label' => 'VALOR8', 'type' => 'number'),
            );

            while ($row = mysqli_fetch_assoc($res)) {

            $temp = array();

            if($data != $row['data']){
                $data = $row['data'];

            $temp[] = array('v' => (string)converte_data($row['data']));                

            }
            $temp[] = array('v' => (int)$row['total']);
            $rows[] = array('c' => $temp);          
            }

            $retorno['rows'] = $rows;
            $jsonTable = json_encode($retorno);

            echo $jsonTable;

Array output:

{"cols":[
{"label":"DATA","type":"string"},
{"label":"VALOR1","type":"number"},
{"label":"VALOR2","type":"number"},
{"label":"VALOR3","type":"number"},
{"label":"VALOR4","type":"number"},
{"label":"VALOR5","type":"number"},
{"label":"VALOR6","type":"number"},
{"label":"VALOR7","type":"number"},
{"label":"VALOR8","type":"number"}
],
"rows":[
{"c":[{"v":"30\/10\/2017"},{"v":1}]},{"c":[{"v":3}]},{"c":[{"v":1}]},{"c":[{"v":1}]},{"c":[{"v":1}]},{"c":[{"v":4}]},{"c":[{"v":1}]},{"c":[{"v":4}]}
]
}

I’ve broken my head and I can’t figure out how to fix it.

I figured out why the array is not working should be mounted this way:

{"cols":[
    {"label":"DATA","type":"string"},
    {"label":"VALOR1","type":"number"},
    {"label":"VALOR2","type":"number"},
    {"label":"VALOR3","type":"number"},
    {"label":"VALOR4","type":"number"},
    {"label":"VALOR5","type":"number"},
    {"label":"VALOR6","type":"number"},
    {"label":"VALOR7","type":"number"},
    {"label":"VALOR8","type":"number"}
    ],
    "rows":[
    {"c":[{"v":"30\/10\/2017"},{"v":1},{"v":3},{"v":1},{"v":1},{"v":1},{"v":4},{"v":1},{"v":4}]},
    {"c":[{"v":"31\/10\/2017"},{"v":2},{"v":4},{"v":3},{"v":2},{"v":4},{"v":3},{"v":2},{"v":5}]}
    ]
    }

Every time you change the date is a new row the database data comes like this:

'VALOR1', '2017-10-30', '1'
'VALOR2', '2017-10-30', '3'
'VALOR3', '2017-10-30', '1'
'VALOR4', '2017-10-30', '1'
'VALOR5', '2017-10-30', '1'
'VALOR6', '2017-10-30', '4'
'VALOR7', '2017-10-30', '1'
'VALOR8', '2017-10-30', '4'
'VALOR1', '2017-10-31', '2'
'VALOR2', '2017-10-31', '4'
'VALOR3', '2017-10-31', '3'
'VALOR4', '2017-10-31', '2'
'VALOR5', '2017-10-31', '4'
'VALOR6', '2017-10-31', '3'
'VALOR7', '2017-10-31', '2'
'VALOR8', '2017-10-31', '5'

I’m not getting to assemble this array.

1 answer

0


I solved the problem by changing the sql so that the database answer came this way:

'DATA', 'VALOR1', 'VALOR2', 'VALOR3', 'VALOR4', 'VALOR5', 'VALOR6', 'VALOR7', 'VALOR8'  
'2017-10-30', '1', '3', '1', '1', '1', '4', '1', '4'
'2017-10-31', '2', '4', '3', '2', '4', '3', '2', '5'

and the loop that mounts the array changed to:

  while ($row = mysqli_fetch_assoc($res)) {

            $temp = array();           

            $temp[] = array('v' => (string)converte_data($row['data']));        
            $temp[] = array('v' => (int)$row['valor1']);
            $temp[] = array('v' => (int)$row['valor2']);
            $temp[] = array('v' => (int)$row['valor3']);
            $temp[] = array('v' => (int)$row['valor4']);
            $temp[] = array('v' => (int)$row['valor5']);
            $temp[] = array('v' => (int)$row['valor6']);
            $temp[] = array('v' => (int)$row['valor7']);
            $temp[] = array('v' => (int)$row['valor8']);
            $rows[] = array('c' => $temp);          
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.