From 8b39605902cb0ae0db75c55bbd8086bc27a8b6be Mon Sep 17 00:00:00 2001 From: Mark McDowell Date: Thu, 3 Sep 2020 17:10:02 +0100 Subject: [PATCH] feat(stories): adding updating data example Testing updates with continuous data scale. --- packages/stories/src/data/index.ts | 1 + .../stories/src/data/withUpdatingData.tsx | 54 +++++++++++ .../src/features/updating/BasicLineSeries.tsx | 95 +++++++++++++++++++ .../src/features/updating/index.stories.tsx | 8 ++ 4 files changed, 158 insertions(+) create mode 100644 packages/stories/src/data/withUpdatingData.tsx create mode 100644 packages/stories/src/features/updating/BasicLineSeries.tsx create mode 100644 packages/stories/src/features/updating/index.stories.tsx diff --git a/packages/stories/src/data/index.ts b/packages/stories/src/data/index.ts index 91d954119..3251bfa4f 100644 --- a/packages/stories/src/data/index.ts +++ b/packages/stories/src/data/index.ts @@ -1,2 +1,3 @@ export * from "./iOHLCData"; export * from "./withOHLCData"; +export * from "./withUpdatingData"; diff --git a/packages/stories/src/data/withUpdatingData.tsx b/packages/stories/src/data/withUpdatingData.tsx new file mode 100644 index 000000000..86f613b87 --- /dev/null +++ b/packages/stories/src/data/withUpdatingData.tsx @@ -0,0 +1,54 @@ +import * as React from "react"; +import { IOHLCData } from "./iOHLCData"; + +interface WithOHLCDataProps { + readonly data: IOHLCData[]; +} + +interface WithOHLCState { + data: IOHLCData[]; + length: number; +} + +export function withUpdatingData(initialLength = 120, interval = 1_000) { + return (OriginalComponent: React.ComponentClass) => { + return class WithOHLCData extends React.Component { + public interval?: number; + + public constructor(props: TProps) { + super(props); + + this.state = { + data: props.data.slice(0, initialLength), + length: initialLength, + }; + } + + public componentDidMount() { + this.interval = window.setInterval(() => { + const { data } = this.props; + const { length } = this.state; + + if (length < data.length) { + this.setState({ + data: data.slice(0, length + 1), + length: length + 1, + }); + } else { + window.clearInterval(this.interval); + } + }, interval); + } + + public componentWillUnmount() { + window.clearInterval(this.interval); + } + + public render() { + const { data } = this.state; + + return ; + } + }; + }; +} diff --git a/packages/stories/src/features/updating/BasicLineSeries.tsx b/packages/stories/src/features/updating/BasicLineSeries.tsx new file mode 100644 index 000000000..775598d9e --- /dev/null +++ b/packages/stories/src/features/updating/BasicLineSeries.tsx @@ -0,0 +1,95 @@ +import { scaleTime } from "d3-scale"; +import * as React from "react"; +import { + CandlestickSeries, + Chart, + ChartCanvas, + CrossHairCursor, + timeFormat, + withDeviceRatio, + withSize, + XAxis, + YAxis, +} from "react-financial-charts"; +import { IOHLCData, withUpdatingData, withOHLCData } from "../../data"; + +interface ChartProps { + readonly data: IOHLCData[]; + readonly height: number; + readonly width: number; + readonly ratio: number; +} + +class BasicLineSeries extends React.Component { + private readonly margin = { left: 0, right: 56, top: 0, bottom: 24 }; + private readonly padding = { left: 0, right: 32, top: 0, bottom: 0 }; + + public render() { + const { data, height, ratio, width } = this.props; + + const xScale = scaleTime(); + const xAccessor = (d: IOHLCData) => d.date; + + return ( + + + + + + + + + ); + } + + private readonly yExtentsCalculator = ({ plotData }: { plotData: IOHLCData[] }) => { + let min: number | undefined; + let max: number | undefined; + for (const { low, high } of plotData) { + if (min === undefined) { + min = low; + } + if (max === undefined) { + max = high; + } + + if (low !== undefined) { + if (min! > low) { + min = low; + } + } + + if (high !== undefined) { + if (max! < high) { + max = high; + } + } + } + + if (min === undefined) { + min = 0; + } + + if (max === undefined) { + max = 0; + } + + const padding = (max - min) * 0.1; + + return [min - padding, max + padding * 2]; + }; +} + +export const Updating = withOHLCData("SECONDS")( + withUpdatingData()(withSize({ style: { minHeight: 600 } })(withDeviceRatio()(BasicLineSeries))), +); diff --git a/packages/stories/src/features/updating/index.stories.tsx b/packages/stories/src/features/updating/index.stories.tsx new file mode 100644 index 000000000..fcb94381f --- /dev/null +++ b/packages/stories/src/features/updating/index.stories.tsx @@ -0,0 +1,8 @@ +import * as React from "react"; +import { Updating } from "./BasicLineSeries"; + +export default { + title: "Features/Updating", +}; + +export const continuous = () => ;