从MySQL查询数据时,趋势线未显示。月和结果均为整数格式。附件为图表图像,趋势线不起作用。可以帮我吗?
编码部分是这样的:
<?php
include ('connection.php');
?>
<html>
<head>
<title>Google Charts Tutorial</title>
<script type = "text/javascript" src = "https://www.gstatic.com/charts/loader.js">
</script>
<script type = "text/javascript" src = "https://www.google.com/jsapi">
</script>
<script type = "text/javascript">
google.charts.load('current', {packages: ['corechart']});
</script>
</head>
<body>
<div id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
</div>
<script language = "JavaScript">
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Month', 'Performance %'],
<?php
$query = "SELECT month , result_ind FROM performance GROUP BY month";
$exec = mysqli_query($db,$query);
while($row = mysqli_fetch_assoc($exec)){
echo "['".$row['month']."',".$row['result_ind']."],";
}
?>
]);
// Set chart options
var options = {
'title':'Month vs %',
'width':550,
'height':400,
trendlines: {
0: {
type: 'exponential',
color: 'green',
visibleInLegend: true,
}
} // Draw a trendline for data series 0.
};
// Instantiate and draw the chart.
var chart = new google.visualization.ScatterChart(document.getElementById('container'));
chart.draw(data, options);
}
google.charts.setOnLoadCallback(drawChart);
</script>
</body>
</html>
答案 0 :(得分:0)
趋势线仅在连续轴(数字,日期等)上起作用
不在离散轴(字符串)上
这样,您将需要从x轴值中删除单引号,在这里...
echo "[".$row['month'].",".$row['result_ind']."],";