trying to include JSON data from a file in my canvas.js for an ejs template

50 views Asked by At

I am an amateur trying to build a template that will use a local JSON file to populate data into an ejs template and data for a chart.

I have a working ejs template that renders most of the JSON data I require except for the chart data. I mean I can load the chart data if I provide a variable with the direct link to the JSON data I require but I want to replace a link in the variable used to retrive the JSON data, this would allow the chart to follow the rest of the data in the template.

I currently have extracted the link I require with getElementbyID and innerText by creating the variable 'elem', I just don't know how to plug it into the 'dataPoints' variable.

fetch ('/officialresults.json')
.then(function(u) { return u.json();}
).then(
  function(json){
   
    const jsondata = json
    const datapoints = jsondata.elem.VoteByTime
  const elem = document.getElementById("text").innerText;
  const ctx = document.getElementById('myChart');
  
  new Chart(ctx, {
    type: 'line',
    data: {
      labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
      datasets: [{
        label: '# of Votes',
        data: datapoints,
        borderWidth: 1
      }]
    },
    options: {
      scales: {
        y: {
          beginAtZero: true
        }
      }
    }
  });
  
}
)

0

There are 0 answers