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

225
Vistas
Adding an additional datasets to chartJS

I got this chart to create

bar chart to create in chartJS

I've successfully created the 2019 part with 5 points of colorful data inside. What I have so far:

enter image description here

I'm trying to add 2020 and 2021 data. Here's my code:

const DATA_COUNT = 3;
const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 70};

const labels = ['2019', '2020', '2021'];
const data = {
  labels: labels,
  datasets: [
    {
      label: 'AUD',
      data: ['51.01'],
      borderColor: '#3088E4',
      backgroundColor: '#3088E4',
    },
    {
      label: 'BEC',
      data: ['59.98'],
      borderColor: '#734C9D',
      backgroundColor: '#734C9D',
    },
    {
      label: 'FAR',
      data: ['46.31'],
      borderColor: '#4FA333',
      backgroundColor: '#4FA333',
    },
    {
      label: 'FAR',
      data: ['56.34'],
      borderColor: '#EC990F',
      backgroundColor: '#EC990F',
    },
    {
      label: 'Cumulative',
      data: ['53.41'],
      borderColor: '#999999',
      backgroundColor: '#999999',
    }
  ]
};

It can be tested live here, by pasting it under "Setup": https://www.chartjs.org/docs/latest/samples/bar/vertical.html

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can pass multiple values inside the data.datasets[x].data attributes

const DATA_COUNT = 3;
const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 70};

const labels = ['2019', '2020', '2021'];
const data = {
  labels: labels,
  datasets: [
    {
      label: 'AUD',
      data: [51.01, 50.02, 45],
      borderColor: '#3088E4',
      backgroundColor: '#3088E4',
    },
    {
      label: 'BEC',
      data: [59.98, 40.05, 56],
      borderColor: '#734C9D',
      backgroundColor: '#734C9D',
    },
    {
      label: 'FAR',
      data: [46.31, 34.57, 78],
      borderColor: '#4FA333',
      backgroundColor: '#4FA333',
    },
    {
      label: 'FAR',
      data: [56.34, 78, 35],
      borderColor: '#EC990F',
      backgroundColor: '#EC990F',
    },
    {
      label: 'Cumulative',
      data: [53.41, 24, 23],
      borderColor: '#999999',
      backgroundColor: '#999999',
    }
  ]
};

const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
    type: 'bar',
    data,
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.8.0/dist/chart.min.js"></script>

<canvas id="myChart" width="400" height="400"></canvas>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Got it figured out. It was a matter of just adding data points right inside under 'data':

const DATA_COUNT = 3;
const NUMBER_CFG = {count: DATA_COUNT, min: 0, max: 70};

const labels = ['2019', '2020', '2021'];
const data = {
  labels: labels,
  datasets: [
    {
      label: 'AUD',
      data: ['51.01', '50.49', '47.98'],
      borderColor: '#3088E4',
      backgroundColor: '#3088E4',
    },
    {
      label: 'BEC',
      data: ['59.98', '62.56', '61.94'],
      borderColor: '#734C9D',
      backgroundColor: '#734C9D',
    },
    {
      label: 'FAR',
      data: ['46.31', '49.98', '44.54'],
      borderColor: '#4FA333',
      backgroundColor: '#4FA333',
    },
    {
      label: 'REG',
      data: ['56.34', '62.29', '59.88'],
      borderColor: '#EC990F',
      backgroundColor: '#EC990F',
    },
    {
      label: 'Cumulative',
      data: ['53.41', '57.67', '53.59'],
      borderColor: '#999999',
      backgroundColor: '#999999',
    }
  ]
};

enter image description here

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