This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c02b4c
commit f040eb3
Showing
4 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React from "react"; | ||
import { last, mean } from "lodash"; | ||
import LiveChart from "./livechart"; | ||
|
||
export default class DemoLiveChart extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.period = 2000; | ||
this.domain = [54.6, 55.25]; | ||
this.maxPoints = 30; | ||
this.state = { | ||
data: this.getInitialData() | ||
}; | ||
} | ||
|
||
nextPoint(previous = null) { | ||
const [low, high] = this.domain; | ||
const newPoint = low + Math.random() * (high - low); | ||
return previous ? mean([previous, newPoint]) : newPoint; | ||
} | ||
|
||
getInitialData() { | ||
return [ | ||
{x: 0, y: this.domain[0] + 0.05}, | ||
{x: 1, y: this.domain[0] + 0.15} | ||
]; | ||
} | ||
|
||
getNewData() { | ||
const data = this.state.data; | ||
|
||
if (data.length !== this.maxPoints) { | ||
// add one | ||
const lastPoint = last(data); | ||
data.push( | ||
{y: this.nextPoint(lastPoint.y), x: lastPoint.x + 1} | ||
); | ||
return data; | ||
} else { | ||
return this.getInitialData(); | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
this.setStateInterval = setInterval(() => { | ||
this.setState({ // eslint-disable-line react/no-did-mount-set-state | ||
data: this.getNewData() | ||
}); | ||
}, this.period); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.setStateInterval); | ||
} | ||
|
||
getStyles() { | ||
return { | ||
parent: { | ||
display: "block", | ||
boxSizing: "border-box", | ||
margin: "0 auto", | ||
padding: 0, | ||
width: "auto", | ||
height: "100%", | ||
maxHeight: "280px" | ||
} | ||
}; | ||
} | ||
|
||
render() { | ||
const styles = this.getStyles(); | ||
return ( | ||
<svg | ||
viewBox="0 0 450 350" | ||
className="fancyBorder" | ||
style={styles.parent} | ||
> | ||
<LiveChart | ||
data={this.state.data} | ||
period={this.period} | ||
domain={this.domain} | ||
/> | ||
</svg> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from "react"; | ||
import { round } from "lodash"; | ||
import { VictoryAxis, VictoryChart, VictoryLine, VictoryScatter } from "victory-chart"; | ||
|
||
const leftPad = (str, len, ch) => { | ||
str = String(str); | ||
let i = -1; | ||
if (!ch && ch !== 0) { | ||
ch = " "; | ||
} | ||
len -= str.length; | ||
while (++i < len) { | ||
str = ch + str; | ||
} | ||
return str; | ||
}; | ||
|
||
const start = Date.now(); | ||
|
||
const makeDate = (period, count) => { | ||
const date = new Date(start + count * period); | ||
const hours = leftPad(date.getHours(), 2, 0); | ||
const minutes = leftPad(date.getMinutes(), 2, 0); | ||
const seconds = leftPad(date.getSeconds(), 2, 0); | ||
return `${hours}:${minutes}:${seconds}`; | ||
}; | ||
|
||
const LiveChart = ({data, period, domain}) => ( | ||
<VictoryChart | ||
animate={{ duration: 1000 }} | ||
padding={{ | ||
left: 80, | ||
right: 50, | ||
top: 20, | ||
bottom: 30 | ||
}} | ||
> | ||
<VictoryAxis | ||
tickFormat={makeDate.bind(null, period)} | ||
tickCount={3} | ||
style={{ | ||
ticks: {stroke: "black", strokeWidth: 3} | ||
}} | ||
/> | ||
<VictoryAxis dependentAxis | ||
label="Price" | ||
tickCount={4} | ||
tickFormat={(y) => round(y, 2)} | ||
domain={domain} | ||
style={{ | ||
axisLabel: { | ||
padding: 50 | ||
}, | ||
ticks: {stroke: "black", strokeWidth: 3} | ||
}} | ||
/> | ||
<VictoryLine | ||
data={data} | ||
interpolation="monotoneX" | ||
style={{ | ||
data: { strokeWidth: 1 } | ||
}} | ||
/> | ||
<VictoryScatter | ||
data={data} | ||
size={5} | ||
style={{ | ||
data: { | ||
fill: "black", | ||
stroke: "white", | ||
strokeWidth: 3 | ||
} | ||
}} | ||
/> | ||
</VictoryChart> | ||
); | ||
|
||
export default LiveChart; |