我尝试在我的一个项目中使用Google Charts。一切都很好,但是我的Y轴完全没有排序。我使用的是我的DB数据(服务器端)。
这是我用来显示图表的实际代码。
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart','bar']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['gisement', 'element1', 'element2', 'element3', 'element4', 'element5', 'element6'],
<?php
$result = $pdo->query ("SELECT
gisement.nom_gisement AS gisement,
SUM(CASE WHEN mesure.e1 IS NOT NULL THEN 1 ELSE 0 END) AS element1,
SUM(CASE WHEN mesure.e2 IS NOT NULL THEN 1 ELSE 0 END) AS element2,
SUM(CASE WHEN mesure.e3 IS NOT NULL THEN 1 ELSE 0 END) AS element3,
SUM(CASE WHEN mesure.e4 IS NOT NULL THEN 1 ELSE 0 END) AS element4,
SUM(CASE WHEN mesure.e5 IS NOT NULL THEN 1 ELSE 0 END) AS element5,
SUM(CASE WHEN mesure.e6 IS NOT NULL THEN 1 ELSE 0 END) AS element6
FROM mesure , puits, niveau , gisement
where mesure.cm = puits.cm
and mesure.num_niveau = niveau.id_niveau
and gisement.id_gisement = puits.num_gisement
group by gisement.nom_gisement");
foreach($result AS $row) {
echo "['".$row['gisement']."','".$row['element1']."','".$row['element2']."','".$row['element3']."','".$row['element4']."','".$row['element5']."','".$row['element6']."'], ";
}
?>
]);
var options = {
chart: {
title: '',
subtitle: 'Nombres de puits annalysés en chaque gisement' }
};
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
<div id='columnchart_material' style=' width: 600px; height: 350px;' class='col-md-offset-3' ></div>
结果看起来有点奇怪,我想Y轴的值并没有排序,而是在它们进来的时候就被取走了。
我希望有人能帮我解决这个问题。
你的数据中的值应该是数字,而不是字符串,这里...
echo "['".$row['gisement']."','".$row['element1']."','".$row['element2']."','".$row['element3']."','".$row['element4']."','".$row['element5']."','".$row['element6']."'], ";
删除值列中的单引号...。
echo "['".$row['gisement']."',".$row['element1'].",".$row['element2'].",".$row['element3'].",".$row['element4'].",".$row['element5'].",".$row['element6']."], ";