import React, { Component } from 'react';
import {Bar} from 'react-chartjs-2';
import $ from 'jquery';
//NÚMERO DE VIAGENS POR MUNICIPIO
class GeraGrafico extends Component{
constructor(props){
super(props);
this.state = {
listaMedia:[],
listaMunicipio:[]
}
}
componentWillMount(){
console.log("MOUNT");
$.ajax({
url:"http://localhost:10774/sbrp-service/rest/grafico/viagens_municipio",
dataType: 'json',
success:function(resposta){
console.log(resposta);
var i;
for (i = 0; i < resposta.length; i++) {
this.state.listaMedia.push(resposta[i].media);
this.state.listaMunicipio.push(resposta[i].municipio);
}
}.bind(this)
}
);
}
render(){
return(
<div>
<Bar
data={{
labels: this.state.listaMunicipio,
datasets: [
{
data: this.state.listaMedia,
backgroundColor: 'rgba(0, 0, 0, 1)'
}]
}}
options={{
legend:{
display: false
}
}}
/>
</div>
)
}
}
export default class GraficoViagensMunicipio extends Component {
render() {
return (
<div>
<div className="box box-primary">
<div id="ViagemMunicipio" className="box-body">
<GeraGrafico/>
</div>
</div>
</div>
);
}
}
I'm developing an application using React, in which I search the database and graphically present the data on the screen of my application. However, the graph appears empty even with the data being loaded by the back end (I can check this with console.log ()).
After changing the dimensions of the application page (for example, inspecting the element), the data is displayed in the graphic.
Have you been through this? Do you know how I can solve it?