-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_page.jsx
253 lines (234 loc) · 7.56 KB
/
data_page.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
244
245
246
247
248
249
250
251
252
253
var React = require("react");
var _ = require("lodash");
var $ = require("jquery");
var AppStore = require("../store/app_store");
var Chart = require("./chart.jsx");
var CSV = require("babyparse");
var stats = require("simple-statistics");
var UploadButton = React.createClass({
componentDidMount: function() {
var schemaName = this.props.schemaName;
function fileHandler() {
var file = this.files.item(0);
AppStore.uploadData(file.name, file, schemaName);
}
var inputElement = document.getElementById("input");
inputElement.addEventListener("change", fileHandler, false);
},
render: function() {
return (
<div className="upload-button">
<span id="input">Upload .csv</span>
<input className="upload" type="file"></input>
</div>
);
}
});
var DownloadButton = React.createClass({
render: function() {
return (
<div className="download-button">
<a href={this.props.url} download={this.props.name}
type="submit"><div>Download All</div></a>
</div>
);
}
});
var TableRows = React.createClass({
statics: {
getRowClass: function(index) {
return index % 2 === 0 ? "even-row" : "odd-row";
},
getColumnClass: function(index) {
return index % 2 === 0 ? "even-column" : "odd-column";
}
},
render: function() {
return (
<tbody className="table-rows">
{
_.map(this.props.rowData, function(row, index) {
return (
<tr className={TableRows.getRowClass(index)} key={index}>
{
_.map(row, function(rowItem, index) {
return <td className={TableRows.getColumnClass(index)} key={index}>
<div>{rowItem}</div></td>;
})
}
</tr>
);
})
}
</tbody>
);
}
});
var TableHeadersRow = React.createClass({
componentDidMount: function() {
var schemaCSVData = this.props.schemaCSVData;
var that = this;
function renderCSVHeader() {
that.props.setActiveHeader(this.id);
}
var tableHeaders = document.getElementsByClassName("csv-header");
_.map(tableHeaders, function(header, index) {
if (index > 1) {
header.addEventListener("click", renderCSVHeader, false);
}
});
},
statics: {
getHeaderClass: function(index) {
return index % 2 === 0 ? "even-header csv-header" : "odd-header csv-header";
}
},
render: function() {
return (
<tr className="table-headers">
{
_.map(this.props.headerKeys, function(header, index) {
return <th id={index} key={index}
className={TableHeadersRow.getHeaderClass(index)}>{header}</th>;
})
}
</tr>
);
}
});
var Table = React.createClass({
getInitialState: function() {
return {
activeHeader: 2,
chartType: "timeSeries"
};
},
statics: {
getToolset: function(chartType, headerlessCSV, headerIndex) {
var columnArray = headerlessCSV.map(function(row) {
return parseInt(row[headerIndex], 10);
});
var toolset = {
Mean: Math.floor(stats.mean(columnArray)),
Median: Math.floor(stats.median(columnArray)),
Mode: Math.floor(stats.mode(columnArray)),
};
return toolset;
}
},
componentDidMount: function() {
var that = this;
/*
Eventually the server will batch together all the csv's into one file using hadoop.
For now I am just getting one users file.
*/
var getSchema = function(schemaName, callback) {
$.ajax({
url: "/api/schema/" + schemaName + "/entriesMetadata",
type: "GET",
beforeSend: function(request) {
request.setRequestHeader("x-jwt", localStorage.getItem("token"));
},
success: function(schemaData) {
callback(schemaData);
},
error: function(error) {
console.error(error);
}
});
};
var schemaName = this.props.schemaName;
getSchema(schemaName, function(schemaData) {
$.ajax({
url: "/api/schema/" + schemaName + "/userID/" + schemaData[0].userID,
type: "GET",
beforeSend: function(request) {
request.setRequestHeader("x-jwt", localStorage.getItem("token"));
},
success: function(data) {
var parsedCSV = CSV.parse(data);
AppStore.updateCSVData(parsedCSV.data);
AppStore.addTools(Table.getToolset(that.state.chartType, _.rest(parsedCSV.data), that.state.activeHeader));
},
error: function() {
console.error("GET request for schema data failed.");
}
});
});
//TODO: Make an array of chart names, and map over it to apply events (185)
var chartEvents = [
["#to-time-series", "timeSeries"],
["#to-histogram", "histogram"],
["#to-scatter-plot", "scatterPlot"]
];
chartEvents.forEach(function(chartEvent){
var chartID = chartEvent[0];
var chartType = chartEvent[1];
$(document).on("click", chartID, function() {
that.setState({chartType: chartType});
});
});
},
componentWillUnmount: function() {
$(document).off("click", "#to-time-series");
$(document).off("click", "#to-histogram");
$(document).off("click", "#to-scatter-plot");
},
render: function() {
var that = this;
var setActiveHeader = function(newHeaderIndex) {
var headerlessCSV = _.rest(that.props.schemaCSVData);
that.setState({activeHeader: newHeaderIndex});
AppStore.addTools(Table.getToolset(that.state.chartType, headerlessCSV, that.state.activeHeader));
};
var rows = this.props.schemaCSVData;
if (rows && Array.isArray(rows[0])) {
var headerKeys = rows[0].map(function(row) {
return AppStore.formatCSVHeader(row);
});
return (
<div className="data-page">
<aside className="tools">
<input className="data-search" placeholder="Search schemas" type="text"/>
<section className="analysis-dashboard">
<ul className="select-chart">Visualizations
<li id="to-time-series">Time Series</li>
<li id="to-histogram">Histogram</li>
<li id="to-scatter-plot">Scatter Plot</li>
</ul>
<ul className="analysis-tools">Analysis Tools
{
_.map(this.props.tools, function(val, key) {
return <li>{key}: {val}</li>;
})
}
</ul>
</section>
</aside>
<section className="viz-and-table-views">
<div className="visualization-block">
<div className="visualization-label">{this.props.schemaName}</div>
<UploadButton schemaName={this.props.schemaName}/>
<DownloadButton />
<Chart schemaName={this.props.schemaName}
csvData={this.props.schemaCSVData}
activeHeader={this.state.activeHeader}
chartType={this.state.chartType}/>
</div>
<div className="table-view">
<table className="schema-csv-table">
<TableHeadersRow schemaCSVData={this.props.schemaCSVData}
headerKeys={headerKeys}
setActiveHeader={setActiveHeader}/>
<TableRows rowData={_.rest(rows)}/>
</table>
</div>
</section>
</div>
);
} else {
return <div/>;
}
}
});
module.exports = Table;