You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nvd3/examples/stackedAreaWithLegend.html

140 lines
3.3 KiB
HTML

<!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;
}
#chart {
margin: 10px;
height: 500px;
}
</style>
<body>
<div id="chart">
<svg></svg>
</div>
<script src="../lib/d3.v2.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/tooltip.js"></script>
<script src="../src/models/xaxis.js"></script>
<script src="../src/models/yaxis.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/line.js"></script>
<script src="../src/models/stackedArea.js"></script>
<script src="../src/models/stackedAreaWithLegend.js"></script>
<script>
var n = 4, // number of layers
m = 120; // number of samples per layer
//color = d3.interpolateRgb("#aad", "#556");
//format data to our liking, add keys
var data = stream_layers(n,m).map(function(data, i) {
return {
key: 'Stream' + i,
values: data
};
});
var width = function() { return parseInt(d3.select('#chart').style('width')) },
height = function() { return parseInt(d3.select('#chart').style('height')) };
var chart = nv.models.stackedAreaWithLegend()
.width(width)
.height(height)
//.y(function(d) { return d.ytest }) //not ready yet for custom accessor, almost there
var svg = d3.select('#chart svg')
.attr('width', width())
.attr('height', height())
.datum(data)
svg.transition().duration(500).call(chart);
chart.dispatch.on('tooltipShow', function(e) {
var offsetElement = document.getElementById("chart"),
left = e.pos[0] + offsetElement.offsetLeft,
top = e.pos[1] + offsetElement.offsetTop,
formatterY = chart.stacked.offset() == 'expand' ? d3.format(',.2%') : d3.format(',.2f'), //TODO: stacked format should be set by caller
formatterX = function(d) { return d };
var content = '<h3>' + e.series.key + '</h3>' +
'<p>' +
formatterY(chart.y()(e.point)) + ' on ' + formatterX(chart.x()(e.point)) +
'</p>';
nv.tooltip.show([left, top], content);
});
chart.dispatch.on('tooltipHide', function(e) {
nv.tooltip.cleanup();
});
window.onresize = function() {
d3.select('#chart svg')
.attr('width', width())
.attr('height', height())
.call(chart);
};
/* 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, ytest: Math.max(0, d)}; //Minor error with custom y getter/setter and stacked, need to fix
return {x: i, y: Math.max(0, d)};
}
</script>