Draw a Bar Chart from Data

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

The BarChart component gets a data prop, a list of { label, value } objects (the preview uses Mon 4, Tue 8, Wed 2, Thu 6 and Fri 5), and a unit prop (default "sales"). Draw it with plain elements, one bar per item, in the given order. Each bar is an element with role="img" and an aria-label like "Tue: 8 sales", so a screen reader can read the chart. Its inline style width is its share of the largest value as a whole-number percentage: the value divided by the largest value, times 100, rounded to the nearest whole number and written like "50%", so the largest bar is "100%". Above the bars, a heading reads like "Total: 25 sales", the sum of all the values. Work it all out from the props.

Your code must pass

  • Each item gets a bar named like "Tue: 8 sales", in order
  • Each bar's width is its share of the largest value
  • A heading shows the total, like "Total: 25 sales"