谷歌图表+ mysqli不是绘图

时间:2015-09-03 09:42:32

标签: javascript php mysql json

我试图绘制谷歌图表,但它没有工作。

我创建了一个php脚本来从mysql中获取数据并构建一个数组,然后编码为json,结果为[["mes ano","num trabalhos"],["Janeiro",56],["fevereiro",36],["Mar\u00e7o",39],["Abril",52],["Maio",38],["Junho",62],["Julho",53],["Agosto",43]]

  

访问getdata.php

    $data[0] = array('mes ano','num trabalhos');

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%1-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $janeiro = 0;           
        while($row = $result->fetch_assoc()) {
            $janeiro = $janeiro + 1;
        }
        $data[1] = array('Janeiro', $janeiro);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%2-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $fevereiro = 0;         
        while($row = $result->fetch_assoc()) {
            $fevereiro = $fevereiro + 1;
        }
        $data[2] = array('fevereiro', $fevereiro);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%3-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $marco = 0;         
        while($row = $result->fetch_assoc()) {
            $marco = $marco + 1;
        }
        $data[3] = array('Março', $marco);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%4-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $abril = 0;         
        while($row = $result->fetch_assoc()) {
            $abril = $abril + 1;
        }
        $data[4] = array('Abril', $abril);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%5-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $maio = 0;          
        while($row = $result->fetch_assoc()) {
            $maio = $maio + 1;
        }
        $data[5] = array('Maio', $maio);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%6-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $junho = 0;         
        while($row = $result->fetch_assoc()) {
            $junho = $junho + 1;
        }
        $data[6] = array('Junho', $junho);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%7-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $julho = 0;         
        while($row = $result->fetch_assoc()) {
            $julho = $julho + 1;
        }
        $data[7] = array('Julho', $julho);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%8-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $agosto = 0;            
        while($row = $result->fetch_assoc()) {
            $agosto = $agosto + 1;
        }
        $data[8] = array('Agosto', $agosto);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%9-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $setembro = 0;          
        while($row = $result->fetch_assoc()) {
            $setembro = $setembro + 1;
        }
        $data[9] = array('Setembro', $setembro);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%10-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $outubro = 0;           
        while($row = $result->fetch_assoc()) {
            $outubro = $outubro + 1;
        }
        $data[10] = array('Outubro', $outubro);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%11-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $novembro = 0;          
        while($row = $result->fetch_assoc()) {
            $novembro = $novembro + 1;
        }
        $data[11] = array('Novembro', $novembro);
    }

    $query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%12-2015%'";
    $result = $mysqli->query($query);

    if ($result->num_rows > 0) {
        $dezembro = 0;          
        while($row = $result->fetch_assoc()) {
            $dezembro = $dezembro + 1;
        }
        $data[12] = array('Dezembro', $dezembro);
    }


    echo json_encode($data);
  

尝试绘制图表   

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

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

