1
votes

Google-chart trend-line not showing. Tried to put the code in vAxis, hAxis and only in options but no luck. I know that the first column must be a number or date and so on and my first column is a date.

 <script type="text/javascript">
  data.sort([{column: 0}]);
  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Date', 'Sum'],
       <?php while($row = mysqli_fetch_assoc($result)) { ?>

      [<?php echo $row['date'] ?>,  <?php echo $row['col2'] ?> ],

    <?php } ?>
    ]);

    var options = {
         legend: { position: 'bottom' },
        title: 'Sum per day',
         hAxis : { textStyle : { fontSize: 10 } },
    vAxis: { viewWindowMode: 'explicit', viewWindow: { min: 0 } },
    trendlines: { 0: {} }
    };

    var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
    chart.draw(data, options);
  }
</script>

enter image description here

1
Yes, of course it´s a string, i edited my code but now it´s look like this, see picture. - John_Doe
try sorting the data before you draw the chart --> data.sort([{column: 0}]); - WhiteHat
I dont know exactly where to put that line. I edited my code. Can You please look? It´s not working and as You can see from the first Picture the dates not match. It should be 2019-01-01 and so on but it´s 1,985 etc. Maybe it´s because i use GROUP BY date in my query? - John_Doe
I have id, date (date), col2 (int11). In the fields: date in format 2019-05-29 and in col2 is only a number, no decimals - John_Doe

1 Answers

0
votes

when loading the data, the first column needs to be converted from a string to a date.
place the value from php in quotes and inside a date constructor.

e.g.

[new Date('<?php echo $row['date'] ?>'),  <?php echo $row['col2'] ?> ],

if you still have problems with the squiggly line,
sort the data before drawing...

data.sort([{column: 0}]);
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
chart.draw(data, options);