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

236
Views
how to dynamically populate datasets using chart.js

I am using a bar chart js for the first time that i want to populate from an array data. this is my array

this.feedBackChart=[{name:"test1",id:"1",score:"5"}
{name:"test1",id:"1",score:"7"}
{name:"test2",id:2,score:"4"}]

i would like to populate two datasets dynamically one for test1 and one for test 2. and then add the scores for test1 which will be 12 and test2 will be 4.

this is what i tried but nothing gets shown on the page with no errors as well

async chart() {
        
        var barChartData = {
            datasets: []
        };
        this.feedbackChart = [];
        this.feedback.map(item => {
            let m = this.feedbackResult.find(x => x.id == item.Id);

            this.feedbackChart.push({
                ["name"]: m.metricName,
                ["id"]:item.id,
                ["score"]: item.score // im not sure how to sum up the scores here

            });
        });
        for (var i = 0; i < this.feedbackChart.length; i++) {
            barChartData.datasets.push({
                label: this.feedbackChart.map(x=> x.name),
                backgroundColor: 'rgb(255, 99, 132)',
                borderColor: 'rgb(255, 99, 132)',
                data:this.feedbackChart.map(x=> x.score),
            });
        }
        this.data = {
            labels: moment.months(),
            datasets: barChartData
                
            
        };

        this.config = {
            type: 'bar',
            data: this.data,
            options: {}
        };
        this.mychart = new Chart(document.getElementById("myChart") as HTMLCanvasElement, this.config);
    }

if i manually load the data as follows for testing purposes it works.but i cant do it this way cause the length of my array will always change

this.data = {
            labels: moment.months(),
            
            datasets: [{
            label: 'test1',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [12],
            },
            {
            label: 'test2',
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: [4],
            }
]
            
        };
about 4 years ago · Juan Pablo Isaza
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!