Chart.js not working - Uncaught RefernceError:











up vote
0
down vote

favorite












I am trying to display in chart form some data on time series data using chart.js. The data is fetched from psql, converted to json, and sent as an argument to js function to render a chart.



The data is received properly in js but there are errors 'Uncaught RefernceError' about the js function call in HTML.



EDIT



It's raising these errors:



Uncaught RefernceError: dspChrt is not defined @ line
dspChrt(WData);
</script>
</body>
</html>


Here is my code:



JS



<script>
var Device_Data;
var rssi, batt;
var rssiArray = ;
var battArray = ;
var N = 12;
for (i = 0; i < N; i++) {
rssiArray.push(0);
battArray.push(0); }

function dspChrt(Device_Data) {

console.log(Device_Data[0].rssi);
console.log(Device_Data[1].battery_voltage_mv);
rssi = Device_Data[0].rssi;
batt = Device_Data[1].battery_voltage_mv;
rssiArray.shift();
rssiArray.push(rssi);
battArray.shift();
battArray.push(batt);

var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [new Date()],
datasets: [{
label: 'rssi',
data: rssiArray,
backgroundColor: "rgba(153,255,51,0.4)"
}, {
label: 'Battery_Voltage',
data: battArray,
backgroundColor: "rgba(255,153,0,0.4)"
}]
}
options: {
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'hour',
}
}]
}
}


});
}

//var myVar = setInterval(dspChrt, 60000);
</script>


HTML/PHP



 <?php
require("Connection.php");
$stmt = $conn->prepare("Select date_time, battery_voltage_mv FROM measuring_device_statuses LIMIT 24");
$stmt->execute();

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$WData = $stmt->fetchAll();

/*
echo "<pre>".print_r($WData, true)."</pre>";
die();
*/

?>

<script>
var WData = <?php print_r(json_encode($WData));?>;
//console.log(WData);
dspChrt(WData);
</script>

</body>
</html>









