Topic: Creating a bar chart using a variable for the data set

hemeleerse free asked 5 years ago


To start, let me admit I'm pretty new into programming...I want to create a bar chart where the data set is read from a variable (array).That array is basically json data appended to an empty array using a for loop.

My issue: although the array is correct (which I checked in the console), the data is not displayed. The chart remains empty. Why?

Here's an extract of the code:

<script>
var MyArray = [];
var MyArrayLength = 0;
$(document).ready(function(){
    $.getJSON('dayjson',function(data){
        for ( var counter=data.measures.length; counter >=1 ; counter--) {
        MyArrayLength = MyArray.push((data.measures[counter-1].HourUse));
        };
    });
});
console.log(MyArray);
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
      type: 'bar',
      data: {
          labels: ["00-01","01-02","02-03","03-04","04-05","05-06","06-07","07-08","08-09","09-10","10-11","11-12","12-13","13-14","14-15","15-16","16-17","17-18","18-19","19-20","20-21","21-22","22-23","23-00"],
          datasets: [{
              label: 'Water use today',
              data: MyArray,
              backgroundColor: [
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)',
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)',
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)',
                  'rgba(255, 99, 132, 0.2)',
                  'rgba(54, 162, 235, 0.2)',
                  'rgba(255, 206, 86, 0.2)',
                  'rgba(75, 192, 192, 0.2)',
                  'rgba(153, 102, 255, 0.2)',
                  'rgba(255, 159, 64, 0.2)'
              ],
              borderColor: [
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(255,99,132,1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(255,99,132,1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(255,99,132,1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)',
                  'rgba(54, 162, 235, 1)',
                  'rgba(255, 206, 86, 1)',
                  'rgba(255,99,132,1)',
                  'rgba(75, 192, 192, 1)',
                  'rgba(153, 102, 255, 1)',
                  'rgba(255, 159, 64, 1)'
              ],
              borderWidth: 1
          }]
      },
      options: {
          scales: {
              yAxes: [{
                  ticks: {
                      beginAtZero:true
                  },
                  scaleLabel: {
                    display: true,
                    labelString: 'use (liter)'
                  }
              }],
              xAxes: [{
                  scaleLabel: {
                    display: true,
                    labelString: 'hour interval'
                  }
              }]
          }
      }
  });
</script>

Piotr Glejzer staff commented 5 years ago

Hi, may you try to fix path to your json file? I admit that 'dayjson' is json so maybe is wrong path. Just try something like that : dayjson.json, day.json or ./dayjson.json or ./day.json

Best, Piotr


hemeleerse free commented 5 years ago

Hi, the path to the json is OK as, using a console log of variable MyArray, the data is there. So the question is: how come the array is ok in the console log while the chart displays no data at all?


Piotr Glejzer staff commented 5 years ago

I don't see the construction of JSON file so I can't do anything because your scripts are looking good.



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Opened

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: General Bootstrap questions
  • MDB Version: -
  • Device: PC
  • Browser: Chrome
  • OS: Windows
  • Provided sample code: No
  • Provided link: No