2012-06-08 21:21:08 +00:00
|
|
|
|
|
|
|
nv.models.discreteBarWithAxes = function() {
|
|
|
|
var margin = {top: 30, right: 20, bottom: 50, left: 60},
|
|
|
|
width = function() { return 960 },
|
|
|
|
height = function() { return 500 },
|
2012-06-09 09:11:05 +00:00
|
|
|
color = d3.scale.category20().range(),
|
|
|
|
staggerLabels = false;
|
2012-06-08 21:21:08 +00:00
|
|
|
|
2012-06-09 08:46:18 +00:00
|
|
|
var discretebar = nv.models.discreteBar(),
|
|
|
|
x = discretebar.xScale(),
|
|
|
|
y = discretebar.yScale(),
|
2012-06-08 21:21:08 +00:00
|
|
|
xAxis = nv.models.axis().scale(x).orient('bottom').highlightZero(false),
|
|
|
|
yAxis = nv.models.axis().scale(y).orient('left'),
|
|
|
|
dispatch = d3.dispatch('tooltipShow', 'tooltipHide');
|
|
|
|
|
|
|
|
//TODO: let user select default
|
|
|
|
var controlsData = [
|
|
|
|
{ key: 'Grouped' },
|
|
|
|
{ key: 'Stacked', disabled: true }
|
|
|
|
];
|
|
|
|
|
|
|
|
function chart(selection) {
|
|
|
|
selection.each(function(data) {
|
|
|
|
var availableWidth = width() - margin.left - margin.right,
|
2012-06-09 08:46:18 +00:00
|
|
|
availableHeight = height() - margin.top - margin.bottom;
|
|
|
|
|
|
|
|
var seriesData = data.filter(function(d) { return !d.disabled })
|
2012-06-08 21:21:08 +00:00
|
|
|
.map(function(d) {
|
|
|
|
return d.values.map(function(d,i) {
|
|
|
|
return { x: discretebar.x()(d,i), y: discretebar.y()(d,i) }
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
discretebar
|
|
|
|
.width(availableWidth)
|
2012-06-09 08:46:18 +00:00
|
|
|
.height(availableHeight);
|
2012-06-08 21:21:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
var wrap = d3.select(this).selectAll('g.wrap.discreteBarWithAxes').data([data]);
|
|
|
|
var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 discreteBarWithAxes').append('g');
|
|
|
|
|
|
|
|
gEnter.append('g').attr('class', 'x axis');
|
|
|
|
gEnter.append('g').attr('class', 'y axis');
|
2012-06-09 08:46:18 +00:00
|
|
|
gEnter.append('g').attr('class', 'barsWrap');
|
2012-06-08 21:21:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var g = wrap.select('g');
|
|
|
|
|
|
|
|
|
|
|
|
g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
|
|
|
|
|
2012-06-09 08:46:18 +00:00
|
|
|
var barsWrap = g.select('.barsWrap')
|
2012-06-08 21:21:08 +00:00
|
|
|
.datum(data.filter(function(d) { return !d.disabled }))
|
|
|
|
|
|
|
|
|
2012-06-09 08:46:18 +00:00
|
|
|
d3.transition(barsWrap).call(discretebar);
|
2012-06-08 21:21:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
xAxis
|
|
|
|
.scale(x)
|
|
|
|
.ticks( availableWidth / 100 )
|
|
|
|
.tickSize(-availableHeight, 0);
|
|
|
|
|
|
|
|
g.select('.x.axis')
|
|
|
|
.attr('transform', 'translate(0,' + y.range()[0] + ')');
|
|
|
|
d3.transition(g.select('.x.axis'))
|
|
|
|
.call(xAxis);
|
|
|
|
|
2012-06-09 09:11:05 +00:00
|
|
|
|
2012-06-08 21:21:08 +00:00
|
|
|
var xTicks = g.select('.x.axis').selectAll('g');
|
|
|
|
|
2012-06-09 09:11:05 +00:00
|
|
|
if (staggerLabels)
|
|
|
|
xTicks
|
|
|
|
.selectAll('text')
|
|
|
|
.attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '0' : '12') + ')' })
|
2012-06-08 21:21:08 +00:00
|
|
|
|
2012-06-09 09:11:05 +00:00
|
|
|
/*
|
2012-06-08 21:21:08 +00:00
|
|
|
xTicks.filter(function(d,i) {
|
|
|
|
return i % Math.ceil(data[0].values.length / (availableWidth / 100)) !== 0;
|
|
|
|
})
|
|
|
|
.selectAll('line, text')
|
|
|
|
.style('opacity', 0)
|
2012-06-09 09:11:05 +00:00
|
|
|
*/
|
2012-06-08 21:21:08 +00:00
|
|
|
|
|
|
|
yAxis
|
2012-06-09 08:46:18 +00:00
|
|
|
.scale(y)
|
2012-06-08 21:21:08 +00:00
|
|
|
.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);
|
|
|
|
});
|
|
|
|
|
|
|
|
discretebar.dispatch.on('elementMouseout.tooltip', function(e) {
|
|
|
|
dispatch.tooltipHide(e);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
chart.dispatch = dispatch;
|
|
|
|
chart.xAxis = xAxis;
|
|
|
|
chart.yAxis = yAxis;
|
|
|
|
|
|
|
|
d3.rebind(chart, discretebar, 'x', 'y', 'xDomain', 'yDomain', 'forceX', 'forceY', 'clipEdge', 'id');
|
|
|
|
|
|
|
|
|
|
|
|
chart.margin = function(_) {
|
|
|
|
if (!arguments.length) return margin;
|
|
|
|
margin = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.width = function(_) {
|
|
|
|
if (!arguments.length) return width;
|
|
|
|
width = d3.functor(_);
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.height = function(_) {
|
|
|
|
if (!arguments.length) return height;
|
|
|
|
height = d3.functor(_);
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
|
|
|
chart.color = function(_) {
|
|
|
|
if (!arguments.length) return color;
|
|
|
|
color = _;
|
|
|
|
discretebar.color(_);
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-06-09 09:11:05 +00:00
|
|
|
chart.staggerLabels = function(_) {
|
|
|
|
if (!arguments.length) return staggerLabels;
|
|
|
|
staggerLabels = _;
|
|
|
|
return chart;
|
|
|
|
};
|
|
|
|
|
2012-06-08 21:21:08 +00:00
|
|
|
|
|
|
|
return chart;
|
|
|
|
}
|