Javascript 使用googlechartapi从MySQL数据库创建饼图

Javascript 使用googlechartapi从MySQL数据库创建饼图,javascript,php,mysql,json,google-visualization,Javascript,Php,Mysql,Json,Google Visualization,我正在尝试使用GoogleChartAPI使用数据库中的数据创建饼图。我的数据库有两列,一列是ID,一列是major。对于行,我有相应的ID和一些主项。我使用PHP将每个专业的代码相加,然后将其转换为JSON。我的数据在JSON中看起来格式正确,但由于某种原因,饼图只显示了主数据,并且数字为2(100%)。它似乎没有正确读取专业总数,我不明白为什么。我做了一些搜索,但找不到有类似问题的人 下面是输出的JSON {"cols":[{"label":"Major","type":"strin

我正在尝试使用GoogleChartAPI使用数据库中的数据创建饼图。我的数据库有两列,一列是ID,一列是major。对于行,我有相应的ID和一些主项。我使用PHP将每个专业的代码相加,然后将其转换为JSON。我的数据在JSON中看起来格式正确,但由于某种原因,饼图只显示了主数据,并且数字为2(100%)。它似乎没有正确读取专业总数,我不明白为什么。我做了一些搜索,但找不到有类似问题的人

下面是输出的JSON

    {"cols":[{"label":"Major","type":"string"},{"label":"CS","type":"number"},{"label":"CIS","type":"number"},{"label":"Other","type":"number"}],"rows":[{"c":[{"v":"MAJOR"},{"v":2},{"v":1},{"v":2}]}]}
这是我的PHP

    $db = new PDO("mysql:host=".$servername.";dbname=".$dbname.";", $username, $password);

    $query = $db->query("SELECT 'MAJOR' as 'Major', SUM(IF(MAJOR = 'Computer Science',1,0)) as CS, SUM(IF(MAJOR = 'Computer Information Systems',1,0)) as CIS, SUM(IF(MAJOR = 'Other',1,0)) as Other FROM ages");


    $rows = array();
    $table = array();

    $table['cols'] = array(
        array('label' => 'Major', 'type' => 'string'),
        array('label' => 'CS', 'type' => 'number'),
        array('label' => 'CIS', 'type' => 'number'),
        array('label' => 'Other', 'type' => 'number')
    );


    while($r = $query->fetch()){
        $temp = array();
        $temp[] = array('v' => $r['Major']);
        $temp[] = array('v' => (int) $r['CS']);
        $temp[] = array('v' => (int) $r['CIS']);
        $temp[] = array('v' => (int) $r['Other']);

        $rows[] = array('c' => $temp);
    }

    $table['rows'] = $rows;

    $jsonTable = json_encode($table);

    echo $jsonTable;
下面是加载图表并使用JSON数据的Javascript

// 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});
    }

不应该选择“专业”作为“专业”
仅仅是
选择“专业”作为“专业”
?@82Tuskers现在只有计算机科学显示为2,这是正确的,但它没有显示我的其余数据。@82t因为某种原因,它似乎只显示最后输入的专业,而不是所有专业。您能在结尾处按1添加一个组吗:
"... 'Other',1,0)正如Other从年龄组1
@82t取得了一些微小的进步,现在计算机科学是唯一显示出来的,尽管最后输入数据库的专业是什么。