In theory, the idea here is similar to when I was learning React/Redux
and diving into SQL
selects. In practice, I think most of D3’s complexity isn’t exactly
in a direction that is elucidated by writing down types for everything,
so the title is a mere personal snowclone. I’m just writing things out
to an arbitrary amount of detail until I understand them and can refer
to what I wrote here later.
Background
D3 is “a JavaScript library for
visualizing data”. It has a lot of sublibraries that interoperate
well but could be used separately — for example, it has utilities for
manipulating colors, time, and SVG paths. Of the various concepts,
though, I think D3 selections are the most distinctive and
fundamental, so they are the focus of this post.
At a high level, D3 selections feel like jQuery. You run some code
and it goes into the DOM and adds, deletes, and mutates a bunch of
elements. The docs even endorse monkeypatching
d3.selection to add custom helpers. However, D3 has data binding and
batch operations that make it easy to change the DOM in a way that
resembles reconciliation in a framework like React.
Selections
API: Selecting
Elements.
A D3 selection holds an array of arrays of
nullable DOM elements. The intermediate
arrays are called groups. Additionally, each group in a
selection is associated with a parent node. During
basic D3 usage, you might only ever work with selections with a single
group and ignore parent nodes.
When relevant, I will call the index of an element inside its group
the “within-group index” and the index of a group among all groups in a
selection the “across-group index”.
A more accurate but less informative title for this post would be
“How I wish React and Redux were explained to me”. Note that this does
not imply that this method of explanation is suitable for
anybody else. I suspect it won’t be for most people.
I had to learn React and Redux the past summer for my internship at
MemSQL, and there were hundreds of articles that explain React and Redux
in addition to the (fine) built-in documentation, but none of them
scratched the itch; I wanted to know what was going on completely,
including some of the technical details and the philosophy I ought to be
following, as well as efficiently. I did not need another explanation
about how to think functionally, in JavaScript types or with immutable
data. React’s chapter on Conditional
Rendering, for example, felt so inefficient — I know what
if statements and conditional expressions are, and I know
how to refactor complicated subexpressions into variables…
So here’s the guide I wish I had. I think. It’s been months since I
started it (as usual, for posts on this blog) and it is probably
incomplete. However, I haven’t written React/Redux deeply in a while, so
I didn’t have much motivation to continue to investigate the incomplete
bits; and the perfect is the enemy of the good, so here it is.
Okay, I think I’m figuring this out. When I make a filler post for
the streak, it should be an
unabashed filler post, so I can accumulate some of the blogging time I
find each day to work on a serious post (and for doing the other
important stuff I should be doing!) instead of wasting it right
away.
Life. I’m programming something for Dad involving a parser using
Jison, and one of the tasks
involved stuffing a
custom
lexer into the parser. I was struggling to get my custom lexer to
track locations properly. I thought this might be because I didn’t
properly understand which components of which location went in which
variables of the lexer, which seemed to be a plausible source of trouble
since I had glanced at the sample code and it seemed to have a weird
half-open range of columns and lines that I didn’t understand how to
mimic in my code.