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
Add "live" chart to homepage #97
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this code is editable you might want to express this animation duration in terms of props.duration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you're right. Or rename
props.duration
. That name is kind of misleading at this point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ cleared this up