1
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 am developing an application using React, in which I do a search in a database and graphically present the data on the screen of my application. However, the chart appears empty even though the data is loaded by the back end (I can check this with consolation.log()).
After changing the dimensions of the application page (for example, inspecting the element) the data is shown in the graph.
Have you been there? You know how I can fix?
Thank you for your attention Pedro. I published the code of my component next to the question.
– João Marcos Soares