-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathApp.jsx
More file actions
158 lines (149 loc) · 5.25 KB
/
App.jsx
File metadata and controls
158 lines (149 loc) · 5.25 KB
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
import React from 'react';
import tips from './tips';
import {sortAs} from '../src/Utilities';
import SubtotalRenderers from '../src/SubtotalRenderers';
import TableRenderers from '../src/TableRenderers';
import createPlotlyRenderers from '../src/PlotlyRenderers';
import createPlotlyComponent from 'react-plotly.js/factory';
import PivotTableUI from '../src/PivotTableUI';
import '../src/pivottable.css';
import Dropzone from 'react-dropzone';
import Papa from 'papaparse';
const Plot = createPlotlyComponent(window.Plotly);
class PivotTableUISmartWrapper extends React.PureComponent {
constructor(props) {
super(props);
this.state = {pivotState: props};
}
componentWillReceiveProps(nextProps) {
this.setState({pivotState: nextProps});
}
render() {
return (
<PivotTableUI
renderers={Object.assign(
{},
TableRenderers,
SubtotalRenderers,
createPlotlyRenderers(Plot)
)}
{...this.state.pivotState}
onChange={s => this.setState({pivotState: s})}
unusedOrientationCutoff={Infinity}
/>
);
}
}
export default class App extends React.Component {
componentWillMount() {
this.setState({
mode: 'demo',
filename: 'Sample Dataset: Tips',
pivotState: {
data: tips,
rows: ['Day of Week', 'Party Size'],
cols: ['Payer Gender', 'Meal'],
aggregatorName: 'Sum',
vals: ['Tip'],
rendererName: 'Table With Subtotal',
sorters: {
Meal: sortAs(['Lunch', 'Dinner']),
'Day of Week': sortAs([
'Thursday',
'Friday',
'Saturday',
'Sunday',
]),
},
plotlyOptions: {width: 900, height: 500},
plotlyConfig: {},
tableOptions: {
clickCallback: function(e, value, filters, pivotData) {
var names = [];
pivotData.forEachMatchingRecord(filters, function(
record
) {
names.push(record.Meal);
});
},
},
},
});
}
onDrop(files) {
this.setState(
{
mode: 'thinking',
filename: '(Parsing CSV...)',
textarea: '',
pivotState: {data: []},
},
() =>
Papa.parse(files[0], {
skipEmptyLines: true,
error: e => alert(e),
complete: parsed =>
this.setState({
mode: 'file',
filename: files[0].name,
pivotState: {data: parsed.data},
}),
})
);
}
onType(event) {
Papa.parse(event.target.value, {
skipEmptyLines: true,
error: e => alert(e),
complete: parsed =>
this.setState({
mode: 'text',
filename: 'Data from <textarea>',
textarea: event.target.value,
pivotState: {data: parsed.data},
}),
});
}
render() {
return (
<div>
<div className="row text-center">
<div className="col-md-3 col-md-offset-3">
<p>Try it right now on a file...</p>
<Dropzone
onDrop={this.onDrop.bind(this)}
accept="text/csv"
className="dropzone"
activeClassName="dropzoneActive"
rejectClassName="dropzoneReject"
>
<p>
Drop a CSV file here, or click to choose a file
from your computer.
</p>
</Dropzone>
</div>
<div className="col-md-3 text-center">
<p>...or paste some data:</p>
<textarea
value={this.state.textarea}
onChange={this.onType.bind(this)}
placeholder="Paste from a spreadsheet or CSV-like file"
/>
</div>
</div>
<div className="row text-center">
<p>
<em>Note: the data never leaves your browser!</em>
</p>
<br />
</div>
<div className="row">
<h2 className="text-center">{this.state.filename}</h2>
<br />
<PivotTableUISmartWrapper {...this.state.pivotState} />
</div>
</div>
);
}
}