Skip to content

Instantly share code, notes, and snippets.

@faulkner
Created June 24, 2012 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faulkner/2983814 to your computer and use it in GitHub Desktop.
Save faulkner/2983814 to your computer and use it in GitHub Desktop.
Cubism not bubbling up left/right arrow key events?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cubism.js</title>
<style>
@import url(//fonts.googleapis.com/css?family=Yanone+Kaffeesatz:400,700);
@import url(http://square.github.com/cubism/style.css);
#example { min-height: 155px; }
</style>
</head>
<body id="body">
<div id="example"></div>
<form action="">
<input type="text" name="text" value="try to get to [here] using your arrow keys after clicking [here]" size="80"/>
</form>
<script src="http://square.github.com/cubism/d3.v2.js"></script>
<script src="http://square.github.com/cubism/cubism.v1.js"></script>
<script>
function random(name) {
var value = 0,
values = [],
i = 0,
last;
return context.metric(function(start, stop, step, callback) {
start = +start, stop = +stop;
if (isNaN(last)) last = start;
while (last < stop) {
last += step;
value = Math.max(-10, Math.min(10, value + .8 * Math.random() - .4 + .2 * Math.cos(i += .2)));
values.push(value);
}
callback(null, values = values.slice((start - stop) / step));
}, name);
}
var context = cubism.context()
.serverDelay(0)
.clientDelay(0)
.step(1e3)
.size(960);
var foo = random("foo"),
bar = random("bar");
d3.select("#example").call(function(div) {
div.append("div")
.attr("class", "axis")
.call(context.axis().orient("top"));
div.selectAll(".horizon")
.data([foo, bar, foo.add(bar), foo.subtract(bar)])
.enter().append("div")
.attr("class", "horizon")
.call(context.horizon().extent([-20, 20]));
div.append("div")
.attr("class", "rule")
.call(context.rule());
});
// On mousemove, reposition the chart values to match the rule.
context.on("focus", function(i) {
d3.selectAll(".value").style("right", i == null ? null : context.size() - i + "px");
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment