Skip to main content
100%

Margin Convention with Model.js

✓ Published0🌍 Public
CCurran Kelleher
Last edited Nov 20, 2015
Created on Sep 21, 2015

This example demonstrates how the D3 Margin Convention can be encapsulated as reusable functions using Model.js, with the SVG element and inner G element automatically resizing to fit the container. The code defines three separate functions—marginConvention, backgroundRect, and a clip rectangle—each managing its own dynamic behavior through Model.js property bindings. It renders a gray background rectangle and a clipped X pattern, with the visualization’s width and height derived from the container’s box dimensions minus the margin. The example uses D3.js for SVG manipulation and Model.js to coordinate reactive updates between the margin, box size, and inner element dimensions.

AI-generated description

This example shows one way to encapsulate the D3 Margin Convention using Model.js.

Check out the resize behavior in full screen mode.

More generally, this example is an experiment in how to generalize self-contained pieces of dynamic graphical behavior. Note that the following three pieces of this visualization are each implemented in separate functions:

  • The margin convention, which
    • resizes the SVG element by setting width and height
    • translates the G element by the margin top and left
    • assigns values to the model properties width and height, which represent the dimensions of the rectangle inside the margin.
  • The background X.
  • The clip rectangle, which will clip the corners of the background X to be within the rectangle inside the margin.

Draws from:

Similar vizzes