|
// This is an example Chaism plugin that uses D3. A colored rectangle is |
|
// created with an X in the background and text in the foreground. The X in the |
|
// background is interactive. Clicking and dragging it updates `lineWidth`. |
|
function ColoredRectangle() { |
|
|
|
// Construct a Chiasm component instance, |
|
// specifying default values for public properties. |
|
var my = ChiasmComponent({ |
|
|
|
// The color of the rectangle, a CSS color string. |
|
color: "white" |
|
|
|
}); |
|
|
|
// Expose a div element that will be added to the Chiasm container. |
|
// This is a special property that chiasm-layout looks for. |
|
my.el = document.createElement("div"); |
|
|
|
// Create an SVG DOM element using D3. |
|
var svg = d3.select(my.el).append("svg"); |
|
|
|
// Add a background rectangle to the SVG. |
|
svg.append("rect") |
|
.attr("x", 0) |
|
.attr("y", 0); |
|
|
|
// Set the rectangle color to be the configured color. |
|
my.when("color", function (color){ |
|
svg.select("rect").attr("fill", color); |
|
}); |
|
|
|
// Respond to width and height changes. |
|
// "box" is a special property that gets set by chiasm-layout. |
|
my.when("box", function (box) { |
|
|
|
// Set the size of the SVG element. |
|
svg |
|
.attr("width", box.width) |
|
.attr("height", box.height); |
|
|
|
// Set the size of the background rectangle. |
|
svg.select("rect") |
|
.attr("width", box.width) |
|
.attr("height", box.height); |
|
|
|
}); |
|
|
|
return my; |
|
} |