diff --git a/src/Graph.tsx b/src/Graph.tsx index 3b2a7da1a38..5e9add014cb 100644 --- a/src/Graph.tsx +++ b/src/Graph.tsx @@ -14,7 +14,7 @@ interface IProps { * Perspective library adds load to HTMLElement prototype. * This interface acts as a wrapper for Typescript compiler. */ -interface PerspectiveViewerElement { +interface PerspectiveViewerElement extends HTMLElement { load: (table: Table) => void, } @@ -32,7 +32,7 @@ class Graph extends Component { componentDidMount() { // Get element to attach the table from the DOM. - const elem: PerspectiveViewerElement = document.getElementsByTagName('perspective-viewer')[0] as unknown as PerspectiveViewerElement; + const elem = document.getElementsByTagName('perspective-viewer')[0] as unknown as PerspectiveViewerElement; const schema = { stock: 'string', @@ -46,19 +46,24 @@ class Graph extends Component { } if (this.table) { // Load the `table` in the `` DOM reference. - - // Add more Perspective configurations here. elem.load(this.table); + + // Correct the attribute names + elem.setAttribute('view', 'y_line'); + elem.setAttribute('column-pivots', '["stock"]'); + elem.setAttribute('row-pivots', '["timestamp"]'); + elem.setAttribute('columns', '["top_ask_price"]'); + elem.setAttribute('aggregates', ` + {"stock": "distinct count", + "top_ask_price": "avg", + "top_bid_price": "avg"}`); } } componentDidUpdate() { - // Everytime the data props is updated, insert the data into Perspective table + // Filter the new data to avoid duplicates 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, @@ -71,3 +76,4 @@ class Graph extends Component { } export default Graph; +