So I’m about to embark on creating a slew of D3 examples for a book project I’m working on, and I’d like to get feedback on the approach. Here’s the first example, a scatter plot.
I’d like to get feedback on:
Imports
import { select } from 'd3-selection';
import { csv } from 'd3-fetch';
import { scaleLinear } from 'd3-scale';
import { extent } from 'd3-array';
import { axisLeft, axisBottom } from 'd3-axis';
This is the way I’m doing imports. I think this is the best format, so it’s clear which packages things are coming from, and it’s compatible with downstream workflows that use popular build tools like Webpack and Rollup. It’s also compatible with import maps. The con of this approach is that it does require use of a build tool or import maps, which adds a bit of up-front complexity to explain.
Naming
I want to establish a naming convention that will make sense for a large family of examples, and I also want to make each example readable as a standalone program without the context of the others. In the history of D3 examples, I sometimes see x being the X value accessor, and sometimes x is the X scale. IMO either way is confusing, so I’m opting for the following conventions:
-
xAccessor
- The value accessor. Sometimes called xValue in examples. Opting for xAccessor overxValue
because I’ve found that I always refer to it as the “X accessor”, so why not name it that. The con of this is that it’s not an established pattern in the history of D3 examples. -
xScale
- The scale. Opting to use this instead of justx
so it is clear that it’s a scale. -
parseRow
- The function that parses a row, the second argument tod3.csv
. In the docs this is calledrow
, but I don’t want to use that name because we might want a variable called row that refers to an individual row.
Margin handling
I’m going for now with the .range([margin.left, width - margin.right])
pattern, as opposed to the .range([0, width - margin.right - margin.left]) pattern with the extra
translated by
margin.leftand
margin.top`. This is mainly because this is how most of the newer D3 examples handle it.
Thoughts on this structure? Any recommendations for change? Thanks!