Instantly share code, notes, and snippets.


Tool Tips

An SVG hovering element that shows the value under the mouse point. This example generates random Y-axis values (-100 to 100) across a 120-second interval (X-axis).

A good amount of this code is for centering text, which I plan to streamline at some point.


Realtime Line Graph

A simple line graph that updates in realtime. In this case, it is showing the millisecond value generated by Javascript every 100ms, over the course of 12 seconds.


if you leave this tab for a second and come back, you can see that window.setTimeout slows down. D3 is simply connecting points, so when the data becomes sparse, you get unexpected patterns.

You may also notice that the points never hit the X-axis: again, points are only generated every 100ms, so unless you load this script at exactly 000ms, D3 will never see a 0ms value. Limitations of data…


#Bar Chart Clock

For a simple bar chart that changes value (this example uses time), defining the width of the bar as a percentage of its container will suffice. 14 lines gets the job done.


#Range Area Graph This graph shows temperature ranges for each day in May.

The low temperatures are drawn as a clipping path to hide the bottom of the low temperature area. This allows for the use of gradients or photos or whatever you'd like in the background.
The graphs are also generated by a function, so the code is easily reusable. The low temperature graph is drawn with the opposite domain it would usually have: this makes it overlap with the high temperature graph to create a proper clipping path.

View 20150420-20150520_tempRange.json

Luminance Histogram for R, G, B Hues

This d3 histogram shows the range of luminance values (0 - 100) for all saturation levels of red, green, and blue hues, respectively. I've used opacity on a dark background to mimic proper color-mixing. D3 transitions provide smooth animation when showing and hiding individual histograms. The particularly interesting result here is that there is a much larger variety of light colors than of dark colors; if you were to choose an rgb() (or hex) color at random, bet on it being > 50 L*.


Move the slider to choose the angle of the x-axis labels.


Geometry is fun! Rotated x-axis labels calculated by input.


A 3-series histogram of red, green, and blue hue luminance distribution. The bars are z-axis sorted by height, and the graph is sortable by color, with animation.


Histograms of the range of luminance values for all unmixed hues of red, green, and blue.

Middle gray for our eyes is 18% gray for computer monitors (rgb(47,47,47) is L*a*b*(50.015), where L*.domain([0, 100])). The background color is the RGB value closest to L* == 50.