Saturday, October 6, 2012

I've been spending lots of time with Data Driven Documents (d3) for building data visualizations. At its simplest, it's a bare bones library for binding data to SVG to create visualizations. For anyone who has used it, the warnings are usually the same, "It's a steep learning curve". The funny thing is that d3 is fairly simple. The problem is that you need to be fairly intimate with drawing and transforming shapes, lines and nodes using SVG. That was the most difficult part for me and it's nothing that d3 has brought to the table. If your work already involves things like transformation matrices, you'd be in familiar territory and d3 would be a pushover.

One of the first things I needed to do was select a circle and have it "fit to screen" which was essentially zooming and centering the circle. There were a couple gotchas that I had to hammer through before I figured it out.

Scale the view THEN translate. It fits my mental model better to make the view the size I need via scale and then to move around it with a translation. I'm sure you could translate first and then scale if you want.

Translations performed on a scaled shape do not need to be scaled themselves. So, if I scale a circle with a diameter of 40 by 2 then now the coordinate system has doubled and its diameter is 80. If I wanted to translate the shape by (10,20) I wouldn't have to scale it first and make it (20, 40).

The top left of your viewport is your origin (0,0) and its positive expansion is down and right.

We divide the height of the viewport by the diameter of the circle so we know how many times we need to magnify the circle to make it fit the screen. In the case of my example, we're scaling based on the height of the viewport since it's shorter than the width. We can only zoom as far as the shortest dimension so as not to cut off the shape.

When you scale, the viewport is being expanded beyond the visible boundaries downwards and to the right. What you see in the viewport if you only scaled would be the top left of the newly scaled view. You need to find out what the X and Y coordinates are for the center of your smaller "window" since that's where you want to put your zoomed in shape.