aboutsummaryrefslogtreecommitdiff
path: root/modern/src/reports/ChartReportPage.js
blob: 5aefca15b38ec0fc0595188cd1b01ffef13bc1c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React, { useState } from 'react';
import { Box, Paper } from '@material-ui/core';
import ReportFilter from './ReportFilter';
import ReportLayoutPage from './ReportLayoutPage';
import { useAttributePreference } from '../common/preferences';
import { getConverter, formatDate } from '../common/formatter';
import {LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';

const ReportFilterForm = ({ setItems }) => {

  const speedUnit = useAttributePreference('speedUnit');

  const handleSubmit = async (deviceId, from, to, mail, headers) => {
    const query = new URLSearchParams({ deviceId, from, to, mail });
    const response = await fetch(`/api/reports/route?${query.toString()}`, { headers });
    if (response.ok) {
      const data = await response.json();
      let formattedData = data.map((obj)=>{
        return Object.assign(obj, 
                              {speed: Number(getConverter('speed')(obj.speed, speedUnit))},
                              {fixTime: formatDate(obj.fixTime)}
                            );
      })
      setItems(formattedData);
    }
  }

  return (
    <ReportFilter handleSubmit={handleSubmit} showOnly />
  )
};

const CustomizedAxisTick = ({ x, y, payload }) =>{
  const parts = payload.value.split(' ');
  return (
    <g transform={`translate(${x},${y})`}>
      <text x={0} y={0} dy={16} textAnchor="end" fill="#666" transform="rotate(-35)">{parts[0]}</text>
      <text x={0} y={16} dy={16} textAnchor="end" fill="#666" transform="rotate(-35)">{parts[1]}</text>
    </g>
  );
}

const ChartReportPage = () => {

  const [items, setItems] = useState([]);
  const [type, setType] = useState('speed');

  return (
    <ReportLayoutPage reportFilterForm={ReportFilterForm} setItems={setItems} setType={setType} showChartType>
      <Paper>
        <Box height={400}>
          <ResponsiveContainer>
            <LineChart data={items}>
              <XAxis dataKey="fixTime" tick={<CustomizedAxisTick/>} height={60} />
              <YAxis />
              <CartesianGrid strokeDasharray="3 3" />
              <Tooltip />
              <Legend />
              <Line type="natural" dataKey={type} />
            </LineChart>
          </ResponsiveContainer>
        </Box>
      </Paper>
    </ReportLayoutPage>
  );
};

export default ChartReportPage;