forked from vagabond-systems/jpmc-task-2
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Expand file tree
/
Copy pathGraph.tsx
More file actions
80 lines (71 loc) · 2.28 KB
/
Graph.tsx
File metadata and controls
80 lines (71 loc) · 2.28 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
import React, { Component } from 'react';
import { Table } from '@finos/perspective';
import { ServerRespond } from './DataStreamer';
import './Graph.css';
/**
* Props declaration for <Graph />
*/
interface IProps {
data: ServerRespond[],
}
/**
* Perspective library adds load to HTMLElement prototype.
* This interface acts as a wrapper for Typescript compiler.
*/
interface PerspectiveViewerElement extends HTMLElement{
load: (table: Table) => void,
}
/**
* React component that renders Perspective based on data
* parsed from its parent through data property.
*/
class Graph extends Component<IProps, {}> {
// Perspective table
table: Table | undefined;
render() {
return React.createElement('perspective-viewer');
}
componentDidMount() {
// Get element to attach the table from the DOM.
const elem = document.getElementsByTagName('perspective-viewer')[0] as unknown as PerspectiveViewerElement;
const schema = {
stock: 'string',
top_ask_price: 'float',
top_bid_price: 'float',
timestamp: 'date',
};
if (window.perspective && window.perspective.worker()) {
this.table = window.perspective.worker().table(schema);
}
if (this.table) {
elem.setAttribute('view', 'y_line');
elem.setAttribute('column-pivots', '["stock"]');
elem.setAttribute('row-pivots', '["timestamp"]');
elem.setAttribute('columns', '["top_ask_price"]');
elem.setAttribute('aggregates', JSON.stringify({
stock: "distinct count",
top_ask_price: "avg",
top_bid_price: "avg",
timestamp: "distinct count"
}));
elem.load(this.table);
}
}
componentDidUpdate() {
// Everytime the data props is updated, insert the data into Perspective table
if (this.table) {
// As part of the task, you need to fix the way we update the data props to
// avoid inserting duplicated entries into Perspective table again.
this.table.update(this.props.data.map((el: any) => {
// Format the data from ServerRespond to the schema
return {
stock: el.stock,
top_ask_price: el.top_ask && el.top_ask.price || 0,
top_bid_price: el.top_bid && el.top_bid.price || 0,
timestamp: el.timestamp,
};
}));
}
}
}
export default Graph;