Reactive Mixins
This example demonstrates how to encapsulate reusable graphical behaviors using Model.js and D3.js, with a focus on dynamic resizing. It shows a margin convention that adjusts an SVG’s width and height, a gray background rectangle, a large diagonal X, and a centered circle, all clipped by a rectangle that follows the inner dimensions. The visualization responds to window resizing by reading the container’s size via `clientWidth` and `clientHeight`, then updating the model’s `box` property, which triggers the reactive `my.when` callbacks to redraw. The code uses D3’s selection and data-join patterns for lines and rects, alongside Model.js’s reactive property tracking to coordinate layout and rendering.
AI-generated descriptionThis 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
widthandheight - translates the G element by the margin
topandleft - assigns values to the model properties
widthandheight, which represent the dimensions of the rectangle inside the margin.
- resizes the SVG element by setting
- 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:
- blockbuilder.org starter code
- Responding to Resize This is where the X in the background comes from.
- Focus + Context Scatter Plots This is where the clip path logic comes from.
forked from <a href='http://bl.ocks.org/curran/'>curran</a>'s block: <a href='http://bl.ocks.org/curran/2c736a99c2f57bfac7e4'>Margin Convention with Model.js</a>