Merging in most of PullRequest #16
commit
a8b080640f
@ -0,0 +1,129 @@
|
||||
<!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;
|
||||
}
|
||||
|
||||
svg {
|
||||
display: block;
|
||||
}
|
||||
|
||||
#wrapper {
|
||||
width: 960;
|
||||
padding: 100px;
|
||||
}
|
||||
|
||||
#chart1 {
|
||||
width: 960px;
|
||||
}
|
||||
|
||||
#chart1 svg{
|
||||
height: 500px;
|
||||
min-width: 100px;
|
||||
min-height: 100px;
|
||||
/*
|
||||
margin: 10px;
|
||||
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="wrapper">
|
||||
|
||||
<div id="chart1">
|
||||
<svg></svg>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script src="../lib/d3.v2.js"></script>
|
||||
<script src="../nv.d3.js"></script>
|
||||
<!-- including all the components so I don't have to minify every time I test in development -->
|
||||
<script src="../src/tooltip.js"></script>
|
||||
<script src="../src/models/axis.js"></script>
|
||||
<script src="../src/models/discreteBar.js"></script>
|
||||
<script src="../src/models/discreteBarChartWithEnabledTooltip.js"></script>
|
||||
<script>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
historicalBarChart = [
|
||||
{
|
||||
key: "Cumulative Return",
|
||||
values: [
|
||||
{
|
||||
"label" : "CDS / Options" ,
|
||||
"value" : -29.765957771107
|
||||
} ,
|
||||
{
|
||||
"label" : "Cash" ,
|
||||
"value" : 0
|
||||
} ,
|
||||
{
|
||||
"label" : "Corporate Bonds" ,
|
||||
"value" : 32.807804682612
|
||||
} ,
|
||||
{
|
||||
"label" : "Equity" ,
|
||||
"value" : 196.45946739256
|
||||
} ,
|
||||
{
|
||||
"label" : "Index Futures" ,
|
||||
"value" : 0.19434030906893
|
||||
} ,
|
||||
{
|
||||
"label" : "Options" ,
|
||||
"value" : -98.079782601442
|
||||
} ,
|
||||
{
|
||||
"label" : "Preferred" ,
|
||||
"value" : -13.925743130903
|
||||
} ,
|
||||
{
|
||||
"label" : "Not Available" ,
|
||||
"value" : -5.1387322875705
|
||||
}
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
nv.addGraph(function() {
|
||||
var chart = nv.models.discreteBarChart()
|
||||
.x(function(d) { return d.label })
|
||||
.y(function(d) { return d.value })
|
||||
.staggerLabels(true)
|
||||
//.staggerLabels(historicalBarChart[0].values.length > 8)
|
||||
.tooltips(true)
|
||||
.showValues(true)
|
||||
|
||||
|
||||
d3.select('#chart1 svg')
|
||||
.datum(historicalBarChart)
|
||||
.transition().duration(500)
|
||||
.call(chart);
|
||||
|
||||
nv.utils.windowResize(chart.update);
|
||||
|
||||
return chart;
|
||||
});
|
||||
|
||||
|
||||
</script>
|
||||
</body>
|
@ -0,0 +1,222 @@
|
||||
|
||||
nv.models.discreteBarChart = function() {
|
||||
var margin = {top: 30, right: 20, bottom: 50, left: 60},
|
||||
width = null,
|
||||
height = null,
|
||||
color = d3.scale.category20().range(),
|
||||
staggerLabels = false,
|
||||
tooltips = true,
|
||||
tooltip = function(key, x, y, e, graph) {
|
||||
return '<h3>' + x + '</h3>' +
|
||||
'<p>' + y + '</p>'
|
||||
};
|
||||
|
||||
|
||||
var discretebar = nv.models.discreteBar(),
|
||||
x = discretebar.xScale(),
|
||||
y = discretebar.yScale(),
|
||||
xAxis = nv.models.axis().scale(x).orient('bottom').highlightZero(false).showMaxMin(false),
|
||||
yAxis = nv.models.axis().scale(y).orient('left'),
|
||||
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
|
||||
|
||||
xAxis.tickFormat(function(d) { return d });
|
||||
yAxis.tickFormat(d3.format(',.1f'));
|
||||
|
||||
|
||||
var showTooltip = function(e, offsetElement) {
|
||||
var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
|
||||
top = e.pos[1] + ( offsetElement.offsetTop || 0),
|
||||
x = xAxis.tickFormat()(discretebar.x()(e.point)),
|
||||
y = yAxis.tickFormat()(discretebar.y()(e.point)),
|
||||
content = tooltip(e.series.key, x, y, e, chart);
|
||||
|
||||
nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
|
||||
};
|
||||
|
||||
|
||||
//TODO: let user select default
|
||||
var controlsData = [
|
||||
{ key: 'Grouped' },
|
||||
{ key: 'Stacked', disabled: true }
|
||||
];
|
||||
|
||||
function chart(selection) {
|
||||
selection.each(function(data) {
|
||||
var container = d3.select(this),
|
||||
that = this;
|
||||
|
||||
var availableWidth = (width || parseInt(container.style('width')) || 960)
|
||||
- margin.left - margin.right,
|
||||
availableHeight = (height || parseInt(container.style('height')) || 400)
|
||||
- margin.top - margin.bottom;
|
||||
|
||||
|
||||
discretebar
|
||||
.width(availableWidth)
|
||||
.height(availableHeight);
|
||||
|
||||
|
||||
var wrap = container.selectAll('g.wrap.discreteBarWithAxes').data([data]);
|
||||
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 discreteBarWithAxes').append('g');
|
||||
var defsEnter = gEnter.append('defs');
|
||||
|
||||
gEnter.append('g').attr('class', 'x axis');
|
||||
gEnter.append('g').attr('class', 'y axis');
|
||||
gEnter.append('g').attr('class', 'barsWrap');
|
||||
|
||||
|
||||
|
||||
var g = wrap.select('g');
|
||||
|
||||
|
||||
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
||||
|
||||
var barsWrap = g.select('.barsWrap')
|
||||
.datum(data.filter(function(d) { return !d.disabled }))
|
||||
|
||||
|
||||
d3.transition(barsWrap).call(discretebar);
|
||||
|
||||
|
||||
defsEnter.append('clipPath')
|
||||
.attr('id', 'x-label-clip-' + discretebar.id())
|
||||
.append('rect')
|
||||
|
||||
g.select('#x-label-clip-' + discretebar.id() + ' rect')
|
||||
.attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
|
||||
.attr('height', 16)
|
||||
.attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
|
||||
|
||||
/*
|
||||
var evenLabelClips = defsEnter.append('clipPath')
|
||||
.attr('id', 'x-label-clip-even-' + discretebar.id())
|
||||
.selectAll('rect')
|
||||
.data(function(d) { return d[0].values.filter(function(d,i) { return i % 2 === 0 }) });
|
||||
|
||||
evenLabelClips.enter().append('rect')
|
||||
.attr('width', x.rangeBand())
|
||||
.attr('height', 32)
|
||||
.attr('y', y.range()[0])
|
||||
.attr('x', function(d,i) { return x(discretebar.x()(d,i)) });
|
||||
|
||||
var oddLabelClips = defsEnter.append('clipPath')
|
||||
.attr('id', 'x-label-clip-odd-' + discretebar.id())
|
||||
.selectAll('rect')
|
||||
.data(function(d) { return d[0].values.filter(function(d,i) { return i % 2 === 1 }) });
|
||||
|
||||
oddLabelClips.enter().append('rect')
|
||||
.attr('width', x.rangeBand())
|
||||
.attr('height', 16)
|
||||
.attr('y', y.range()[0] + 16 + (staggerLabels ? 12: 0))
|
||||
.attr('x', function(d,i) { return x(discretebar.x()(d,i)) });
|
||||
*/
|
||||
|
||||
|
||||
|
||||
xAxis
|
||||
.ticks( availableWidth / 100 )
|
||||
.tickSize(-availableHeight, 0);
|
||||
|
||||
g.select('.x.axis')
|
||||
.attr('transform', 'translate(0,' + (y.range()[0] + (discretebar.showValues() ? 16 : 0)) + ')')
|
||||
//d3.transition(g.select('.x.axis'))
|
||||
g.select('.x.axis').transition().duration(0)
|
||||
.call(xAxis);
|
||||
|
||||
|
||||
var xTicks = g.select('.x.axis').selectAll('g');
|
||||
|
||||
if (staggerLabels)
|
||||
xTicks
|
||||
.selectAll('text')
|
||||
.attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '0' : '12') + ')' })
|
||||
|
||||
xTicks
|
||||
.selectAll('text')
|
||||
.attr('clip-path', function(d,i,j) { return 'url(#x-label-clip-' + discretebar.id() + ')' });
|
||||
|
||||
|
||||
yAxis
|
||||
.ticks( availableHeight / 36 )
|
||||
.tickSize( -availableWidth, 0);
|
||||
|
||||
d3.transition(g.select('.y.axis'))
|
||||
.call(yAxis);
|
||||
|
||||
|
||||
discretebar.dispatch.on('elementMouseover.tooltip', function(e) {
|
||||
e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
|
||||
dispatch.tooltipShow(e);
|
||||
});
|
||||
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
|
||||
|
||||
discretebar.dispatch.on('elementMouseout.tooltip', function(e) {
|
||||
dispatch.tooltipHide(e);
|
||||
});
|
||||
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
|
||||
|
||||
|
||||
//TODO: decide if this makes sense to add into all the models for ease of updating (updating without needing the selection)
|
||||
chart.update = function() { selection.transition().call(chart); };
|
||||
chart.container = this; // I need a reference to the container in order to have outside code check if the chart is visible or not
|
||||
|
||||
});
|
||||
|
||||
return chart;
|
||||
}
|
||||
|
||||
|
||||
chart.dispatch = dispatch;
|
||||
chart.discretebar = discretebar; // really just makign the accessible for discretebar.dispatch, may rethink slightly
|
||||
chart.xAxis = xAxis;
|
||||
chart.yAxis = yAxis;
|
||||
|
||||
d3.rebind(chart, discretebar, 'x', 'y', 'xDomain', 'yDomain', 'forceX', 'forceY', 'id', 'showValues', 'valueFormat');
|
||||
|
||||
|
||||
chart.margin = function(_) {
|
||||
if (!arguments.length) return margin;
|
||||
margin = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.width = function(_) {
|
||||
if (!arguments.length) return width;
|
||||
width = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.height = function(_) {
|
||||
if (!arguments.length) return height;
|
||||
height = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.color = function(_) {
|
||||
if (!arguments.length) return color;
|
||||
color = _;
|
||||
discretebar.color(_);
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.staggerLabels = function(_) {
|
||||
if (!arguments.length) return staggerLabels;
|
||||
staggerLabels = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.tooltips = function(_) {
|
||||
if (!arguments.length) return tooltips;
|
||||
tooltips = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
chart.tooltipContent = function(_) {
|
||||
if (!arguments.length) return tooltip;
|
||||
tooltip = _;
|
||||
return chart;
|
||||
};
|
||||
|
||||
|
||||
return chart;
|
||||
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue