forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Chart.jsx
243 lines (213 loc) · 7.25 KB
/
Chart.jsx
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import cx from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { exportChart } from '../../../explore/exploreUtils';
import SliceHeader from '../SliceHeader';
import ChartContainer from '../../../chart/ChartContainer';
import { chartPropType } from '../../../chart/chartReducer';
import { slicePropShape } from '../../util/propShapes';
import { VIZ_TYPES } from '../../../visualizations';
const propTypes = {
id: PropTypes.number.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
updateSliceName: PropTypes.func.isRequired,
// from redux
chart: PropTypes.shape(chartPropType).isRequired,
formData: PropTypes.object.isRequired,
datasource: PropTypes.object.isRequired,
slice: slicePropShape.isRequired,
sliceName: PropTypes.string.isRequired,
timeout: PropTypes.number.isRequired,
filters: PropTypes.object.isRequired,
refreshChart: PropTypes.func.isRequired,
toggleExpandSlice: PropTypes.func.isRequired,
addFilter: PropTypes.func.isRequired,
editMode: PropTypes.bool.isRequired,
isExpanded: PropTypes.bool.isRequired,
supersetCanExplore: PropTypes.bool.isRequired,
sliceCanEdit: PropTypes.bool.isRequired,
};
// we use state + shouldComponentUpdate() logic to prevent perf-wrecking
// resizing across all slices on a dashboard on every update
const RESIZE_TIMEOUT = 350;
const SHOULD_UPDATE_ON_PROP_CHANGES = Object.keys(propTypes).filter(
prop => prop !== 'width' && prop !== 'height',
);
const OVERFLOWABLE_VIZ_TYPES = new Set([VIZ_TYPES.filter_box]);
class Chart extends React.Component {
constructor(props) {
super(props);
this.state = {
width: props.width,
height: props.height,
};
this.addFilter = this.addFilter.bind(this);
this.exploreChart = this.exploreChart.bind(this);
this.exportCSV = this.exportCSV.bind(this);
this.forceRefresh = this.forceRefresh.bind(this);
this.getFilters = this.getFilters.bind(this);
this.resize = this.resize.bind(this);
this.setDescriptionRef = this.setDescriptionRef.bind(this);
this.setHeaderRef = this.setHeaderRef.bind(this);
}
shouldComponentUpdate(nextProps, nextState) {
// this logic mostly pertains to chart resizing. we keep a copy of the dimensions in
// state so that we can buffer component size updates and only update on the final call
// which improves performance significantly
if (
nextState.width !== this.state.width ||
nextState.height !== this.state.height
) {
return true;
}
for (let i = 0; i < SHOULD_UPDATE_ON_PROP_CHANGES.length; i += 1) {
const prop = SHOULD_UPDATE_ON_PROP_CHANGES[i];
if (nextProps[prop] !== this.props[prop]) {
return true;
}
}
if (
nextProps.width !== this.props.width ||
nextProps.height !== this.props.height
) {
clearTimeout(this.resizeTimeout);
this.resizeTimeout = setTimeout(this.resize, RESIZE_TIMEOUT);
}
return false;
}
componentWillUnmount() {
clearTimeout(this.resizeTimeout);
}
getFilters() {
return this.props.filters;
}
getChartHeight() {
const headerHeight = this.getHeaderHeight();
const descriptionHeight =
this.props.isExpanded && this.descriptionRef
? this.descriptionRef.offsetHeight
: 0;
return this.state.height - headerHeight - descriptionHeight;
}
getHeaderHeight() {
return (this.headerRef && this.headerRef.offsetHeight) || 30;
}
setDescriptionRef(ref) {
this.descriptionRef = ref;
}
setHeaderRef(ref) {
this.headerRef = ref;
}
resize() {
const { width, height } = this.props;
this.setState(() => ({ width, height }));
}
addFilter(...args) {
this.props.addFilter(this.props.chart, ...args);
}
exploreChart() {
exportChart(this.props.formData);
}
exportCSV() {
exportChart(this.props.formData, 'csv');
}
forceRefresh() {
return this.props.refreshChart(this.props.chart, true, this.props.timeout);
}
render() {
const {
id,
chart,
slice,
datasource,
isExpanded,
editMode,
formData,
updateSliceName,
sliceName,
toggleExpandSlice,
timeout,
supersetCanExplore,
sliceCanEdit,
} = this.props;
// this should never happen but prevents throwing in the case that a gridComponent
// references a chart that is not associated with the dashboard
if (!chart || !slice) return null;
const { width } = this.state;
const { queryResponse, chartUpdateEndTime } = chart;
const isCached = queryResponse && queryResponse.is_cached;
const cachedDttm = queryResponse && queryResponse.cached_dttm;
const isOverflowable = OVERFLOWABLE_VIZ_TYPES.has(slice && slice.viz_type);
return (
<div>
<SliceHeader
innerRef={this.setHeaderRef}
slice={slice}
isExpanded={!!isExpanded}
isCached={isCached}
cachedDttm={cachedDttm}
updatedDttm={chartUpdateEndTime}
toggleExpandSlice={toggleExpandSlice}
forceRefresh={this.forceRefresh}
editMode={editMode}
annotationQuery={chart.annotationQuery}
exploreChart={this.exploreChart}
exportCSV={this.exportCSV}
updateSliceName={updateSliceName}
sliceName={sliceName}
supersetCanExplore={supersetCanExplore}
sliceCanEdit={sliceCanEdit}
/>
{/*
This usage of dangerouslySetInnerHTML is safe since it is being used to render
markdown that is sanitized with bleach. See:
https://github.com/apache/incubator-superset/pull/4390
and
https://github.com/apache/incubator-superset/commit/b6fcc22d5a2cb7a5e92599ed5795a0169385a825
*/}
{isExpanded &&
slice.description_markeddown && (
<div
className="slice_description bs-callout bs-callout-default"
ref={this.setDescriptionRef}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: slice.description_markeddown }}
/>
)}
<div
className={cx(
'dashboard-chart',
isOverflowable && 'dashboard-chart--overflowable',
)}
>
<ChartContainer
containerId={`slice-container-${id}`}
chartId={id}
datasource={datasource}
formData={formData}
headerHeight={this.getHeaderHeight()}
height={this.getChartHeight()}
width={width}
timeout={timeout}
vizType={slice.viz_type}
addFilter={this.addFilter}
getFilters={this.getFilters}
annotationData={chart.annotationData}
chartAlert={chart.chartAlert}
chartStatus={chart.chartStatus}
chartUpdateEndTime={chart.chartUpdateEndTime}
chartUpdateStartTime={chart.chartUpdateStartTime}
latestQueryFormData={chart.latestQueryFormData}
lastRendered={chart.lastRendered}
queryResponse={chart.queryResponse}
queryRequest={chart.queryRequest}
triggerQuery={chart.triggerQuery}
/>
</div>
</div>
);
}
}
Chart.propTypes = propTypes;
export default Chart;