nvd3/examples/multiBarWithLegend.html

216 lines
5.0 KiB
HTML
Raw Normal View History

<!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;
}
#chart1 {
height: 500px;
margin: 10px;
min-width: 100px;
min-height: 100px;
/*
Minimum height and width is a good idea to prevent negative SVG dimensions...
For example width should be =< margin.left + margin.right + 1,
of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
*/
}
</style>
<body>
<div id="chart1">
</div>
<script src="../lib/d3.v2.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/tooltip.js"></script>
<script src="../src/models/legend.js"></script>
<script src="../src/models/axis.js"></script>
<script src="../src/models/multiBar.js"></script>
<script src="../src/models/multiBarWithLegend.js"></script>
<script src="stream_layers.js"></script>
<script>
var test_data = stream_layers(3,Math.random()*200,.1).map(function(data, i) {
return {
key: 'Stream' + i,
values: data
};
});
historicalBarChart = [
{
key: "Cumulative Return",
values: [
{
"label" : "CDS / Options" ,
"color": true,
"value" : -29.765957771107
} ,
{
"label" : "Cash" ,
"color": true,
"value" : 0
} ,
{
"label" : "Corporate Bonds" ,
"color": true,
"value" : 32.807804682612
} ,
{
"label" : "Equity" ,
"color": true,
"value" : 196.45946739256
} ,
{
"label" : "Index Futures" ,
"color": true,
"value" : 0.19434030906893
} ,
{
"label" : "Options" ,
"color": true,
"value" : -98.079782601442
} ,
{
"label" : "Preferred" ,
"color": true,
"value" : -13.925743130903
} ,
{
"label" : "Not Available" ,
"color": true,
"value" : -5.1387322875705
}
]
}
];
var selector = '#chart1',
chart = nv.models.multiBarWithLegend()
.x(function(d) { return d.label })
.y(function(d) { return d.value })
.showControls(false)
.showLegend(false),
data = historicalBarChart,
xTickFormat = function(d) { return d },
yTickFormat = d3.format(',.2f'),
xAxisLabel = null,
yAxisLabel = null,
duration = 500;
nv.addGraph({
generate: function() {
var container = d3.select(selector),
width = function() { return parseInt(container.style('width')) },
height = function() { return parseInt(container.style('height')) },
svg = container.append('svg');
chart
.width(width)
.height(height);
chart.xAxis
.tickFormat(xTickFormat);
chart.yAxis
.tickFormat(yTickFormat)
.axisLabel(yAxisLabel);
svg
.attr('width', width())
.attr('height', height())
.datum(data)
.transition().duration(duration).call(chart);
return chart;
},
callback: function(chart) {
var showTooltip = function(e) {
var offsetElement = document.getElementById(selector.substr(1)),
left = e.pos[0] + offsetElement.offsetLeft,
top = e.pos[1] + offsetElement.offsetTop,
formatY = chart.yAxis.tickFormat(), //Assumes using same format as axis, can customize to show higher precision, etc.
formatX = chart.xAxis.tickFormat();
// uses the chart's getX and getY, you may customize if x position is not the same as the value you want
// ex. daily data without weekends, x is the index, while you want the date
var content = '<h3>' + e.series.key + '</h3>' +
'<p>' +
formatY(chart.y()(e.point)) + ' at ' + formatX(chart.x()(e.point)) +
'</p>';
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's');
};
chart.dispatch.on('tooltipShow', showTooltip);
chart.dispatch.on('tooltipHide', nv.tooltip.cleanup);
window.onresize= function() {
// now that width and height are functions, should be automatic..of course you can always override them
d3.select('#chart1 svg')
.attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
.attr('height', chart.height()())
.transition().duration(duration)
.call(chart);
/*
d3.select('#chart1 svg')
.attr('width', chart.width()()) //need to set SVG dimensions, chart is not aware of the SVG component
.attr('height', chart.height()())
.call(chart);
*/
};
}
});
function sinAndCos() {
var sin = [],
cos = [];
for (var i = 0; i < 100; i++) {
sin.push({x: i, y: Math.sin(i/10)});
cos.push({x: i, y: .5 * Math.cos(i/10)});
}
return [
{
values: sin,
key: "Sine Wave",
color: "#ff7f0e"
},
{
values: cos,
key: "Cosine Wave",
color: "#2ca02c"
}
];
}
</script>