Skip to content

Instantly share code, notes, and snippets.

@Hypercubed
Forked from curran/LICENSE
Last active May 3, 2017 00:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hypercubed/b01a767b41b0e679aade to your computer and use it in GitHub Desktop.
Save Hypercubed/b01a767b41b0e679aade to your computer and use it in GitHub Desktop.
Chiasm Boilerplate (with download buttons)

Boilerplate starter code for Chiasm.

To get started, go ahead and edit this example on blockbuilder.org. From there you can experiment with creating your own Chiasm components and applications, then publish your work back to bl.ocks.org.

The Chiasm components demonstrated here are:

  • chiasm-layout A Chiasm plugin for nested box layout. You can use this plugin to impose a grid system of rectangles on any collection of Chiasm components.

  • coloredRectangle An example custom Chiasm component that draws a colored rectangle using D3.js and SVG. The naming convention of my for the component is inspired by the wonderful piece on reusable D3 charts Towards Reusable Charts.

  • downloadButton Based on coloredRectangle but with an interface to svgsaver.

For a more complex example that creates data visualizations, check out Fundamental Visualizations.

// This function defines a Chiasm component that draws a colored rectangle using D3.js.
function ColoredRectangle() {
// Construct a Chiasm component instance,
// specifying default values for public properties.
var my = new ChiasmComponent({
// The color of the rectangle, a CSS color string.
color: "white"
});
// Initialize an SVG Group element for containing this component.
var svg = my.initSVG();
// Add a background rectangle to the SVG Group element using D3.
var rect = d3.select(svg).append("rect");
// Set the rectangle color to be the configured color.
my.when("color", function (color){
rect.attr("fill", color);
});
// Respond to dynamic width and height.
// "box" is a special property set by chiasm-layout.
my.when("box", function (box) {
// Set the size of the background rectangle.
rect
.attr("width", box.width)
.attr("height", box.height);
});
// Set up the rectangle so that when you click on it, it changes color.
rect.on("click", function (){
my.color = randomColor();
});
// Generates a random color.
function randomColor(){
return "rgb(" + rand() + "," + rand() + "," + rand() + ")";
}
// Generates a random integer between 0 and 255.
function rand(){
return Math.floor(Math.random() * 256)
};
return my;
}
// This function defines a Chiasm component that draws a colored rectangle using D3.js.
function ColoredButton() {
// Construct a Chiasm component instance,
// specifying default values for public properties.
var my = new ChiasmComponent({
// The color of the rectangle, a CSS color string.
color: "white",
text: "Download SVG",
downloadAs: "SVG"
});
var svgsaver = new SvgSaver();
// Initialize an SVG Group element for containing this component.
var _g = my.initSVG();
var g = d3.select(_g);
g.style('cursor', 'pointer');
// Add a background rectangle to the SVG Group element using D3.
var rect = g.append("rect");
var label = g.append('text')
.attr("text-anchor", "middle");
// Set the rectangle color to be the configured color.
my.when("color", function (color){
rect.attr("fill", color);
});
my.when("text", function (text){
label.text(text);
});
// Respond to dynamic width and height.
// "box" is a special property set by chiasm-layout.
my.when("box", function (box) {
// Set the size of the background rectangle.
rect
.attr("width", box.width)
.attr("height", box.height);
label
.attr('x', box.width/2)
.attr('y', box.height/2);
});
// Set up the rectangle so that when you click on it, it downlaods the SVG
g.on("click", function (){
if (my.downloadAs === "SVG") {
svgsaver.asSvg(_g.ownerSVGElement);
} else {
svgsaver.asPng(_g.ownerSVGElement);
}
});
return my;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Chiasm Boilerplate</title>
<!-- A functional reactive model library. See github.com/curran/model -->
<script src="//curran.github.io/model/cdn/model-v0.2.4.js"></script>
<!-- The common base for Chiasm components (depends on Model.js). -->
<script src="//chiasm-project.github.io/chiasm-component/chiasm-component-v0.2.1.js"></script>
<!-- This script defines the ColoredRectangle component. -->
<script src="coloredRectangle.js"> </script>
<script src="downloadButton.js"> </script>
<!-- Chiasm.js depends on Model.js, Lodash.js, D3.js. -->
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<!-- Load Chiasm.js. See github.com/chiasm-project -->
<script src="//chiasm-project.github.io/chiasm/chiasm-v0.2.0.js"></script>
<!-- Load the nested box layout plugin. See https://github.com/chiasm-project/chiasm-layout -->
<script src="//chiasm-project.github.io/chiasm-layout/chiasm-layout-v0.2.2.js"></script>
<!-- Load FileSaver and svgsaver -->
<script src="//rawgit.com/eligrey/FileSaver.js/master/FileSaver.js"></script>
<script src="//rawgit.com/Hypercubed/svgsaver/v0.3.0/browser.min.js"></script>
<!-- Make the Chiasm container fill the page and have a 20px black border. -->
<style>
body {
background-color: black;
}
#chiasm-container {
background-color: white;
position: fixed;
left: 20px;
right: 20px;
top: 20px;
bottom: 20px;
}
</style>
</head>
<body>
<!-- Chiasm component DOM elements will be injected into this div. -->
<div id="chiasm-container"></div>
<!-- This is the main program that sets up the Chiasm application. -->
<script>
// Create a new Chiasm instance.
var chiasm = new Chiasm();
// Register plugins that the configuration can access.
chiasm.plugins.layout = ChiasmLayout;
chiasm.plugins.coloredRectangle = ColoredRectangle;
chiasm.plugins.coloredButton = ColoredButton;
// Set the Chaism configuration.
chiasm.setConfig({
"layout": {
"plugin": "layout",
"state": {
"containerSelector": "#chiasm-container",
"layout": {
"orientation": "vertical",
"children": [
"A", {
"orientation": "horizontal",
"children": [
"B", {
"orientation": "vertical",
"children": [ "C", "D" ]
},
"E"
]
}
]
}
}
},
"A": { "plugin": "coloredRectangle" },
"B": {
"plugin": "coloredRectangle",
"state": { "color": "#a8f0ff" }
},
"C": {
"plugin": "coloredButton",
"state": {
"color": "#ffe2a8",
"text": "Click here to download SVG",
"downloadAs": "SVG"
}
},
"D": {
"plugin": "coloredButton",
"state": {
"color": "#99e2c8",
"text": "Click here to download PNG",
"downloadAs": "PNG"
}
},
"E": {
"plugin": "coloredRectangle",
"state": {
"color": "#a8ffd0",
}
}
});
// Here's a simpler configuration to try,
// including just a single component instance.
/*
chiasm.setConfig({
"layout": {
"plugin": "layout",
"state": {
"containerSelector": "#chiasm-container",
"layout": "visualization"
}
},
"visualization": {
"plugin": "coloredRectangle",
"state": {
"color": "blue"
}
}
});
*/
</script>
</body>
</html>
The MIT License (MIT)
Copyright (c) 2015 Curran Kelleher
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment