2018-06-12 21:56:50 +00:00
|
|
|
import React, { Component } from 'react';
|
2018-07-14 02:40:45 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-06-12 21:56:50 +00:00
|
|
|
|
2018-07-13 19:07:53 +00:00
|
|
|
import ms from 'ms';
|
2018-07-14 05:03:12 +00:00
|
|
|
import {
|
|
|
|
ResponsiveContainer,
|
|
|
|
AreaChart,
|
|
|
|
XAxis,
|
|
|
|
YAxis,
|
|
|
|
CartesianGrid,
|
|
|
|
Tooltip,
|
|
|
|
Area,
|
|
|
|
ReferenceLine,
|
|
|
|
} from 'recharts';
|
2018-06-12 21:56:50 +00:00
|
|
|
|
2018-07-13 19:07:53 +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,
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window.addEventListener('resize', this.handleScreenChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.handleScreenChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleScreenChange = () => {
|
|
|
|
this.setState({
|
|
|
|
screenWidth: window.innerWidth,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-13 19:07:53 +00:00
|
|
|
processData() {
|
2018-07-14 05:03:12 +00:00
|
|
|
const { data } = this.props;
|
2018-06-12 21:56:50 +00:00
|
|
|
|
2018-07-14 05:03:12 +00:00
|
|
|
return data.map(([timestamp, latency]) => ({
|
|
|
|
timestamp,
|
|
|
|
latency,
|
|
|
|
})).sort(({ timestamp: a }, { timestamp: b }) => a - b);
|
2018-06-12 21:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 19:10:00 +00:00
|
|
|
isSmallScreen() {
|
2018-07-14 02:36:42 +00:00
|
|
|
return this.state.screenWidth < 500;
|
2018-07-13 19:10:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:56:50 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2018-07-13 19:07:53 +00:00
|
|
|
<div className="graph-container">
|
2018-07-14 05:03:12 +00:00
|
|
|
<ResponsiveContainer width="100%" height={175}>
|
|
|
|
<AreaChart
|
|
|
|
data={this.processData()}
|
|
|
|
>
|
|
|
|
<XAxis
|
|
|
|
dataKey="timestamp"
|
|
|
|
tickFormatter={(tick) => ms(Date.now() - tick)}
|
|
|
|
tickLine={false}
|
|
|
|
/>
|
2018-07-14 05:12:59 +00:00
|
|
|
{this.isSmallScreen()
|
|
|
|
? null
|
|
|
|
: <YAxis
|
|
|
|
dataKey="latency"
|
|
|
|
tickLine={false}
|
|
|
|
tickFormatter={(tick) => `${tick}ms`}
|
|
|
|
/>
|
|
|
|
}
|
2018-07-14 05:03:12 +00:00
|
|
|
<CartesianGrid strokeDasharray="1 1" />
|
|
|
|
<Tooltip
|
|
|
|
isAnimationActive={false}
|
|
|
|
formatter={(value) => `${value}ms`}
|
|
|
|
label="DAB"
|
|
|
|
separator=": "
|
|
|
|
labelFormatter={() => null}
|
|
|
|
/>
|
|
|
|
<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>
|
2018-07-13 19:07:53 +00:00
|
|
|
</div>
|
2018-06-12 21:56:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|