Google条形图 - 如何使用JSON为每个条目获取2个不同的条形图

时间:2017-10-09 14:47:15

标签: javascript php mysql json charts

我希望得到这样一个带有两个不同条形的条形图: Google bar chart

但我不知道,如何用JSON做到这一点。我希望每个x轴条目都有一个红色和蓝色条。

这是我的代码:

<?php
$con = new mysqli($servername, $username, $password, $dbname);

$query = "SELECT COUNT(CASE WHEN name_Gleitzeitrahmen = 'Ja' THEN 1 END) as Ja,COUNT(CASE WHEN name_Gleitzeitrahmen = 'Nein' THEN 1 END)as Nein,quarter(datum) as quartal FROM dashboard GROUP BY quartal";
$exec = mysqli_query($con,$query);


$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(

// Labels for your chart, these represent the column titles
percentage and string will be used for column title
array('label' => 'quartal', 'type' => 'string'),
array('label' => 'Ja', 'type' => 'number'),
array('label' => 'Nein', 'type' => 'number')
);

$rows = array();
while($r = mysqli_fetch_assoc($exec)) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp[] = array('v' => (string) $r['quartal']); 

// Values of each slice
$temp[] = array('v' => (int) $r['Ja'] ); 
$temp[] = array('s' => (int) $r['Nein']); 
$rows[] = array('c' => $temp);
}

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

 ?>

实际图表如下所示:Actual chart

格式必须如下:

 function drawStuff() {
    var data = new google.visualization.arrayToDataTable([
      ['Galaxy', 'Distance', 'Brightness'],
      ['Canis Major Dwarf', 8000, 23.3],
      ['Sagittarius Dwarf', 24000, 4.5],
      ['Ursa Major II Dwarf', 30000, 14.3],
      ['Lg. Magellanic Cloud', 50000, 0.9],
      ['Bootes I', 60000, 13.1]
    ]);

1 个答案:

答案 0 :(得分:0)

我现在找到了答案!

我尝试了几种组合,最后找到了正确的组合。

这是:

$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(

// Labels for your chart, these represent the column titles
// Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
array('label' => 'Quartal', 'type' => 'string'),
array('label' => 'Anzahl Ja', 'type' => 'number'),
array('label' => 'Anzahl Nein', 'type' => 'number')
);

$rows = array();
while($r = mysqli_fetch_assoc($exec)) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp[] = array('v' => (string) $r['quartal']); 

// Values of each slice
$temp[] = array('v' => (int) $r['Ja'] ); 
$temp[] = array('v' => (int) $r['Nein'] );
$rows[] = array('c' => $temp);

}