Reputation: 31
The trendline is not showing when querying data from MySQL. The month and the result is both in integer format. Attached is the image of the chart and the trendline is not working. Can help me on this?
The coding part is like this:
<?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>
Upvotes: 1
Views: 280
Reputation: 61230
the trendline only only works on a continuous axis (number, date, etc...)
not on a discrete axis (string)
as such, you will need to remove the single quotes from the x-axis values, here...
echo "[".$row['month'].",".$row['result_ind']."],";
Upvotes: 1