import React, { Component } from 'react' import PropTypes from 'prop-types' import ms from 'ms' import { ResponsiveContainer, AreaChart, XAxis, YAxis, CartesianGrid, Tooltip, Area, ReferenceLine, } from 'recharts' import './Graph.css' export default class Graph extends Component { static propTypes = { data: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)).isRequired, } state = { screenWidth: window.innerWidth, } componentDidMount() { window.addEventListener('resize', this.handleScreenChange) } componentWillUnmount() { window.removeEventListener('resize', this.handleScreenChange) } handleScreenChange = () => { this.setState({ screenWidth: window.innerWidth, }) } processData() { const { data } = this.props const objects = data.map(([timestamp, latency]) => ({ timestamp, latency, })) // sort so that new entries are first return objects.sort(({ timestamp: a }, { timestamp: b }) => a - b) } isSmallScreen() { return this.state.screenWidth < 500 } render() { const yAxis = this.isSmallScreen() ? null : ( `${tick}ms`} /> ) return (
ms(Date.now() - tick)} tickLine={false} /> {yAxis} `${value}ms`} label="DAB" separator=": " labelFormatter={() => null} />
) } }