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 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
6x
2x
2x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
3x
3x
2x
2x
1x
1x
1x
1x
1x
| import React from 'react';
import { Alert, Button, Col, Modal } from 'react-bootstrap';
import Select from 'react-select';
import { Table } from 'reactable';
import shortid from 'shortid';
const CHART_TYPES = [
{ value: 'dist_bar', label: 'Distribution - Bar Chart', requiresTime: false },
{ value: 'pie', label: 'Pie Chart', requiresTime: false },
{ value: 'line', label: 'Time Series - Line Chart', requiresTime: true },
{ value: 'bar', label: 'Time Series - Bar Chart', requiresTime: true },
];
const propTypes = {
onHide: React.PropTypes.func,
query: React.PropTypes.object,
show: React.PropTypes.bool,
};
const defaultProps = {
show: false,
query: {},
onHide: () => {},
};
class VisualizeModal extends React.PureComponent {
constructor(props) {
super(props);
const uniqueId = shortid.generate();
this.state = {
chartType: CHART_TYPES[0],
datasourceName: uniqueId,
columns: {},
hints: [],
};
}
componentDidMount() {
this.validate();
}
componentWillReceiveProps(nextProps) {
this.setStateFromProps(nextProps);
}
setStateFromProps(props) {
if (
!props.query ||
!props.query.results ||
!props.query.results.columns) {
return;
}
const columns = {};
props.query.results.columns.forEach((col) => {
columns[col.name] = col;
});
this.setState({ columns });
}
validate() {
const hints = [];
const cols = this.mergedColumns();
const re = /^\w+$/;
Object.keys(cols).forEach((colName) => {
if (!re.test(colName)) {
hints.push(
<div>
"{colName}" is not right as a column name, please alias it
(as in SELECT count(*) <strong>AS my_alias</strong>) using only
alphanumeric characters and underscores
</div>);
}
});
Iif (this.state.chartType === null) {
hints.push('Pick a chart type!');
} else Iif (this.state.chartType.requiresTime) {
let hasTime = false;
for (const colName in cols) {
const col = cols[colName];
if (col.hasOwnProperty('is_date') && col.is_date) {
hasTime = true;
}
}
if (!hasTime) {
hints.push('To use this chart type you need at least one column flagged as a date');
}
}
this.setState({ hints });
}
changeChartType(option) {
this.setState({ chartType: option }, this.validate);
}
mergedColumns() {
const columns = Object.assign({}, this.state.columns);
Iif (this.props.query && this.props.query.results.columns) {
this.props.query.results.columns.forEach((col) => {
if (columns[col.name] === undefined) {
columns[col.name] = col;
}
});
}
return columns;
}
visualize() {
const vizOptions = {
chartType: this.state.chartType.value,
datasourceName: this.state.datasourceName,
columns: this.state.columns,
sql: this.props.query.sql,
dbId: this.props.query.dbId,
};
window.open('/superset/sqllab_viz/?data=' + JSON.stringify(vizOptions));
}
changeDatasourceName(event) {
this.setState({ datasourceName: event.target.value });
this.validate();
}
changeCheckbox(attr, columnName, event) {
let columns = this.mergedColumns();
const column = Object.assign({}, columns[columnName], { [attr]: event.target.checked });
columns = Object.assign({}, columns, { [columnName]: column });
this.setState({ columns }, this.validate);
}
changeAggFunction(columnName, option) {
let columns = this.mergedColumns();
const val = (option) ? option.value : null;
const column = Object.assign({}, columns[columnName], { agg: val });
columns = Object.assign({}, columns, { [columnName]: column });
this.setState({ columns }, this.validate);
}
render() {
if (!(this.props.query) || !(this.props.query.results) || !(this.props.query.results.columns)) {
return (
<div className="VisualizeModal">
<Modal show={this.props.show} onHide={this.props.onHide}>
<Modal.Body>
No results available for this query
</Modal.Body>
</Modal>
</div>
);
}
const tableData = this.props.query.results.columns.map((col) => ({
column: col.name,
is_dimension: (
<input
type="checkbox"
onChange={this.changeCheckbox.bind(this, 'is_dim', col.name)}
checked={(this.state.columns[col.name]) ? this.state.columns[col.name].is_dim : false}
className="form-control"
/>
),
is_date: (
<input
type="checkbox"
className="form-control"
onChange={this.changeCheckbox.bind(this, 'is_date', col.name)}
checked={(this.state.columns[col.name]) ? this.state.columns[col.name].is_date : false}
/>
),
agg_func: (
<Select
options={[
{ value: 'sum', label: 'SUM(x)' },
{ value: 'min', label: 'MIN(x)' },
{ value: 'max', label: 'MAX(x)' },
{ value: 'avg', label: 'AVG(x)' },
{ value: 'count_distinct', label: 'COUNT(DISTINCT x)' },
]}
onChange={this.changeAggFunction.bind(this, col.name)}
value={(this.state.columns[col.name]) ? this.state.columns[col.name].agg : null}
/>
),
}));
const alerts = this.state.hints.map((hint, i) => (
<Alert bsStyle="warning" key={i}>{hint}</Alert>
));
const modal = (
<div className="VisualizeModal">
<Modal show={this.props.show} onHide={this.props.onHide}>
<Modal.Header closeButton>
<Modal.Title>Visualize</Modal.Title>
</Modal.Header>
<Modal.Body>
{alerts}
<div className="row">
<Col md={6}>
Chart Type
<Select
name="select-chart-type"
placeholder="[Chart Type]"
options={CHART_TYPES}
value={(this.state.chartType) ? this.state.chartType.value : null}
autosize={false}
onChange={this.changeChartType.bind(this)}
/>
</Col>
<Col md={6}>
Datasource Name
<input
type="text"
className="form-control input-sm"
placeholder="datasource name"
onChange={this.changeDatasourceName.bind(this)}
value={this.state.datasourceName}
/>
</Col>
</div>
<hr />
<Table
className="table table-condensed"
columns={['column', 'is_dimension', 'is_date', 'agg_func']}
data={tableData}
/>
<Button
onClick={this.visualize.bind(this)}
bsStyle="primary"
disabled={(this.state.hints.length > 0)}
>
Visualize
</Button>
</Modal.Body>
</Modal>
</div>
);
return modal;
}
}
VisualizeModal.propTypes = propTypes;
VisualizeModal.defaultProps = defaultProps;
export default VisualizeModal;
|