-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcxContext.js
57 lines (47 loc) · 1.47 KB
/
cxContext.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
import React from "react";
import * as crossfilter from "crossfilter2";
import {csv,timeFormat,timeParse,timeMonth,format} from 'd3'
import "dc/dc.css";
export const CXContext = React.createContext("CXContext");
export const dateFormatSpecifier = '%m/%d/%Y';
export const dateFormat = timeFormat(dateFormatSpecifier);
export const dateFormatParser = timeParse(dateFormatSpecifier);
export const numberFormat = format('.2f');
export class DataContext extends React.Component {
constructor(props) {
super(props);
this.state={loading:false,hasNDX:false};
}
componentDidMount(){
if (this.state.hasNDX){
return
}
if(this.state.loading){
return
}
this.setState({loading:true})
csv('./ndx.csv')
.then((data)=> {
data.forEach(function (d) {
d.dd = dateFormatParser(d.date);
d.month = timeMonth(d.dd); // pre-calculate month for better performance
d.close = +d.close; // coerce to number
d.open = +d.open;
});
this.ndx = crossfilter(data); //TODO possibly need to update this
this.setState({loading:false,hasNDX:true})
})
}
render() {
if(!this.state.hasNDX){
return null;
}
return (
<CXContext.Provider value={{ndx:this.ndx}}>
<div ref={this.parent}>
{this.props.children}
</div>
</CXContext.Provider>
);
}
}