View this code at http://livecoding.io/9963037
Created
April 3, 2014 21:12
-
-
Save iros/9963037 to your computer and use it in GitHub Desktop.
created by http://livecoding.io
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
{ | |
"libraries": [ | |
"d3" | |
], | |
"mode": "javascript", | |
"layout": "fullscreen mode (vertical)", | |
"resolution": "reset" | |
} |
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
/* css goes here */ |
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
<div id="vis"></div> |
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 base = d3.select("#vis"); | |
var chart = base.append("canvas") | |
.attr("width", 400) | |
.attr("height", 300); | |
var context = chart.node().getContext("2d"); | |
var dataContainer = base.append("custom"); | |
function drawCustom(data) { | |
var scale = d3.scale.linear() | |
.range([10, 390]) | |
.domain(d3.extent(data)); | |
var dataBinding = dataContainer.selectAll("custom.rect") | |
.data(data, function(d) { return d; }); | |
dataBinding | |
.attr("size", 8) | |
.transition() | |
.duration(1000) | |
.attr("size", 15) | |
.attr("fillStyle", "green"); | |
dataBinding.enter() | |
.append("custom") | |
.classed("rect", true) | |
.attr("x", scale) | |
.attr("y", 100) | |
.attr("size", 8) | |
.attr("fillStyle", "red"); | |
dataBinding.exit() | |
.attr("size", 8) | |
.transition() | |
.duration(1000) | |
.attr("size", 5) | |
.attr("fillStyle", "lightgrey"); | |
} | |
function drawCanvas() { | |
// clear canvas | |
context.fillStyle = "#fff"; | |
context.rect(0,0,chart.attr("width"),chart.attr("height")); | |
context.fill(); | |
var elements = dataContainer.selectAll("custom.rect"); | |
elements.each(function(d) { | |
var node = d3.select(this); | |
context.beginPath(); | |
context.fillStyle = node.attr("fillStyle"); | |
context.rect(node.attr("x"), node.attr("y"), node.attr("size"), node.attr("size")); | |
context.fill(); | |
context.closePath(); | |
}) | |
} | |
d3.timer(drawCanvas); | |
drawCustom([1,2,13,20,23]); | |
drawCustom([1,2,12,16,20]); | |
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
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment