我想得到这样一个带有 2 个不同条形的条形图: 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]
]);
最佳答案
我现在找到答案了!
我尝试了几种组合,终于找到了正确的组合。
这里是:
$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);
}
关于javascript - Google 条形图 - 如何使用 JSON 为每个条目获取 2 个不同的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46649217/