Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda