0

Hi I am new to HTML & javascript and trying to build a website.

I am trying to make a histogram plot in HTML by retrieving data from my database, converting them into Json response, and passing it to HTML.

My Json response
{'bins':[b1,b2,b3,b4...], 'number':[n1,n2,n3,n4...]}

The problem is, if the number of data records retrieved is larger than some value (around 100, not strictly tested), the chart will be there, but it is blank.

If the number of records retrieved is fewer, the chart will be displaying normally.

I've checked the JSON response and the response is indeed sent back every time.

<script>
  var endpoint = '/api/data/'
  var defaultData = []
  var labels = []
  $.ajax({
    type: "get",
    url: endpoint,
    success: function(data) {
      labels = data.bins
      defaultData = data.number
      var ctx = document.getElementById("myChart");
      var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
          labels: labels,
          datasets: [{
            label: '# of records',
            data: defaultData
          }]
        }
      })
    },
    error: function(error_data) {
      alert('Error: ajax request error!')
      console.log('error')
      console.log(error_data)
    }
  })
</script>

<div class='row'>
  <div class='col-sm-3' url-endpoint='{% url "api-data" %}'>
    <canvas id='myChart' class="plot" data-url='/api/data' width='400px' , height='400px'> </canvas>
  </div>
</div>

Many thanks!

Updates My apologize for not mentioning the package I've used. I was using Django and chart.js for this chart plotting. And I found the reason of this problem is actually due to the printing of data after plotting the chart. Please see my code below for more information.

<!-- Plotting the chart -->
<script>
  var endpoint = '/api/data/'
  var defaultData = []
  var labels = []
  $.ajax({
    type: "get",
    url: endpoint,
    success: function(data) {
      labels = data.bins
      defaultData = data.number
      var ctx = document.getElementById("myChart");
      var myChart = new Chart(ctx, {
        type: 'bar',
        data: {
          labels: labels,
          datasets: [{
            label: '# of records',
            data: defaultData
          }]
        }
      })
    },
    error: function(error_data) {
      alert('Error: ajax request error!')
      console.log('error')
      console.log(error_data)
    }
  })
</script>

<div class='row'>
  <div class='col-sm-3' url-endpoint='{% url "api-data" %}'>
    <canvas id='myChart' class="plot" data-url='/api/data' width='400px' , height='400px'> </canvas>
  </div>
</div>

<!-- Printing the data in a table -->
<table class="table table-striped" data-toggle="table"
 data-pagination="true"  data-side-pagination="client"  data-height="700" data-show-columns="true" data-search="true" data-show-export="true"  data-click-to-select="true" data-toolbar="#toolbar" data-page-size="10" >

<thead>
    <tr>
      <th>Compound</th>
      <th>Standard name</th>
      <th>Value</th>
      <th>Specifier</th>
      <th>DOI</th>
      <th>Measurement Wavelength (None for not provided or unextracted)</th>
    </tr>
</thead>
 <ul>
       <tbody>
{% for record in records %}
       <tr>
<td>{{ record.compound }}</td>
<td>{{ record.normalised_name }}</td>
<td>{{ record.value }}</td>
<td>{{ record.specifier }}</td>
<td>{{ record.doi }}</td>
<td>{{ record.wavelength }}</td>
       </tr>
{% endfor %}
       </tbody>
<br><br><br>
</ul>
</table>

If I remove the "printing the data in a table" parts, then no matter how many data are retrieved, the chart can be successfully plotted.

It will be greatly appreciated if someone could help me spot the problem of my code and any suggestions of how to improve it will be very helpful.

Jiuyang
  • 1
  • 1
  • Please remember to say what library you are using while asking (I suppose it's ChartJS but it may be something else). Anyway, I'm trying to reproduce your error starting from your code, but it works even with 200+ data and labels. Can you provide more details? – Lykos94 Dec 11 '19 at 22:38
  • @Lykos94 Many thanks for your reply. I am sorry for not mentioning the library. And I've got more findings of my code. Could you please see the "Update" of my post? Thanks again! – Jiuyang Dec 12 '19 at 11:17
  • Thanks for the update, I tried again this time using a Django online IDE to reproduce your code... and it still works. I'm leaving here a link to it so you can check if it is similar to your code/what you want to achieve. Please note the ```template/main/index.html``` and ```main/views.py``` files. Here's the [link](https://repl.it/repls/LowElaborateString) – Lykos94 Dec 12 '19 at 11:49
  • @Lykos94 Thanks for your help. I took a closer look at my case, and it turns to be if the data fed is more than ~360 then the chart doesn't work. And I also found that it is correlated with the total number of cells, i.e. if I only printing one or two columns, then >500 data could be plotted. However, when I put the – Jiuyang Dec 13 '19 at 16:21
  • If that's the case, put your script inside a document.onload function to be sure everything is already loaded. Check this [link](https://stackoverflow.com/questions/588040/window-onload-vs-document-onload) – Lykos94 Dec 13 '19 at 16:42
  • @Lykos94 I've added $ (document).ready(function () {myfunction} ) to my script and it works smoothly now. So I guess that is the case. Thanks again for your help! – Jiuyang Dec 13 '19 at 20:09

0 Answers0