Trendline is not showing when displaying data from mysqli

81 views
Skip to first unread message

dyana fadzil

unread,
Dec 25, 2019, 11:10:52 PM12/25/19
to Google Chart API
Hi All,

can help me teach and check on this google chart i have tried, seems the trendline is not showing when querying data from mysqli. 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? i have been stuck here long time already.. 

Thank you!

my 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>

google chart.PNG

Daniel LaLiberte

unread,
Dec 26, 2019, 1:16:50 PM12/26/19
to google-c...@googlegroups.com
Hi Dyana,

In the data that you generate, all the month data is actually strings, not numbers, because you have added quotes.  

  echo "['".$row['month']."',".$row['result_ind']."],";

Remove the single quotes around the month values, so it will be:

  echo "[".$row['month'].",".$row['result_ind']."],";

Hope that helps.

--
You received this message because you are subscribed to the Google Groups "Google Chart API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-chart-a...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-chart-api/f319de46-86dd-4c57-ba6a-f37a313ca182%40googlegroups.com.


--

dyana fadzil

unread,
Dec 30, 2019, 1:45:26 AM12/30/19
to Google Chart API
Hi Daniel,
This works for me! thanks so much for your help! :)
To unsubscribe from this group and stop receiving emails from it, send an email to google-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages