US Rainfall Part 6 — set up scales and axis
Previous Lessons
- Remove Characters from Beginning of JavaScript String
- Divide Large Data Set into Smaller Data Sets
- Handling Radio Buttons in D3.js
- Set Up SVG Viewport with D3.js
- D3.js Data Update Pattern
In the previous lesson, we plotted circles to correspond to rainfall by state. However, we did not show the state name or the units of rainfall. We’re going to use D3 scales to create both the axis and the position of the circles.
Step Overview
- Scale
- Axis
- Group
- Update
- Call
Scale
Create xScale above the d3.csv
section. Do not set the .domain
yet.
const xScale = d3.scaleBand()
.range([0, width])
.padding(0.1);
Axis
const xAxis = d3.axisBottom(xScale);
Group
create xGroup and translate.
const xGroup = svg.append('g')
.attr('transform', `translate(0, ${height})`);
Update
in drawRain
, create an array of states for the data set. You will use this data set to update the domain of xScale.
Get States
let states = [];
dataset.forEach(element => {
states.push(element.state);
});
Update Domain
xScale
.domain(states);
Call
xGroup
.call(xAxis);
Next Lesson
Use D3.js Data for x-coordinate Plotting