2012-04-02 20:20:14 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
|
|
|
<link href="../src/d3.css" rel="stylesheet" type="text/css">
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
|
|
|
body {
|
|
|
|
overflow-y:scroll;
|
|
|
|
}
|
|
|
|
|
|
|
|
text {
|
|
|
|
font: 12px sans-serif;
|
|
|
|
}
|
|
|
|
|
|
|
|
</style>
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="chart">
|
|
|
|
<svg></svg>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script src="../lib/d3.v2.js"></script>
|
|
|
|
<script src="../nv.d3.js"></script>
|
2012-05-07 12:53:51 +00:00
|
|
|
<script src="../src/tooltip.js"></script>
|
2012-04-02 20:20:14 +00:00
|
|
|
<script src="../src/models/stackedArea.js"></script>
|
|
|
|
<script>
|
|
|
|
|
|
|
|
|
|
|
|
//Format A
|
|
|
|
nv.addGraph({
|
|
|
|
generate: function() {
|
|
|
|
var n = 10, // number of layers
|
2012-04-03 07:19:09 +00:00
|
|
|
m = 200; // number of samples per layer
|
2012-04-02 20:20:14 +00:00
|
|
|
//color = d3.interpolateRgb("#aad", "#556");
|
|
|
|
|
2012-04-03 07:19:09 +00:00
|
|
|
var data = stream_layers(n,m).map(function(data, i) {
|
|
|
|
return {
|
|
|
|
key: 'Stream' + i,
|
|
|
|
values: data
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-05-07 12:53:51 +00:00
|
|
|
var width = nv.utils.windowSize().width - 20,
|
|
|
|
height = nv.utils.windowSize().height - 20;
|
2012-04-02 20:20:14 +00:00
|
|
|
|
|
|
|
var chart = nv.models.stackedArea()
|
|
|
|
.width(width)
|
|
|
|
.height(height)
|
|
|
|
//.offset('wiggle')
|
|
|
|
//.order('default')
|
|
|
|
|
|
|
|
|
|
|
|
var svg = d3.select('#chart svg')
|
|
|
|
.attr('width', width)
|
|
|
|
.attr('height', height)
|
|
|
|
.datum(data)
|
|
|
|
|
|
|
|
svg.transition().duration(500).call(chart);
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
},
|
|
|
|
callback: function(graph) {
|
|
|
|
|
2012-05-07 12:53:51 +00:00
|
|
|
|
|
|
|
|
2012-04-02 20:20:14 +00:00
|
|
|
graph.dispatch.on('tooltipShow', function(e) {
|
2012-05-07 12:53:51 +00:00
|
|
|
var offsetElement = document.getElementById("chart"),
|
|
|
|
left = e.pos[0] + offsetElement.offsetLeft,
|
|
|
|
top = e.pos[1] + offsetElement.offsetTop,
|
|
|
|
formatterY = d3.format(",.2%"),
|
2012-04-02 20:20:14 +00:00
|
|
|
formatterX = function(d) {
|
2012-05-07 12:53:51 +00:00
|
|
|
return d3.time.format('%x')(new Date(d))
|
2012-04-02 20:20:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var content = '<h3>' + e.series.key + '</h3>' +
|
|
|
|
'<p>' +
|
2012-05-07 12:53:51 +00:00
|
|
|
formatterY(graph.y()(e.point)) + ' at ' + formatterX(graph.x()(e.point)) +
|
2012-04-02 20:20:14 +00:00
|
|
|
'</p>';
|
|
|
|
|
2012-05-07 12:53:51 +00:00
|
|
|
nv.tooltip.show([left, top], content);
|
2012-04-02 20:20:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
graph.dispatch.on('tooltipHide', function(e) {
|
2012-05-07 12:53:51 +00:00
|
|
|
nv.tooltip.cleanup();
|
2012-04-02 20:20:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-07 12:53:51 +00:00
|
|
|
window.onResize = function() {
|
|
|
|
var width = nv.utils.windowSize().width - 20,
|
|
|
|
height = nv.utils.windowSize().height - 20,
|
2012-04-02 20:20:14 +00:00
|
|
|
margin = graph.margin();
|
|
|
|
|
|
|
|
|
|
|
|
if (width < margin.left + margin.right + 20)
|
|
|
|
width = margin.left + margin.right + 20;
|
|
|
|
|
|
|
|
if (height < margin.top + margin.bottom + 20)
|
|
|
|
height = margin.top + margin.bottom + 20;
|
|
|
|
|
|
|
|
|
|
|
|
graph
|
|
|
|
.width(width)
|
|
|
|
.height(height);
|
|
|
|
|
|
|
|
d3.select('#chart svg')
|
|
|
|
.attr('width', width)
|
|
|
|
.attr('height', height)
|
|
|
|
.call(graph);
|
|
|
|
|
2012-05-07 12:53:51 +00:00
|
|
|
}
|
2012-04-02 20:20:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Inspired by Lee Byron's test data generator. */
|
|
|
|
function stream_layers(n, m, o) {
|
|
|
|
if (arguments.length < 3) o = 0;
|
|
|
|
function bump(a) {
|
|
|
|
var x = 1 / (.1 + Math.random()),
|
|
|
|
y = 2 * Math.random() - .5,
|
|
|
|
z = 10 / (.1 + Math.random());
|
|
|
|
for (var i = 0; i < m; i++) {
|
|
|
|
var w = (i / m - y) * z;
|
|
|
|
a[i] += x * Math.exp(-w * w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d3.range(n).map(function() {
|
|
|
|
var a = [], i;
|
|
|
|
for (i = 0; i < m; i++) a[i] = o + o * Math.random();
|
|
|
|
for (i = 0; i < 5; i++) bump(a);
|
|
|
|
return a.map(stream_index);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Another layer generator using gamma distributions. */
|
|
|
|
function stream_waves(n, m) {
|
|
|
|
return d3.range(n).map(function(i) {
|
|
|
|
return d3.range(m).map(function(j) {
|
|
|
|
var x = 20 * j / m - i / 3;
|
|
|
|
return 2 * x * Math.exp(-.5 * x);
|
|
|
|
}).map(stream_index);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function stream_index(d, i) {
|
|
|
|
return {x: i, y: Math.max(0, d)};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
</script>
|