Your game in c14 used arrays to hold fireflies. Arrays can hold something else too: data. Monthly rainfall, feed bills, scores. A list of numbers is hard to read, but a bar chart shows the story instantly. Today your code will draw one, and the key tool is a function that converts a data value into a number of pixels.
map()
map(60, 0, 120, 0, 160) asks: 60 is halfway along 0 to 120, so what is halfway along 0 to 160? The answer is 80. You met this idea in maths as a linear rule: same fraction of the way along both scales.
Choose a city. Tap a bar to read its exact value. Notice how the bars rescale so the tallest one always fits the canvas.
240 - h?On a canvas, y = 0 is the top and y grows downward (c01). The chart's floor sits at y = 240. A bar that is h pixels tall must start h pixels above the floor, so its top-left corner is at 240 - h. Forget the subtraction and every bar hangs from the floor line like a bat.
With biggest = 110 and the pixel range 0 to 190, what height does map(55, 0, 110, 0, 190) give, and what is the bar's top y-coordinate on a floor at 240?
Answer: 55 is exactly half of 110, so the height is 95. The bar's top is at 240 - 95 = 145.
values with six numbers you invent, such as your horse's weight in kg each month. Does the chart still fit the canvas? Why, even with a very large number?values is [10, 20, 40] and the pixel range is 0 to 160, write the three bar heights on paper before running anything. Then check by running.240 - h to just 240 and run it. Describe exactly what you see, then explain it using "y grows downward".240 - h - 6. Why minus again?map() range so the bars start from a non-zero value, such as 30. Which bars look dramatically different now, and why can that fool a reader?text() and units on the vertical side.