D3 为什么如此冗长?
Why is D3 so Verbose?

原始链接: https://theheasman.com/short_stories/why-is-d3-code-so-long-and-complicated-or-why-is-it-so-verbose/

学习D3.js最初具有挑战性,因为它冗长——创建一个简单的箱线图需要大约194行代码,而Excel只需要几次点击。然而,这种复杂性解锁了令人难以置信的灵活性和定制性。 D3本质上将数据转换为可缩放矢量图形(SVGs),使用详细的指令为每个元素定义,例如为线条定义精确的坐标。虽然可以使用固定值绘制基本形状,但D3的力量在于将这些指令*绑定*到数据,动态生成视觉效果。 这种详细的方法允许完全控制可视化的各个方面,从而实现独特且艺术化的呈现,超越了Datawrapper或Flourish等更简单工具的能力。尽管学习曲线陡峭,但作者认为冗长最终是一种优势,它使数据可视化人员能够创建真正定制化且具有影响力的作品——从交互式滚动叙事到全新的图表类型。

相关文章

原文

D3 is a b***h of a language at first glance. It’s long. It’s complicated and verbose. You have to enter what feels like an obscene amount of key strokes to draw one line.

I’m currently about one-third of the way through my journey of learning D3.js, having finished Interactive Visualisation for the Web by Scott Murray, and now picking my way through D3.js in Action by Elijah Meeks and Anne-Marie Dufour. (And just for kicks, I’m reading D3 for the Impatient by Philipp K Janert on the side whenever I have some free time).

Which means, I just finished drawing a box plot from chapter 6. Yup. You know. That thing, you just make four or five clicks in Microsoft Excel, and voila, it appears. In a way that you don’t think is magical, because Excel has been around longer than the dial up modem.

I just shared this on my social media, and for the non-technical, I had to explain “hey, so, the reason why this is impressive is because…”

That’s 194 lines of code to draw a box plot. Why on earth would any masochist do this?

Because of the flexibility D3 provides.

The reason why there are so many lines is because:

  1. I’m doing this the long way. I could write some functions or components which could collapse a lot of those lines, but I’m still learning the ropes. Better to do it the long and manual way so I don’t skip anything
  2. This is important: D3 is a library which allows you to draw SVGs and bind them to data

All D3 is doing is saying “so, hey, I got all this data. Now. Dear browser. I’d like you to draw these SVG shapes, according to these instructions.”

SVG code reads like an extended HTML block of code. To draw a line on a two dimensional grid you have to define:

  • x1 – the horizontal position of point 1
  • x2 – the horizontal position of point 2
  • y1 – the vertical position of point 1
  • y2 – the vertical position of point 2

In D3 if I was just going to draw this with fixed values, say a line that goes from co-ordinates (0,12) to (4,15):

But the power comes from when I bind this to data:

That block of code draws the bottom horizontal line of the box plot for each of those boxes, getting the values from the data, and adjusted for some other variables that I defined elsewhere.

It might seem long, but what’s great about it, is how much you can customise those values. You could make that line a curve if you want. Or you could add little tiny whiskers throughout, so your box plot starts to look like an unruly cat. Whatever. It’s up to you.

That’s what’s great about D3. It’s like drawing. Sure you can take a photograph, but isn’t it cooler to draw something, showing your interpretation of the object in question?

What I’m trying to say is:

D3 is verbose so you can create art.

With tools like datawrapper and flourish there’s little need these days to learn D3. However, you aren’t reading this because you want click and deploy tools.

You’re reading this because you’ve seen some of the great visualisations Data Visualisers have created out there. From scrollytelling to streamgraphs, or something unique and special that has no name.

And while some tools allow you to do some of that, D3 allows you to do it all.

And one day, on your learning journey, you’ll love how verbose it is.


Don’t mind me, just gonna plug my newsletter below:

Live in London? Sign up to get data stories that go one layer deeper on the big conversations. Subscribers get my exclusive three part data story on London’s Affordability Crisis. Sign up here.

Box not appearing? Click here and scroll to the bottom.

联系我们 contact @ memedata.com