function drawChart() {
  var jsonData = $.ajax({
      url: "getData.php",
      dataType:"json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});
}

在主要的php文件中,我加载了js文件,我的代码来自google Populating Data Using Server-Side Code

有人可以告诉我,我做错了什么吗?

最好的问候

2 个答案:

答案 0 :(得分:0)

DataTable期望以下列格式提供JSON数据:

var jsonData = {
                "cols": [
                    { "id": "", "label": "mes ano", "pattern": "", "type": "string" },
                    { "id": "", "label": "num trabalhos", "pattern": "", "type": "number" }
                ],
                "rows": [
                    { "c": [{ "v": "Janeiro", "f": null }, { "v": 56, "f": null }] },
                    { "c": [{ "v": "fevereiro", "f": null }, { "v": 36, "f": null }] },
                    { "c": [{ "v": "Mar\u00e7o", "f": null }, { "v": 39, "f": null }] },
                    { "c": [{ "v": "Abril", "f": null }, { "v": 52, "f": null }] },
                    { "c": [{ "v": "Maio", "f": null }, { "v": 38, "f": null }] },
                    { "c": [{ "v": "Junho", "f": null }, { "v": 62, "f": null }] },
                    { "c": [{ "v": "Julho", "f": null }, { "v": 53, "f": null }] },
                    { "c": [{ "v": "Agosto", "f": null }, { "v": 43, "f": null }] }
                ]
            };

基本上你有两个选择

选项1

修改PHP处理程序以按以下格式返回数据:

{
                "cols": [
                    { "id": "", "label": "mes ano", "pattern": "", "type": "string" },
                    { "id": "", "label": "num trabalhos", "pattern": "", "type": "number" }
                ],
                "rows": [
                    { "c": [{ "v": "Janeiro", "f": null }, { "v": 56, "f": null }] },
                    { "c": [{ "v": "fevereiro", "f": null }, { "v": 36, "f": null }] },
                    { "c": [{ "v": "Mar\u00e7o", "f": null }, { "v": 39, "f": null }] },
                    { "c": [{ "v": "Abril", "f": null }, { "v": 52, "f": null }] },
                    { "c": [{ "v": "Maio", "f": null }, { "v": 38, "f": null }] },
                    { "c": [{ "v": "Junho", "f": null }, { "v": 62, "f": null }] },
                    { "c": [{ "v": "Julho", "f": null }, { "v": 53, "f": null }] },
                    { "c": [{ "v": "Agosto", "f": null }, { "v": 43, "f": null }] }
                ]
            };

选项2

使用JavaScript将数据转换为DataTable JSON数据格式,如下所示:



// Load the Visualization API and the piechart package.
google.load('visualization', '1', { 'packages': ['corechart'] });

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

function drawChart() {
    /*var jsonData = $.ajax({
        url: "getData.php",
        dataType:"json",
        async: false
        }).responseText;*/

    var jsonData = [["mes ano", "num trabalhos"], ["Janeiro", 56], ["fevereiro", 36], ["Mar\u00e7o", 39], ["Abril", 52], ["Maio", 38], ["Junho", 62], ["Julho", 53], ["Agosto", 43]];

    var formattedJsonData = {
        "cols": [],
        "rows": []
    };


    jsonData.forEach(function (item, i) {
        if (i === 0) {
            formattedJsonData.cols = [
              { "id": "", "label": "mes ano", "pattern": "", "type": "string" },
              { "id": "", "label": "num trabalhos", "pattern": "", "type": "number" }
            ];
        }
        else {
            formattedJsonData.rows.push({ "c": [{ "v": item[0], "f": null }, { "v": item[1], "f": null }] });
        }
    });





    // Create our data table out of JSON data loaded from server.
    var data = new google.visualization.DataTable(formattedJsonData);

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, { width: 400, height: 240 });
}

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以尝试这个,但这不使用ajax:

ClassNotFoundException:AnnotationTest

这会给你一个json,类似于:

SELECT max(y.id), x.Prop1, x.Prop2 
FROM YourTab y,
  ( SELECT Prop1, Prop2 FROM YourTab
    HAVING COUNT(*)>1
    GROUP BY Prop1,Prop2 ) x
WHERE y.Prop1 = x.Prop1 AND y.Prop2 = y.Prop2
GROUP BY x.Prop1, x.Prop2

然后是图表:

<?php
$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%1-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $janeiro = 0;           
    while($row = $result->fetch_assoc()) {
        $janeiro = $janeiro + 1;
    }
    $data[0] = array('Janeiro', $janeiro);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%2-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $fevereiro = 0;         
    while($row = $result->fetch_assoc()) {
        $fevereiro = $fevereiro + 1;
    }
    $data[1] = array('fevereiro', $fevereiro);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%3-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $marco = 0;         
    while($row = $result->fetch_assoc()) {
        $marco = $marco + 1;
    }
    $data[2] = array('Março', $marco);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%4-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $abril = 0;         
    while($row = $result->fetch_assoc()) {
        $abril = $abril + 1;
    }
    $data[3] = array('Abril', $abril);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%5-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $maio = 0;          
    while($row = $result->fetch_assoc()) {
        $maio = $maio + 1;
    }
    $data[4] = array('Maio', $maio);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%6-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $junho = 0;         
    while($row = $result->fetch_assoc()) {
        $junho = $junho + 1;
    }
    $data[5] = array('Junho', $junho);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%7-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $julho = 0;         
    while($row = $result->fetch_assoc()) {
        $julho = $julho + 1;
    }
    $data[6] = array('Julho', $julho);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%8-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $agosto = 0;            
    while($row = $result->fetch_assoc()) {
        $agosto = $agosto + 1;
    }
    $data[7] = array('Agosto', $agosto);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%9-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $setembro = 0;          
    while($row = $result->fetch_assoc()) {
        $setembro = $setembro + 1;
    }
    $data[8] = array('Setembro', $setembro);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%10-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $outubro = 0;           
    while($row = $result->fetch_assoc()) {
        $outubro = $outubro + 1;
    }
    $data[9] = array('Outubro', $outubro);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%11-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $novembro = 0;          
    while($row = $result->fetch_assoc()) {
        $novembro = $novembro + 1;
    }
    $data[10] = array('Novembro', $novembro);
}

$query = "SELECT data_pedido FROM `gestao_netflow`.`trabalhos` WHERE data_pedido LIKE '%12-2015%'";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    $dezembro = 0;          
    while($row = $result->fetch_assoc()) {
        $dezembro = $dezembro + 1;
    }
    $data[11] = array('Dezembro', $dezembro);
}
?>