-
Notifications
You must be signed in to change notification settings - Fork 67
/
keen_viz.js
102 lines (87 loc) · 2.89 KB
/
keen_viz.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from 'react';
import ChartTypeUtils from '../../../utils/ChartTypeUtils';
import FormatUtils from '../../../utils/FormatUtils';
import KeenDataviz from 'keen-dataviz';
import moment from 'moment-timezone';
const KeenViz = React.createClass({
lastDataTimestamp: null,
lastChartType: null,
datavizInstance: null,
// ***********************
// Convenience functions
// ***********************
showVisualization: function() {
if (this.datavizInstance) {
this.datavizInstance.destroy();
}
let sortGroups;
if (this.props.model.query.analysis_type !== "funnel") {
sortGroups = 'desc';
}
let type;
if (this.props.model.metadata.visualization.chart_type) {
type = this.props.model.metadata.visualization.chart_type;
}
let results = this.props.model.response;
if (Array.isArray(results.result)
&& results.result[0].timeframe && results.result[0].timeframe.start
&& !this.props.model.response.dateConvertedToTimezone
&& type !== 'table'
){
this.props.model.response.dateConvertedToTimezone = true;
const dateFormat = 'YYYY-MM-DDTHH:mm:ss.000Z';
results.result.forEach((result, key) => {
results.result[key].timeframe.start = moment(result.timeframe.start).tz(results.query.timezone).format(dateFormat);
results.result[key].timeframe.end = moment(result.timeframe.end).tz(results.query.timezone).format(dateFormat);
});
}
this.datavizInstance = new KeenDataviz({
container: this.refs['keen-viz'],
showTitle: false,
type,
sortGroups,
results,
...((this.props.config && this.props.config.keenDatavizOptions) || {})
});
this.lastDataTimestamp = this.props.model.dataTimestamp;
this.lastChartType = this.props.model.metadata.visualization.chart_type;
},
// ***********************
// Lifecycle hooks
// ***********************
shouldComponentUpdate: function(nextProps, nextState) {
if (this.lastChartType !== nextProps.model.metadata.visualization.chart_type) {
return true;
}
if (!this.lastDataTimestamp || this.lastDataTimestamp !== nextProps.model.dataTimestamp) {
return true
}
return false;
},
componentDidUpdate: function() {
this.showVisualization();
},
componentDidMount: function() {
this.showVisualization();
},
render: function() {
var exportBtn;
if (ChartTypeUtils.isTableChartType(this.props.model.metadata.visualization.chart_type)) {
exportBtn = (
<button className="btn btn-default btn-download-csv"
role="export-table"
type="button"
onClick={() => this.props.exportToCsv(this.datavizInstance)}>
Download CSV
</button>
);
}
return (
<div ref="keen-viz-wrapper">
<div ref="keen-viz"></div>
{exportBtn}
</div>
);
}
});
export default KeenViz;