graph-cases.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React from 'react';
  2. import format from 'date-fns/format';
  3. import { LineChart, XAxis, YAxis, Line, CartesianGrid } from 'recharts';
  4. import { DataSource, Country, Data } from '../types';
  5. import { getCases } from '../utils/get-cases';
  6. import { processCases } from '../utils/regression';
  7. type Props = {
  8. country: Country;
  9. };
  10. const margin = {
  11. top: 0,
  12. right: 0,
  13. bottom: 0,
  14. left: 0,
  15. };
  16. const dateTickFormatter = (date: Date): string => {
  17. return format(new Date(date), 'LLL do'); // 'Do MMM');
  18. };
  19. const GraphCases: React.FC<Props> = ({ country }) => {
  20. const dataSource = React.useMemo<DataSource>(() => getCases(country), [country]);
  21. const data = React.useMemo<Data>(() => processCases(dataSource.cases), [dataSource.cases]);
  22. return (
  23. <div>
  24. <h3>Country: {country.toUpperCase()}</h3>
  25. <p>
  26. Source: <a href={dataSource.source}>{dataSource.source}</a>
  27. </p>
  28. <LineChart width={640} height={480} data={data} margin={margin}>
  29. <XAxis
  30. dataKey="xValue"
  31. domain={['auto', 'auto']}
  32. tickFormatter={dateTickFormatter}
  33. type="number"
  34. />
  35. <YAxis tick yAxisId="left" />
  36. <CartesianGrid stroke="#f5f5f5" />
  37. <Line type="monotone" dataKey="value" stroke="#ffabf8" yAxisId="left" />
  38. <Line type="monotone" dataKey="regression" stroke="#ffcffb" yAxisId="left" dot={false} />
  39. <Line type="monotone" dataKey="valueCumulative" stroke="#cc3d55" yAxisId="left" />
  40. <Line
  41. type="monotone"
  42. dataKey="regressionCumulative"
  43. stroke="#e86178"
  44. yAxisId="left"
  45. dot={false}
  46. />
  47. </LineChart>
  48. </div>
  49. );
  50. };
  51. export default GraphCases;