Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Frontend and backend for chart using chartjs, mongodb, and controller

I am new to generating a chart using the ajax mechanism and chartjs. It seems that I managed to generate the graph but they are plotted incorrectly. Please enlighten me on how I could improve. Would really appreciate Thank you

Following is my code in javascript to plot chart

      var ctx = document.getElementById('myChart').getContext('2d');
    
      $.ajax({
        url:"/dashboard",
        type:"POST",
        data: {},
        error: function() {
            alert("Error");
        },
        success: function(data, status, xhr) {
    
          debugger
          // declare all variables to draw chart
          var chartDim = {};      
          var chartDim = data.chartDim;
          var xLabels = data.labels;
    
          var vLabels = [];
          var vData = [];
    
          let newValues =[]
          // get from database to push to draw the chart
          for (const [key, values] of Object.entries(chartDim)) {
            vLabels.push(key);
            let newValues = values.map(myFunction);
            debugger;
            vData.push(newValues);
          } 
    
          debugger
          var myChart = new Chart(ctx, {
            data: {
            labels: xLabels,
            datasets: []
            },
            options: {
                responsive: false
            }
          });
          // draw to chart 
          debugger
          for (i= 0; i < vLabels.length; i++ ) {
            myChart.data.datasets.push({
            label: vLabels[i],
            type: "bar",
            // borderColor: '#'+(0x1ff0000+Math.random()*0xffffff).toString(16).substr(1,6),
            borderColor: '#'+(0x1100000+Math.random()*0xffffff).toString(16).substr(1,6),
            backgroundColor: "rgba(249, 238, 236, 0.74)",
            data: vData[i],
            spanGaps: true
            });
            myChart.update();
          }
    
      }})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This happens because while making your chart you push each data point as a new dataset. Instead you just need to use a single dataset and pass the data array to it like so:

var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: xLabels,
    datasets: [{
      borderColor: '#' + (0x1100000 + Math.random() * 0xffffff).toString(16).substr(1, 6),
      backgroundColor: "rgba(249, 238, 236, 0.74)",
      data: vData,
      spanGaps: true
    }]
  },
  options: {
    responsive: false
  }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!