# All files / src/components/Graphs Graph.tsx

0% Statements 0/47
0% Branches 0/44
0% Functions 0/8
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210

     import type { FC } from 'react';
    import { useRef } from 'react';
    import { useParentSize } from '@cutting/hooks';
    import {
      VictoryChart,
      VictoryGroup,
      VictoryLegend,
      VictoryTooltip,
      VictoryLine,
      VictoryScatter,
      VictoryAxis,
    } from 'victory';
    import dayjs from 'dayjs';
    import { ApplicationLayout } from 'src/layouts/ApplicationLayout';
    import { LoadingOverlay } from '@cutting/component-library';
    import { ResponsiveSVG } from '@cutting/svg';
    import { AxisColor, Countries, DayData, countryData, CountriesResult } from '../Graphs/types';
    import * as Urls from 'src/urls';
    import { NavLink, useLocation } from 'react-router-dom';
    import { assert } from 'assert-ts';

    import styles from './Graph.module.scss';

    export type GraphProps = {
      result: { data?: CountriesResult };
      xAxisLabel: string;
      yAxisLabel: string;
      xTickFormat?: (label: string, i: number, a: unknown[]) => string;
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      yTickFormat?: (tick: any, index: number, ticks: any[]) => string | number;
      heading: string;
      labels?: ({ datum }: { datum: DayData }) => string;
    };

    export const Graph: FC<GraphProps> = ({
      result,
      yAxisLabel,
      labels,
      heading,
      xTickFormat = (label: string, i: number, a: unknown[]) =>
        i % 2 === 0 || i === a.length - 1 ? `${dayjs(label).format('DD/MM')}` : '',
      yTickFormat = (t) => t,
    }) => {
      const location = useLocation();
      const legendRef = useRef<HTMLDivElement>(null);
      const chartRef = useRef<HTMLDivElement>(null);

      const largeScreen = typeof window !== 'undefined' && window.screen.availWidth > 500;

      const { width: legendWdith, height: legendHeight } = useParentSize(legendRef, { debounceDelay: 50 });

      const { width: chartWidth, height: chartHeight } = useParentSize(chartRef, { debounceDelay: 50 });

      return (
        <ApplicationLayout heading={heading} showFooter={true}>
          <div className={styles.container}>
            <ul className={styles.links}>
              {[
                {
                  url: Urls.Covid19,
                  text: 'Daily increase in deaths (Scotland)',
                },
                {
                  url: Urls.IncreaseInDeaths,
                  text: 'Daily increase in deaths (World)',
                },
                // {
                //   url: Urls.RateOfChange,
                //   text: 'Rate of change',
                // },
                // { url: Urls.Deaths, text: 'Total deaths' },
              ].map((u) => {
                if (location.pathname === u.url) {
                  return <li key={u.url}>{u.text}</li>;
                }

                return (
                  <li key={u.url}>
                    <NavLink activeClassName={styles.active} to={u.url} exact={true}>
                      {u.text}
                    </NavLink>
                  </li>
                );
              })}
            </ul>
            <div className={styles.legend} ref={legendRef}>
              <ResponsiveSVG height={legendHeight} width={legendWdith}>
                <VictoryLegend
                  x={0}
                  y={0}
                  height={legendHeight}
                  width={legendWdith}
                  centerTitle={largeScreen}
                  standalone={false}
                  orientation="horizontal"
                  itemsPerRow={largeScreen ? 8 : 4}
                  gutter={20}
                  style={{
                    labels: {
                      fill: AxisColor,
                    },
                    title: {
                      fill: '#fff',
                    },
                  }}
                  data={Object.keys(countryData)
                    .filter((k) => {
                      if (location.pathname !== Urls.Covid19) {
                        return true;
                      }
                      return ['SCO'].includes(k);
                    })
                    .map((k) => ({
                      name: countryData[k as Countries].longName,
                      symbol: { fill: countryData[k as Countries].color },
                    }))}
                />
              </ResponsiveSVG>
            </div>

            <div className={styles.chart} ref={chartRef}>
              {typeof result?.data === 'undefined' ? (
                <LoadingOverlay busy={true} darkMode />
              ) : (
                <ResponsiveSVG width={chartWidth} height={chartHeight}>
                  <VictoryChart
                    width={chartWidth}
                    height={chartHeight}
                    standalone={false}
                    theme={{
                      axis: {
                        style: {
                          axis: { stroke: AxisColor },
                          tickLabels: {
                            color: AxisColor,
                            fill: AxisColor,
                          },
                          grid: { stroke: '#fff', strokeOpacity: 0.2 },
                        },
                      },
                    }}
                  >
                    <VictoryAxis
                      dependentAxis
                      label={`${yAxisLabel} up until ${dayjs().subtract(1, 'day').format('DD/MM/YYYY')}`}
                      orientation="left"
                      standalone={false}
                      style={{
                        axisLabel: {
                          fill: AxisColor,
                          fillOpacity: 0.5,
                        },
                      }}
                      tickFormat={yTickFormat}
                    />
                    <VictoryAxis
                      orientation={'bottom'}
                      standalone={false}
                      style={{
                        tickLabels: {
                          angle: largeScreen ? 45 : 90,
                          verticalAnchor: 'middle',
                          textAnchor: 'start',
                        },
                      }}
                      tickFormat={xTickFormat}
                    />
                    {/* eslint-disable-next-line @typescript-eslint/no-explicit-any */}
                    {Object.keys(result.data as any).map((k) => {
                      assert(result.data?.[k as Countries], `No country data ${k}`);

                      const country = result.data?.[k as Countries];
                      return (
                        <VictoryGroup
                          key={k}
                          color={country?.color}
                          standalone={false}
                          labels={labels}
                          labelComponent={
                            <VictoryTooltip
                              style={{ fontSize: 15, paddingRight: 4 }}
                              renderInPortal={false}
                              constrainToVisibleArea={true}
                              flyoutWidth={200}
                            />
                          }
                          data={country?.data}
                        >
                          <VictoryLine
                            interpolation="natural"
                            style={{
                              data: { strokeWidth: ['SCO'].includes(k) ? 7 : 2 },
                            }}
                            standalone={false}
                          />
                          <VictoryScatter standalone={false} size={() => (['SCO'].includes(k) ? 7 : 3)} />
                        </VictoryGroup>
                      );
                    })}
                  </VictoryChart>
                </ResponsiveSVG>
              )}
            </div>
          </div>
        </ApplicationLayout>
      );
    };

    export default Graph;