Created
January 14, 2014 00:48
-
-
Save reverofevil/8410990 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .CodeMirror { | |
| background: white; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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