Skip to content

Instantly share code, notes, and snippets.

@veltman
veltman / README.md
Created January 2, 2018 03:08
Departures board (slow)
@veltman
veltman / README.md
Last active March 11, 2024 16:22
Departures board
@veltman
veltman / README.md
Created December 27, 2017 18:28
Automatic label placement along a path #2

A method for automatically finding the best eligible label position and font size for a label that's going to go along a path inside of an area, similar to this example but with two embellishments:

  • Using d3plus.polygonRayCast() to more accurately measure the vertical clearance available at a given x position with any rotation.
  • Avoiding label positions that would cause the text to be overlapped by another shape for a case like this bump chart.

The measurement gets thrown off a little bit by the curve function and the fact that text is rotated letter by letter instead of continuously, but the results seem good enough.

See also: Automatic label placement along a path Streamgraph label positions #2

@veltman
veltman / README.md
Last active January 16, 2020 10:50
Automatic label placement along path

A method for automatically finding the best eligible label position and size for a label that's going to go along a path inside of an area. This is potentially suitable for area charts where the area being labeled has a clear direction the text should "flow" in.

The basic approach is a crude one: test a center point at a certain font size, and work outwards to the left and right and disqualify it if you ever hit the side of the chart or run out of vertical space. If it fits, try a bigger size. If not, try a smaller size. Rinse/repeat until you've either found the largest size that will fit at that position or found that nothing will fit. Repeat with each possible center point until you've found the largest font size you can use, and where to put it.

Notes:

  • This adds a small optimization of disqualifying any candidate points that involve a sharp angle turn, because text turning at a sharp angle can be pretty hard to read.
  • Another optimization that's omitted for brevity is to first try to use a flat label
{
"name": "world",
"children": [
{
"name": "Asia",
"color": "#f58321",
"children": [
{"name": "China", "weight": 14.84, "code": "CN"},
{"name": "Japan", "weight": 5.91, "code": "JP"},
{"name": "India", "weight": 2.83, "code": "IN"},
license: lgpl-3.0
@veltman
veltman / README.md
Created September 28, 2017 15:34
Click-to-zoom via fitSize

Click-to-zoom using projection.fitSize() to interpolate a projection's scale and translate instead of modifying the SVG transform. Has the advantage of leaving stroke-widths alone and the disadvantage of probably being a lot slower.

See also: click-to-zoom via transform

@veltman
veltman / README.md
Created September 2, 2017 12:31
SVG animation to video

Converting an SVG animation to a video with the MediaRecorder API and a hidden canvas.

Drawing frames from img elements can introduce an extra delay, so this version generates all the frames upfront and then renders them in a loop with requestAnimationFrame().

See also: Canvas animation to video

@veltman
veltman / README.md
Created September 2, 2017 12:00
Canvas animation to video
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.11.3/xlsx.full.min.js"></script>
<script>
d3.request("turtles.xlsx")
.responseType("arraybuffer")
.get(function(err, response){