Skip to content

Commit

Permalink
Add switch to hide all countries
Browse files Browse the repository at this point in the history
  • Loading branch information
rickkln committed Apr 30, 2020
1 parent 66c79d5 commit c069fab
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ module.exports = {
rules: {
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
"jsx-a11y/label-has-associated-control": [2, {
"controlComponents": ["Switch"],
"depth": 3,
}],
},
};
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"react-dom": "^16.12.0",
"react-helmet": "^5.2.1",
"react-icons": "^3.9.0",
"react-switch": "^5.0.1",
"react-table": "^7.0.4",
"react-tag-autocomplete": "^5.13.0",
"victory": "^34.1.3"
Expand Down
89 changes: 49 additions & 40 deletions src/components/dataChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import { Tag } from 'react-tag-autocomplete';
import { Country } from '../utilities/getData';
import Theme from './chartTheme';
import {
purpleA100, tealA200, cyanA400, deepPurpleA200, lightBlueA700, greenA700, purpleA200, green600, teal400, green900, green700, lightGreen700,
cyanA400,
deepPurpleA200,
teal400,
lightBlueA700,
purpleA200,
lightGreen700,
} from './colors';

interface DataChartProps {
countries: Country[]
tags: Tag[]
x: string
y: string
tags: Tag[]
showAll: boolean
}

interface Selected {
Expand All @@ -30,7 +36,7 @@ const selectedColors = [
];

const DataChart = ({
countries, tags, x, y,
countries, x, y, tags, showAll,
}: DataChartProps) => {
const selected: Selected = {};
tags.forEach(
Expand All @@ -39,43 +45,46 @@ const DataChart = ({
},
);
return (
<VictoryChart
theme={Theme}
height={220}
width={600}
padding={{
top: 10,
bottom: 30,
left: 70,
right: 3,
}}
domainPadding={{ x: [0, -4], y: [0, 1] }}
minDomain={{ y: 0 }}
>
<VictoryAxis fixLabelOverlap />
<VictoryAxis dependentAxis />
{countries.map((country) => {
if (country.name === undefined) { return undefined; }
const data = Object.keys(selected).includes(country.name)
? {
stroke: selected[country.name],
strokeWidth: 1.8,
}
: {
strokeWidth: 1,
};
return (
<VictoryLine
key={country.name}
data={country.periods.slice(0).reverse()}
interpolation="monotoneX"
style={{ data }}
x={x}
y={y}
/>
);
})}
</VictoryChart>
<>
<VictoryChart
theme={Theme}
height={220}
width={600}
padding={{
top: 10,
bottom: 30,
left: 70,
right: 3,
}}
domainPadding={{ x: [0, -4], y: [0, 1] }}
minDomain={{ y: 0 }}
>
<VictoryAxis fixLabelOverlap />
<VictoryAxis dependentAxis />
{countries.map((country) => {
if (country.name === undefined) { return undefined; }
if (!showAll && !Object.keys(selected).includes(country.name)) { return undefined; }
const data = Object.keys(selected).includes(country.name)
? {
stroke: selected[country.name],
strokeWidth: 1.8,
}
: {
strokeWidth: 1,
};
return (
<VictoryLine
key={country.name}
data={country.periods.slice(0).reverse()}
interpolation="monotoneX"
style={{ data }}
x={x}
y={y}
/>
);
})}
</VictoryChart>
</>
);
};

Expand Down
4 changes: 4 additions & 0 deletions src/components/dataContent.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.switch {
top: 0.6em;
margin-left: 1em;
}
33 changes: 25 additions & 8 deletions src/components/dataContent.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState, CSSProperties } from 'react';
import { Link } from 'gatsby';
import Switch from 'react-switch';
import Layout from './layout';
import SEO from './seo';
import {
Expand All @@ -10,6 +11,7 @@ import {
} from './tables';
import DataChart from './dataChart';
import CountryFilter, { Tags } from './countryFilter';
import styles from './dataContent.module.css';

const buttonStyle: CSSProperties = {
fontSize: '0.85em',
Expand Down Expand Up @@ -44,6 +46,7 @@ const DataContent = ({ countries }: { countries: Country[] }) => {
],
suggestedTags: possibleTags,
});
const [showAll, setShowAll] = useState(true);
return (
<Layout>
<SEO title="All Data" />
Expand Down Expand Up @@ -115,21 +118,35 @@ const DataContent = ({ countries }: { countries: Country[] }) => {
{selectedTable === 'newCases' && 'New cases in period.'}
{selectedTable === 'totalCases' && 'Cases to date.'}
{' '}
Table color coded by
{' '}
<Link to="/details">Outbreak Status</Link>
<br />
<label>
<span>Include all countries</span>
<Switch
id="showAll"
onChange={setShowAll}
checked={showAll}
onColor="#28c53c"
offColor="#ddd"
className={styles.switch}
/>
</label>
</p>
<CountryFilter tags={tags} setTags={setTags} />
{selectedTable === 'growth'
&& <DataChart countries={countries} tags={tags.currentTags} x="endDate" y="growthRate" />}
&& <DataChart countries={countries} x="endDate" y="growthRate" tags={tags.currentTags} showAll={showAll} />}
{selectedTable === 'newDeaths'
&& <DataChart countries={countries} tags={tags.currentTags} x="endDate" y="newDeaths" />}
&& <DataChart countries={countries} x="endDate" y="newDeaths" tags={tags.currentTags} showAll={showAll} />}
{selectedTable === 'totalDeaths'
&& <DataChart countries={countries} tags={tags.currentTags} x="endDate" y="totalDeaths" />}
&& <DataChart countries={countries} x="endDate" y="totalDeaths" tags={tags.currentTags} showAll={showAll} />}
{selectedTable === 'newCases'
&& <DataChart countries={countries} tags={tags.currentTags} x="endDate" y="newCases" />}
&& <DataChart countries={countries} x="endDate" y="newCases" tags={tags.currentTags} showAll={showAll} />}
{selectedTable === 'totalCases'
&& <DataChart countries={countries} tags={tags.currentTags} x="endDate" y="totalCases" />}
&& <DataChart countries={countries} x="endDate" y="totalCases" tags={tags.currentTags} showAll={showAll} />}
<small>
The table below is color coded by
{' '}
<Link to="/details">Outbreak Status</Link>
</small>
{selectedTable === 'growth' && <GrowthTable data={countries} />}
{selectedTable === 'newDeaths' && <NewDeathsTable data={countries} />}
{selectedTable === 'totalDeaths' && <TotalDeathsTable data={countries} />}
Expand Down

0 comments on commit c069fab

Please sign in to comment.