/
widget-google-trends.js
64 lines (55 loc) · 1.39 KB
/
widget-google-trends.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React from "react";
import ReactDOM from "react-dom";
export default class WidgetGoogleTrends extends React.Component {
constructor(props) {
super(props);
this.state = {
word: this.props.word
};
this.widget = this.widget.bind(this);
}
clear() {
var iframes = document.querySelectorAll("iframe");
for (var i = 0; i < iframes.length; i++) {
iframes[i].parentNode.removeChild(iframes[i]);
}
}
componentDidMount() {
this.widget();
}
widget() {
this.clear();
let _ = this;
let script = document.createElement("script");
script.src =
"https://ssl.gstatic.com/trends_nrtr/2213_RC01/embed_loader.js";
script.async = true;
ReactDOM.findDOMNode(_.refs.charts).appendChild(script);
script.onload = function() {
trends.embed.renderExploreWidgetTo(
ReactDOM.findDOMNode(_.refs.charts),
"TIMESERIES",
{
comparisonItem: [
{ keyword: _.state.word, geo: "", time: "today 12-m" }
],
category: 0,
property: ""
},
{
exploreQuery: `q=${_.state.word}&date=today 12-m`,
guestPath: "https://trends.google.com:443/trends/embed/"
}
);
};
}
componentWillReceiveProps(props) {
this.setState({
word: props.word
});
this.widget();
}
render() {
return <div ref="charts" />;
}
}