|
| 1 | +import * as React from 'react'; |
| 2 | +import { BarChart, BarChartProps, barElementClasses } from '@mui/x-charts/BarChart'; |
| 3 | +import Stack from '@mui/material/Stack'; |
| 4 | +import Typography from '@mui/material/Typography'; |
| 5 | +import FormControl from '@mui/material/FormControl'; |
| 6 | +import FormLabel from '@mui/material/FormLabel'; |
| 7 | +import RadioGroup from '@mui/material/RadioGroup'; |
| 8 | +import FormControlLabel from '@mui/material/FormControlLabel'; |
| 9 | +import Radio from '@mui/material/Radio'; |
| 10 | +import { countryData } from '../dataset/countryData'; |
| 11 | +import householdSavings from '../dataset/oecdHouseholdSavings2024.json'; |
| 12 | + |
| 13 | +/* Source: https://www.oecd.org/en/data/indicators/household-savings.html */ |
| 14 | +const savings = Object.entries(householdSavings) |
| 15 | + .map(([country, value]) => ({ |
| 16 | + country, |
| 17 | + value, |
| 18 | + })) |
| 19 | + .sort((a, b) => a.value - b.value); |
| 20 | + |
| 21 | +const percentageFormatter = new Intl.NumberFormat('en-US', { |
| 22 | + style: 'percent', |
| 23 | + maximumFractionDigits: 1, |
| 24 | +}); |
| 25 | + |
| 26 | +const settings = { |
| 27 | + height: 600, |
| 28 | + xAxis: [{ label: 'Household Savings (%)' }], |
| 29 | + layout: 'horizontal', |
| 30 | + hideLegend: true, |
| 31 | +} satisfies Partial<BarChartProps>; |
| 32 | + |
| 33 | +export default function BarOECDHouseholdSavings() { |
| 34 | + const [gradientUnits, setGradientUnits] = React.useState< |
| 35 | + 'objectBoundingBox' | 'userSpaceOnUse' |
| 36 | + >('userSpaceOnUse'); |
| 37 | + |
| 38 | + return ( |
| 39 | + <Stack width="100%"> |
| 40 | + <Typography variant="h6" textAlign="center"> |
| 41 | + Household Savings in OECD Countries (2016) |
| 42 | + </Typography> |
| 43 | + |
| 44 | + <FormControl fullWidth> |
| 45 | + <FormLabel id="gradient-units-label">Gradient Units</FormLabel> |
| 46 | + <RadioGroup |
| 47 | + row |
| 48 | + aria-labelledby="gradient-units-label" |
| 49 | + name="gradient-units" |
| 50 | + value={gradientUnits} |
| 51 | + onChange={(event) => |
| 52 | + setGradientUnits( |
| 53 | + event.target.value as 'objectBoundingBox' | 'userSpaceOnUse', |
| 54 | + ) |
| 55 | + } |
| 56 | + > |
| 57 | + <FormControlLabel |
| 58 | + value="objectBoundingBox" |
| 59 | + control={<Radio />} |
| 60 | + label="objectBoundingBox (default)" |
| 61 | + /> |
| 62 | + <FormControlLabel |
| 63 | + value="userSpaceOnUse" |
| 64 | + control={<Radio />} |
| 65 | + label="userSpaceOnUse" |
| 66 | + /> |
| 67 | + </RadioGroup> |
| 68 | + </FormControl> |
| 69 | + |
| 70 | + <BarChart |
| 71 | + {...settings} |
| 72 | + yAxis={[ |
| 73 | + { |
| 74 | + data: savings.map((v) => v.country), |
| 75 | + valueFormatter: (value: keyof typeof countryData) => |
| 76 | + countryData[value].country, |
| 77 | + width: 100, |
| 78 | + }, |
| 79 | + ]} |
| 80 | + series={[ |
| 81 | + { |
| 82 | + label: 'Household Savings', |
| 83 | + data: savings.map((v) => v.value), |
| 84 | + color: 'url(#savings-gradient)', |
| 85 | + valueFormatter: (value) => percentageFormatter.format(value! / 100), |
| 86 | + }, |
| 87 | + ]} |
| 88 | + sx={ |
| 89 | + gradientUnits === 'userSpaceOnUse' |
| 90 | + ? { |
| 91 | + [`.${barElementClasses.root}`]: { |
| 92 | + fill: 'url(#savings-gradient-user-space)', |
| 93 | + }, |
| 94 | + } |
| 95 | + : undefined |
| 96 | + } |
| 97 | + > |
| 98 | + <Gradient id="savings-gradient" x1="0" x2="100%" /> |
| 99 | + <Gradient |
| 100 | + id="savings-gradient-user-space" |
| 101 | + gradientUnits="userSpaceOnUse" |
| 102 | + x1="150" |
| 103 | + x2="100%" |
| 104 | + /> |
| 105 | + </BarChart> |
| 106 | + <Typography variant="caption">Source: Our World in Data</Typography> |
| 107 | + </Stack> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function Gradient(props: React.SVGProps<SVGLinearGradientElement>) { |
| 112 | + return ( |
| 113 | + <linearGradient x1="0" y1="0%" x2="100%" y2="0%" {...props}> |
| 114 | + <stop offset="0" stopColor="#ff2f1b" /> |
| 115 | + <stop offset="0.5" stopColor="#fce202" /> |
| 116 | + <stop offset="1" stopColor="#02b32b" /> |
| 117 | + </linearGradient> |
| 118 | + ); |
| 119 | +} |
0 commit comments