Skip to content

Instantly share code, notes, and snippets.

@reverofevil
Created January 14, 2014 00:48
Show Gist options
  • Select an option

  • Save reverofevil/8410990 to your computer and use it in GitHub Desktop.

Select an option

Save reverofevil/8410990 to your computer and use it in GitHub Desktop.
.CodeMirror {
background: white;
}
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="http://codemirror.net/lib/codemirror.css">
<script src="http://codemirror.net/lib/codemirror.js"></script>
<script src="http://codemirror.net/mode/xml/xml.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<form>
<textarea id="code" name="code"></textarea>
</form>
<svg class="chart"></svg>
<pre id="dump"></pre>
</body>
</html>
var rectSize = 32;
var rectPadding = 8;
// Get color based on action
function getColor(d, i) {
switch (d.origin) {
case "+input": return "blue";
case "+delete": return "red";
case "paste": return "green";
case "undo": return "magenta";
case "redo": return "cyan";
}
return "grey";
}
// Create infographics
function redraw() {
var shift = rectSize + rectPadding;
var line = lines
.selectAll("g")
.data(d3.pairs(changes));
line.exit().remove();
line.enter()
.append("line")
.attr("transform", function (d, i) {
return "translate(" + rectSize / 2 + "," +
(rectSize / 2 + i * shift) + ")";
})
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", 0)
.attr("y2", rectPadding + rectSize)
.attr("stroke", "black")
.attr("stroke-width", 1);
var item = items
.selectAll("g")
.data(changes);
item.exit().remove();
item.enter()
.append("g")
.attr("transform", function(d, i) {
return "translate(0," + i * shift + ")";
})
.append("rect")
.attr("fill", getColor)
.attr("width", rectSize)
.attr("height", rectSize);
}
// On editor change
function onChange(cm, obj) {
var dump = document.getElementById("dump");
dump.innerHTML += JSON.stringify(obj) + "\n";
changes.push(obj);
redraw();
}
// Common state
var changes = [];
// Initialize D3
var chart = d3
.select(".chart")
.attr("width", 100)
.attr("height", 500);
var lines = chart.append("g");
var items = chart.append("g");
// Initialize CodeMirror
var code = document.getElementById("code");
var cm = CodeMirror.fromTextArea(code, {});
cm.on("change", onChange);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment