0

Hi I'm trying to display a dynamic line chart using Chartjs with data pulled from SQL using PHP in JSON format. The data is successfully selected but does not successfully display just a blank chart, any help is appreciated.

$(document).ready(function() {
  var dataPointsA = []
  var dataPointsB = []

  $.ajax({
    type: 'GET',
    url: 'data.php',
    dataType: 'json',
    success: function(field) {

      for (var i = 0; i < field.length; i++) {
        dataPointsA.push({
          x: field[i].datetime,
          y: field[i].roomtemp
        });
        dataPointsB.push({
          x: field[i].datetime,
          y: field[i].tanktemp
        });
      }
console.log(field);
      var chartdata = {
        title: {
          text: "Fish Tank Monitor"
        },

        data: [{
          type: "line",
          name: "line1",
          dataPoints: dataPointsA
        }, {
          type: "line",
          name: "line2",
          dataPoints: dataPointsB
        }, ]
      };
console.log(chartdata);

      var ctx = mycanvas.getContext('2d');

      var barGraph = new Chart(ctx, {
                type: 'line',
                data: chartdata,
                backgroundColor: 'rgba(0, 119, 204, 0.3)'
            });
    }
  });
});

Example JSON

[{"datetime":"2018-07-28 22:33:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:32:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:31:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:30:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:29:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:28:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:27:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:26:00.000","roomtemp":26.8,"tanktemp":28.4},{"datetime":"2018-07-28 22:25:00.000","roomtemp":26.9,"tanktemp":28.4},{"datetime":"2018-07-28 22:24:00.000","roomtemp":26.9,"tanktemp":28.4}]

1 Answer 1

1

I think its happeing due to incorrect date time format. datetime key you used is acting as a string. I think you need to apply one date and time filter

Sign up to request clarification or add additional context in comments.

Comments

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.