1

I've a function which feeds a chart with fixed data:

function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Date', 'CPU Temp'],
        ['2004', 340],
        ['2005',  460],
        ['2006', 1120],
        ['2007',  540]
    ]);
}

I'd like to replace the fixed data with the one read from a DB table containing two columns: date and temperature. The read data is stored in the $json variable:

[
  {"datain":"2021-04-20 12:00:03","temp":"39.2"},
  {"datain":"2021-04-21 00:00:05","temp":"40.2"},
  {"datain":"2021-04-21 12:00:03","temp":"40.8"},
  {"datain":"2021-08-01 12:00:12","temp":"47.2"}
]

Here's my bad code:

let date= "";
let temp = "";
for (let i = 0; i < <?php echo count($json)?>; i++){
    date = <?php echo $json[0] ?>;
    temp = <?php echo $json[1] ?>;
}
  
function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ['Date', 'CPU Temp'],
        [date,  temp]
    ]);
}

How can I make this work?

EDIT: Code after being edited with @trincot's suggestions:

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

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Date', 'Temp'],
    ...<?php echo $json; ?>.map(({
      datain,
      temp
    }) => [new Date(datain), parseFloat(temp)])
  ]);

  var options = {
    title: 'CPU Temp History',
    curveType: 'function',
    legend: {
      position: 'bottom'
    }
  };

  var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

  chart.draw(data, options);
} <
/script>

Original code is here:chart code

5
  • What is the structure/content of $json? Can you give explicit sample data in PHP or JSON format? Commented Aug 1, 2021 at 17:21
  • Sure:( [0] => Array ( [datain] => 2021-04-20 12:00:03 [temp] => 39.2 ) [1] => Array ( [datain] => 2021-04-21 00:00:05 [temp] => 40.2 ) [2] => Array ( [datain] => 2021-04-21 12:00:03 [temp] => 40.8 ) [3] => Array ( [datain] => 2021-08-01 12:00:12 [temp] => 47.2 ) ) Commented Aug 1, 2021 at 17:24
  • How do I display that info? Commented Aug 1, 2021 at 17:30
  • 1
    ok: [{"datain":"2021-04-20 12:00:03","temp":"39.2"},{"datain":"2021-04-21 00:00:05","temp":"40.2"},{"datain":"2021-04-21 12:00:03","temp":"40.8"},{"datain":"2021-08-01 12:00:12","temp":"47.2"}] Commented Aug 1, 2021 at 17:37
  • It is not needed to copy code from an answer back into the question. Stack Overflow works well with question and answer kept separate. Commented Aug 1, 2021 at 19:07

1 Answer 1

3

Given that your $json is an array of objects with two properties, you'll need to convert that to an array of arrays (pairs):

function drawChart() {
    var data = google.visualization.arrayToDataTable([
       ['Date', 'CPU Temp'],
       ...<?php echo $json; ?>.map(({datain, temp}) => [new Date(datain), +temp])
    ]);
}

So this just injects the whole JSON inside the array literal that is given as argument to arrayToDataTable. The .map call converts the plain objects to pairs and:

  • the date strings are converted to Date objects
  • the temperature strings are converted to numbers. It is a pity that your PHP code has somehow stored the temperatures as strings. That is odd. It would be better practice to have them there with a number data type, not a string data type.

When you look at the source via your browser, you should see something like this:

function drawChart() {
    var data = google.visualization.arrayToDataTable([
       ['Date', 'CPU Temp'],
       ...[{"datain":"2021-04-20 12:00:03","temp":"39.2"},{"datain":"2021-04-21 00:00:05","temp":"40.2"},{"datain":"2021-04-21 12:00:03","temp":"40.8"},{"datain":"2021-08-01 12:00:12","temp":"47.2"}].map(({datain, temp}) => [new Date(datain), +temp])
    ]);
}
9
  • 2
    Ok thank you but I don't get any errors now as I said. The chart just doesn't display. Will update my original code to reflect your suggestions. Thanks again!! Commented Aug 1, 2021 at 18:29
  • 1
    Oh I thought you referred to the code at the start of your question. Didn't realise you meant my answer's code worked. Looking at the JSON, I see that there are multiple lines with the same date (just different time). You might want to verify the source of your data if you intended to have only one figure per day. Commented Aug 1, 2021 at 18:33
  • 2
    OMG! Yes! that could be an issue. Will see if I can change the data. Thank you. Commented Aug 1, 2021 at 18:43
  • It looks like I'm dealing with strings. Here's the what the browser sees: function drawChart() { var data = google.visualization.arrayToDataTable([ ['Date', 'Temp'], [...[{"datain":"2021-04-20 12:00:03","temp":"39.2"},{"datain":"2021-04-21 00:00:05","temp":"40.2"},{"datain":"2021-08-01 12:00:12","temp":"47.2"}].map(({datain, temp}) => [new Date(datain), temp])] ]); Commented Aug 1, 2021 at 18:53
  • 1
    Will check my php code. In the DB the temp field is set to float. Thank you again!! Commented Aug 1, 2021 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.