I am trying to use google charts in an ReactJS application. I do not want to use "react-google-charts".
I already searched the whole internet and this is my current code...
In my index.html I do:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload=
{'modules':[{'name':'visualization','version':'1.1','packages':
['corechart']}]}"></script>
And my component to use the chart looks like this:
import React from 'react';
import {MainViewHeader} from './MainViewHeader';
import TouristService from '../../services/TouristService';
import Util from '../../services/Util';
google.charts.load('current', {'packages':['corechart']});
export class MainView extends React.Component {
constructor() {
super();
this.state = {
touristInfos: [],
touristInfosHeader: []
};
google.charts.setOnLoadCallback(() => this.renderGoogleTable());
}
componentDidMount() {
let touristInfos = TouristService.getAllTouristInfos();
touristInfos.then(
info =>
this.setState({
touristInfos: Util.getResults(info)
})
);
//this.renderGoogleTable();
}
componentDidUpdate(){
this.renderGoogleTable();
}
renderGoogleTable(){
let data = google.visualization.arrayToDataTable(this.state.touristInfos);
let table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {showRowNumber: true, width: '100%', height: '100%'});
}
render() {
return (
<div className="search-and-overview">
<MainViewHeader />
<div className="mid-region">
<div className="content">
<div className="problem-list">
<div id="table_div"></div>
</div>
</div>
</div>
</div>
);
}
}
I have no clue where or how to use the google.charts.setOnLoadCallback(myGraphDrawingFunction) so I tried this version I found on a stackoverflow post for using google charts in angular.
I need to use this in the renderGoogleTable function. So when I use the "regular setOnLoadCallback way" I cannot use this anymore.
My chrome dev tools throw this error
MainView.js:36 Uncaught (in promise) TypeError: google.visualization.Table is not a constructor
at MainView.renderGoogleTable (MainView.js:36)
at MainView.js:15
at <anonymous>