Show contents
Chapter 4 of 6 · 20 min
Drawing in SVG
By the end of this chapter, you'll be able to
- Describe an SVG drawing with rectangles, lines and text
- Write a scale function that turns a value into a position
- Draw a bar chart and a line chart in SVG with JavaScript
- Make a chart readable and accessible: title, labels, contrast, alternative text
SVG in thirty seconds
SVG (Scalable Vector Graphics) describes a drawing with tags, as HTML describes a page. It stays sharp at every size. The origin (0, 0) is at the top left, and the vertical axis goes down:
<svg viewBox="0 0 300 120">
<rect x="10" y="20" width="150" height="30" fill="#2b6cb0" /> ← a rectangle
<line x1="10" y1="100" x2="290" y2="100" stroke="#333" /> ← a line
<text x="10" y="15">Bamako</text> ← a text
<polyline points="10,90 60,60 110,70" fill="none" stroke="#c05621" /> ← a polyline
</svg>
viewBox="0 0 300 120" defines a drawing space 300 units wide by 120 high; the browser then stretches it to the real size of the screen.
The scale: from value to position
The heart of every chart is a scale function: it turns a data value (from 0 to 1,210 kg) into a position on the drawing (from 0 to 250 units). It is a simple rule of three:
function scale(domainMin, domainMax, rangeMin, rangeMax) {
return (value) => rangeMin + ((value - domainMin) / (domainMax - domainMin)) * (rangeMax - rangeMin);
}
const width = scale(0, 1210, 0, 250);
console.log(width(1210), width(605), width(0));
// For a line chart, the vertical axis is inverted: the largest value is at the top (small y).
const height = scale(0, 400, 180, 20);
console.log(height(0), height(400), height(100));
250 125 0
180 20 140
A function that returns a function: scale(...) builds a scale, which is then called on each value. For the vertical axis, we give an inverted range (from 180 to 20) because SVG's y goes down.
A bar chart
const sales = [["Bamako", 1210], ["Koutiala", 820], ["Sikasso", 640], ["Ségou", 410]];
const x = (v) => (v / 1210) * 220;
const bars = sales.map(([name, kg], i) => {
const y = 10 + i * 30;
return `<text x="0" y="${y + 15}">${name}</text>`
+ `<rect x="70" y="${y}" width="${Math.round(x(kg))}" height="20" fill="#2b6cb0"/>`
+ `<text x="${Math.round(x(kg)) + 75}" y="${y + 15}">${kg}</text>`;
});
console.log(bars[0]);
console.log(bars.length, "bars");
<text x="0" y="25">Bamako</text><rect x="70" y="10" width="220" height="20" fill="#2b6cb0"/><text x="295" y="25">1210</text>
4 bars
Each bar is made of three elements: the name on the left, the rectangle whose width comes from the scale, and the value at the end. In the mini-lab, these strings are inserted into an <svg> to be displayed.
The value is written at the end of the bar: the reader does not need to estimate.
Text description of the animation
- We start from a data value.
- The scale does a rule of three: the largest value takes the whole available width.
- The result is rounded: it is the bar's width, in drawing units.
- The rectangle starts at x = 70, after the names column, so it always starts from the same line: zero.
- The value is written at the end of the bar: the reader does not need to estimate.
Mini-lab: the sales chartHTML · CSS · JS
Preview
Console
(Nothing in the console yet.)Your code runs in an isolated page, with no access to this site or the Internet · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Readable and accessible
A chart must be understood by everyone, including on a small screen, by a colour-blind person or with a screen reader:
- a title that says what, where, when and in which unit;
- written values on the important bars or points;
- enough contrast (dark text on a light background) and a colour that is never the only information: red and green are confused by about 1 man in 12;
- an alternative text:
role="img"and a<desc>tag summarising the chart for screen readers.
Your turn
Worked exercise
Worked exercise: the points of a line
Total sales for four months are [260, 360, 250, 310] kg. Compute the coordinates of a line's points in a 300 × 200 space: the months are evenly spaced from x = 30 to x = 270, and values from 0 to 400 kg go from y = 180 (bottom) to y = 20 (top). Produce the string for the points attribute of a <polyline>.
Hint 1
Hint 2
scale function with a range from 180 to 20.Show the solution and common mistakes
function scale(domainMin, domainMax, rangeMin, rangeMax) {
return (value) => rangeMin + ((value - domainMin) / (domainMax - domainMin)) * (rangeMax - rangeMin);
}
const totals = [260, 360, 250, 310];
const x = scale(0, totals.length - 1, 30, 270);
const y = scale(0, 400, 180, 20);
const points = totals.map((kg, i) => `${x(i)},${y(kg)}`).join(" ");
console.log(points);
30,76 110,36 190,80 270,56
October's point (360 kg) has the smallest y (36): it is the highest on the drawing, as expected. It is then inserted into <polyline points="30,76 110,36 190,80 270,56" fill="none" stroke="#c05621"/>.
Common mistakes:
- Using the month's index directly as a coordinate (0, 1, 2, 3): all the points pile up in the left corner.
- Starting the vertical axis at the smallest value (250) instead of 0 without saying so: for a line chart, this is acceptable only if the axis is labelled and the information shown clearly.
Summary
- 1An SVG chart is made of shapes (rect, line, polyline, text) placed at coordinates.
- 2A scale function turns a value into a position with a rule of three.
- 3In SVG, y increases downwards: a line chart's vertical scale must be inverted.
- 4Bars all start from the same line, zero, and carry their value.
- 5Title, written values, contrast, colour never alone and alternative text make a chart accessible.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.