Skip to content

Update Dataset by Label name #10222

Answered by LeeLenaleee
ImJustBull asked this question in Q&A
Discussion options

You must be logged in to vote

You can use the find method to get the correct dataset by comparing the label. Then you can update it:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderColor: 'pink'
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderColor: 'orange'
      }
    ]
  },
  options: {}
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
const chart = new Chart(ctx, options);

setTimeout(() => {
  chart.data.datasets.find(e => e.label === '# of Points').data[0] = 50;
  chart.update();
}, 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by ImJustBull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants