elstat/priv/frontend/src/components/Graph.js

99 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-07-16 04:04:43 +00:00
import React, { Component } from 'react'
import PropTypes from 'prop-types'
2018-06-12 21:56:50 +00:00
2018-07-16 04:04:43 +00:00
import ms from 'ms'
import {
ResponsiveContainer,
AreaChart,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Area,
ReferenceLine,
2018-07-16 04:04:43 +00:00
} from 'recharts'
2018-06-12 21:56:50 +00:00
2018-07-16 04:04:43 +00:00
import './Graph.css'
2018-06-12 21:56:50 +00:00
export default class Graph extends Component {
2018-07-14 02:40:45 +00:00
static propTypes = {
data: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)).isRequired,
}
2018-07-14 02:36:42 +00:00
state = {
screenWidth: window.innerWidth,
}
2018-08-08 23:43:53 +00:00
componentDidMount() {
2018-07-16 04:04:43 +00:00
window.addEventListener('resize', this.handleScreenChange)
2018-07-14 02:36:42 +00:00
}
2018-08-08 23:43:53 +00:00
componentWillUnmount() {
2018-07-16 04:04:43 +00:00
window.removeEventListener('resize', this.handleScreenChange)
2018-07-14 02:36:42 +00:00
}
handleScreenChange = () => {
this.setState({
screenWidth: window.innerWidth,
2018-07-16 04:04:43 +00:00
})
2018-07-14 02:36:42 +00:00
}
2018-08-08 23:43:53 +00:00
processData() {
2018-07-16 04:04:43 +00:00
const { data } = this.props
2018-06-12 21:56:50 +00:00
2018-07-16 04:04:43 +00:00
const objects = data.map(([timestamp, latency]) => ({
timestamp,
latency,
2018-07-16 04:04:43 +00:00
}))
// sort so that new entries are first
return objects.sort(({ timestamp: a }, { timestamp: b }) => a - b)
2018-06-12 21:56:50 +00:00
}
2018-08-08 23:43:53 +00:00
isSmallScreen() {
2018-07-16 04:04:43 +00:00
return this.state.screenWidth < 500
2018-07-13 19:10:00 +00:00
}
2018-08-08 23:43:53 +00:00
render() {
const yAxis = this.isSmallScreen() ? null : (
<YAxis
dataKey="latency"
tickLine={false}
tickFormatter={(tick) => `${tick}ms`}
/>
)
2018-07-16 04:04:43 +00:00
2018-06-12 21:56:50 +00:00
return (
<div className="graph-container">
<ResponsiveContainer width="100%" height={175}>
2018-08-08 23:43:53 +00:00
<AreaChart data={this.processData()}>
<XAxis
dataKey="timestamp"
tickFormatter={(tick) => ms(Date.now() - tick)}
tickLine={false}
/>
2018-07-16 04:04:43 +00:00
{yAxis}
2018-08-08 23:43:53 +00:00
<CartesianGrid strokeDasharray="1 1" />
<Tooltip
isAnimationActive={false}
formatter={(value) => `${value}ms`}
label="DAB"
separator=": "
labelFormatter={() => null}
/>
2018-08-08 23:43:53 +00:00
<ReferenceLine y={1000} label="1s" stroke="pink" />
<Area
type="monotone"
dataKey="latency"
stroke="hsla(200, 100%, 55%, 1)"
fill="hsl(200, 100%, 85%)"
isAnimationActive={false}
/>
</AreaChart>
</ResponsiveContainer>
</div>
2018-07-16 04:04:43 +00:00
)
2018-06-12 21:56:50 +00:00
}
}