Scaled Scatter plot with Axes
✓ Published0🌍 Public
Aalokkshukla
Last edited Sep 3, 2017
Created on Sep 3, 2017
A scatter plot displays 50 randomly generated data points, with circles scaled in size and stroke width based on their y-values. The chart includes x and y axes generated using D3 v4's `d3.axisBottom` and `d3.axisLeft`, which use linear scales (`d3.scaleLinear`) for positioning. The SVG rendering also adds light gray vertical guide lines from each point to the baseline, while the axes are styled with teal strokes and bold text via CSS. Tick counts are configured on each axis.
AI-generated descriptionIntroducing Axes
- Visual features unlike
scale.
d3.axisTop // Ticks on top
d3.axisBottom
d3.axisLeft
d3.axisRight // Ticks on right
- Take
scaleas a parameter
var xAxis = d3.axisBottom()
.scale(xScale);
- Put inside a group, assign a class ( for CSS ), and position using transform's translate.
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
D3’s call() function takes the incoming selection, as received from the prior link in the chain, and hands that selection off to any function.
- Use CSS to style; note svg attributes differ from CSS ones
.axis path,
.axis line {
stroke: teal;
shape-rendering: crispEdges;
}
.axis text {
font-family: Optima, Futura, sans-serif;
font-weight: bold;
font-size: 14px;
fill: teal;
}
- Check for ticks.
var xAxis = d3.axisBottom()
.scale(xScale)
.ticks(5);
var xAxis = d3.axisBottom()
.scale(xScale)
.tickValues([0, 100, 250, 600]);
var formatAsPercentage = d3.format(".1%");
xAxis.tickFormat(formatAsPercentage);