Chartjs with dynamic dataset

Asked

Viewed 303 times

0

I need to create a dataset dynamic where the data will be filled through a query in the database, this data may generate N bars to be plotted then the dataset needs to be created according to information coming from DB.

The number of bars created also depends on the query. It’ll stay that way:

Data 1-> barra 1
Data 2 -> barra 2
Data 3 -> barra 3
Data 1(horario diferente, mesmo ID) -> barra 4....

I’m using the chartsjs plotting.

1 answer

1

Create an array with the information returned in the database and include the array in the dataset parameters.

var data[]; 
var valores[];
//data e valores virão do seu banco.

this.barChart = new Chart(this.barCanvas.nativeElement, {
type: 'bar',
data: {
    labels: "Nome do gráfico",
    datasets: [{                
        label:data,
        data: valores,
        backgroundColor: #00000,
        borderWidth: 0                
    }]
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.