share|improve this question




























    up vote
    0
    down vote

    favorite












    I am trying to display in chart form some data on time series data using chart.js. The data is fetched from psql, converted to json, and sent as an argument to js function to render a chart.



    The data is received properly in js but there are errors 'Uncaught RefernceError' about the js function call in HTML.



    EDIT



    It's raising these errors:



    Uncaught RefernceError: dspChrt is not defined @ line
    dspChrt(WData);
    </script>
    </body>
    </html>


    Here is my code:



    JS



    <script>
    var Device_Data;
    var rssi, batt;
    var rssiArray = ;
    var battArray = ;
    var N = 12;
    for (i = 0; i < N; i++) {
    rssiArray.push(0);
    battArray.push(0); }

    function dspChrt(Device_Data) {

    console.log(Device_Data[0].rssi);
    console.log(Device_Data[1].battery_voltage_mv);
    rssi = Device_Data[0].rssi;
    batt = Device_Data[1].battery_voltage_mv;
    rssiArray.shift();
    rssiArray.push(rssi);
    battArray.shift();
    battArray.push(batt);

    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'line',
    data: {
    labels: [new Date()],
    datasets: [{
    label: 'rssi',
    data: rssiArray,
    backgroundColor: "rgba(153,255,51,0.4)"
    }, {
    label: 'Battery_Voltage',
    data: battArray,
    backgroundColor: "rgba(255,153,0,0.4)"
    }]
    }
    options: {
    scales: {
    xAxes: [{
    type: 'time',
    time: {
    unit: 'hour',
    }
    }]
    }
    }


    });
    }

    //var myVar = setInterval(dspChrt, 60000);
    </script>


    HTML/PHP



     <?php
    require("Connection.php");
    $stmt = $conn->prepare("Select date_time, battery_voltage_mv FROM measuring_device_statuses LIMIT 24");
    $stmt->execute();

    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $WData = $stmt->fetchAll();

    /*
    echo "<pre>".print_r($WData, true)."</pre>";
    die();
    */

    ?>

    <script>
    var WData = <?php print_r(json_encode($WData));?>;
    //console.log(WData);
    dspChrt(WData);
    </script>

    </body>
    </html>









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to display in chart form some data on time series data using chart.js. The data is fetched from psql, converted to json, and sent as an argument to js function to render a chart.



      The data is received properly in js but there are errors 'Uncaught RefernceError' about the js function call in HTML.



      EDIT



      It's raising these errors:



      Uncaught RefernceError: dspChrt is not defined @ line
      dspChrt(WData);
      </script>
      </body>
      </html>


      Here is my code:



      JS



      <script>
      var Device_Data;
      var rssi, batt;
      var rssiArray = ;
      var battArray = ;
      var N = 12;
      for (i = 0; i < N; i++) {
      rssiArray.push(0);
      battArray.push(0); }

      function dspChrt(Device_Data) {

      console.log(Device_Data[0].rssi);
      console.log(Device_Data[1].battery_voltage_mv);
      rssi = Device_Data[0].rssi;
      batt = Device_Data[1].battery_voltage_mv;
      rssiArray.shift();
      rssiArray.push(rssi);
      battArray.shift();
      battArray.push(batt);

      var ctx = document.getElementById('myChart').getContext('2d');
      var myChart = new Chart(ctx, {
      type: 'line',
      data: {
      labels: [new Date()],
      datasets: [{
      label: 'rssi',
      data: rssiArray,
      backgroundColor: "rgba(153,255,51,0.4)"
      }, {
      label: 'Battery_Voltage',
      data: battArray,
      backgroundColor: "rgba(255,153,0,0.4)"
      }]
      }
      options: {
      scales: {
      xAxes: [{
      type: 'time',
      time: {
      unit: 'hour',
      }
      }]
      }
      }


      });
      }

      //var myVar = setInterval(dspChrt, 60000);
      </script>


      HTML/PHP



       <?php
      require("Connection.php");
      $stmt = $conn->prepare("Select date_time, battery_voltage_mv FROM measuring_device_statuses LIMIT 24");
      $stmt->execute();

      $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
      $WData = $stmt->fetchAll();

      /*
      echo "<pre>".print_r($WData, true)."</pre>";
      die();
      */

      ?>

      <script>
      var WData = <?php print_r(json_encode($WData));?>;
      //console.log(WData);
      dspChrt(WData);
      </script>

      </body>
      </html>









      share|improve this question















      I am trying to display in chart form some data on time series data using chart.js. The data is fetched from psql, converted to json, and sent as an argument to js function to render a chart.



      The data is received properly in js but there are errors 'Uncaught RefernceError' about the js function call in HTML.



      EDIT



      It's raising these errors:



      Uncaught RefernceError: dspChrt is not defined @ line
      dspChrt(WData);
      </script>
      </body>
      </html>


      Here is my code:



      JS



      <script>
      var Device_Data;
      var rssi, batt;
      var rssiArray = ;
      var battArray = ;
      var N = 12;
      for (i = 0; i < N; i++) {
      rssiArray.push(0);
      battArray.push(0); }

      function dspChrt(Device_Data) {

      console.log(Device_Data[0].rssi);
      console.log(Device_Data[1].battery_voltage_mv);
      rssi = Device_Data[0].rssi;
      batt = Device_Data[1].battery_voltage_mv;
      rssiArray.shift();
      rssiArray.push(rssi);
      battArray.shift();
      battArray.push(batt);

      var ctx = document.getElementById('myChart').getContext('2d');
      var myChart = new Chart(ctx, {
      type: 'line',
      data: {
      labels: [new Date()],
      datasets: [{
      label: 'rssi',
      data: rssiArray,
      backgroundColor: "rgba(153,255,51,0.4)"
      }, {
      label: 'Battery_Voltage',
      data: battArray,
      backgroundColor: "rgba(255,153,0,0.4)"
      }]
      }
      options: {
      scales: {
      xAxes: [{
      type: 'time',
      time: {
      unit: 'hour',
      }
      }]
      }
      }


      });
      }

      //var myVar = setInterval(dspChrt, 60000);
      </script>


      HTML/PHP



       <?php
      require("Connection.php");
      $stmt = $conn->prepare("Select date_time, battery_voltage_mv FROM measuring_device_statuses LIMIT 24");
      $stmt->execute();

      $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
      $WData = $stmt->fetchAll();

      /*
      echo "<pre>".print_r($WData, true)."</pre>";
      die();
      */

      ?>

      <script>
      var WData = <?php print_r(json_encode($WData));?>;
      //console.log(WData);
      dspChrt(WData);
      </script>

      </body>
      </html>






      javascript php html chart.js psql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 15:58

























      asked Nov 7 at 15:46









      XCeptable

      56941433




      56941433
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          When I looked at your code, I found labels were configured, however your timestamp format will never match those labels. As a result, showing 0 makes sense.



          Not sure what do you want. Maybe something like this?
          Take a look at the script they coded, you will find out they didn't configure labels.



          If I were you, I might use scatter plot with showLines=true instead.






          share|improve this answer





















          • Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
            – XCeptable
            Nov 8 at 9:36










          • Ctrl+Shift+i and you can find the script of that link in <body>.
            – chouhy
            Nov 8 at 10:29












          • I edited the code but it's still complaining about syntax where I put 'options'.
            – XCeptable
            Nov 8 at 15:59













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192904%2fchart-js-not-working-uncaught-refernceerror%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          When I looked at your code, I found labels were configured, however your timestamp format will never match those labels. As a result, showing 0 makes sense.



          Not sure what do you want. Maybe something like this?
          Take a look at the script they coded, you will find out they didn't configure labels.



          If I were you, I might use scatter plot with showLines=true instead.






          share|improve this answer





















          • Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
            – XCeptable
            Nov 8 at 9:36










          • Ctrl+Shift+i and you can find the script of that link in <body>.
            – chouhy
            Nov 8 at 10:29












          • I edited the code but it's still complaining about syntax where I put 'options'.
            – XCeptable
            Nov 8 at 15:59

















          up vote
          0
          down vote













          When I looked at your code, I found labels were configured, however your timestamp format will never match those labels. As a result, showing 0 makes sense.



          Not sure what do you want. Maybe something like this?
          Take a look at the script they coded, you will find out they didn't configure labels.



          If I were you, I might use scatter plot with showLines=true instead.






          share|improve this answer





















          • Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
            – XCeptable
            Nov 8 at 9:36










          • Ctrl+Shift+i and you can find the script of that link in <body>.
            – chouhy
            Nov 8 at 10:29












          • I edited the code but it's still complaining about syntax where I put 'options'.
            – XCeptable
            Nov 8 at 15:59















          up vote
          0
          down vote










          up vote
          0
          down vote









          When I looked at your code, I found labels were configured, however your timestamp format will never match those labels. As a result, showing 0 makes sense.



          Not sure what do you want. Maybe something like this?
          Take a look at the script they coded, you will find out they didn't configure labels.



          If I were you, I might use scatter plot with showLines=true instead.






          share|improve this answer












          When I looked at your code, I found labels were configured, however your timestamp format will never match those labels. As a result, showing 0 makes sense.



          Not sure what do you want. Maybe something like this?
          Take a look at the script they coded, you will find out they didn't configure labels.



          If I were you, I might use scatter plot with showLines=true instead.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 6:41









          chouhy

          1




          1












          • Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
            – XCeptable
            Nov 8 at 9:36










          • Ctrl+Shift+i and you can find the script of that link in <body>.
            – chouhy
            Nov 8 at 10:29












          • I edited the code but it's still complaining about syntax where I put 'options'.
            – XCeptable
            Nov 8 at 15:59




















          • Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
            – XCeptable
            Nov 8 at 9:36










          • Ctrl+Shift+i and you can find the script of that link in <body>.
            – chouhy
            Nov 8 at 10:29












          • I edited the code but it's still complaining about syntax where I put 'options'.
            – XCeptable
            Nov 8 at 15:59


















          Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
          – XCeptable
          Nov 8 at 9:36




          Thank you very much chouhy ! Yes, something like that in the link I want. The time I want to appear in label horizontal & other parameters over time then in the graph. If the code for the link available, will help a lot. Can you please help edit my code for time to appear in label like in the link. I have basically 15 minutes interval.
          – XCeptable
          Nov 8 at 9:36












          Ctrl+Shift+i and you can find the script of that link in <body>.
          – chouhy
          Nov 8 at 10:29






          Ctrl+Shift+i and you can find the script of that link in <body>.
          – chouhy
          Nov 8 at 10:29














          I edited the code but it's still complaining about syntax where I put 'options'.
          – XCeptable
          Nov 8 at 15:59






          I edited the code but it's still complaining about syntax where I put 'options'.
          – XCeptable
          Nov 8 at 15:59




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53192904%2fchart-js-not-working-uncaught-refernceerror%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()