2 nv.models.multiBar = function() {
4 //============================================================
5 // Public Variables with Default Settings
6 //------------------------------------------------------------
8 var margin = {top: 0, right: 0, bottom: 0, left: 0}
11 , x = d3.scale.ordinal()
12 , y = d3.scale.linear()
13 , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
14 , getX = function(d) { return d.x }
15 , getY = function(d) { return d.y }
16 , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
19 , stackOffset = 'zero' // options include 'silhouette', 'wiggle', 'expand', 'zero', or a custom function
20 , color = nv.utils.defaultColor()
22 , barColor = null // adding the ability to set the color for each rather than the whole group
23 , disabled // used in conjunction with barColor to communicate from multiBarHorizontalChart what series are disabled
30 , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout')
33 //============================================================
36 //============================================================
38 //------------------------------------------------------------
40 var x0, y0 //used to store previous scales
43 //============================================================
46 function chart(selection) {
47 selection.each(function(data) {
48 var availableWidth = width - margin.left - margin.right,
49 availableHeight = height - margin.top - margin.bottom,
50 container = d3.select(this);
52 if(hideable && data.length) hideable = [{
53 values: data[0].values.map(function(d) {
63 data = d3.layout.stack()
65 .values(function(d){ return d.values })
67 (!data.length && hideable ? hideable : data);
70 //add series index to each data point for reference
71 data.forEach(function(series, i) {
72 series.values.forEach(function(point) {
78 //------------------------------------------------------------
79 // HACK for negative value stacking
81 data[0].values.map(function(d,i) {
82 var posBase = 0, negBase = 0;
83 data.map(function(d) {
85 f.size = Math.abs(f.y);
88 negBase = negBase - f.size;
91 f.y1 = f.size + posBase;
92 posBase = posBase + f.size;
97 //------------------------------------------------------------
100 // remap and flatten the data for use in calculating the scales' domains
101 var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
102 data.map(function(d) {
103 return d.values.map(function(d,i) {
104 return { x: getX(d,i), y: getY(d,i), y0: d.y0, y1: d.y1 }
108 x .domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
109 .rangeBands(xRange || [0, availableWidth], groupSpacing);
111 //y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y + (stacked ? d.y1 : 0) }).concat(forceY)))
112 y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return stacked ? (d.y > 0 ? d.y1 : d.y1 + d.y ) : d.y }).concat(forceY)))
113 .range(yRange || [availableHeight, 0]);
115 // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
116 if (x.domain()[0] === x.domain()[1])
118 x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
121 if (y.domain()[0] === y.domain()[1])
123 y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
130 //------------------------------------------------------------
133 //------------------------------------------------------------
134 // Setup containers and skeleton of chart
136 var wrap = container.selectAll('g.nv-wrap.nv-multibar').data([data]);
137 var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multibar');
138 var defsEnter = wrapEnter.append('defs');
139 var gEnter = wrapEnter.append('g');
140 var g = wrap.select('g')
142 gEnter.append('g').attr('class', 'nv-groups');
144 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
146 //------------------------------------------------------------
150 defsEnter.append('clipPath')
151 .attr('id', 'nv-edge-clip-' + id)
153 wrap.select('#nv-edge-clip-' + id + ' rect')
154 .attr('width', availableWidth)
155 .attr('height', availableHeight);
157 g .attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
161 var groups = wrap.select('.nv-groups').selectAll('.nv-group')
162 .data(function(d) { return d }, function(d,i) { return i });
163 groups.enter().append('g')
164 .style('stroke-opacity', 1e-6)
165 .style('fill-opacity', 1e-6);
168 .selectAll('rect.nv-bar')
169 .delay(function(d,i) {
170 return i * delay/ data[0].values.length;
172 .attr('y', function(d) { return stacked ? y0(d.y0) : y0(0) })
176 .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
177 .classed('hover', function(d) { return d.hover })
178 .style('fill', function(d,i){ return color(d, i) })
179 .style('stroke', function(d,i){ return color(d, i) });
182 .style('stroke-opacity', 1)
183 .style('fill-opacity', .75);
186 var bars = groups.selectAll('rect.nv-bar')
187 .data(function(d) { return (hideable && !data.length) ? hideable.values : d.values });
189 bars.exit().remove();
192 var barsEnter = bars.enter().append('rect')
193 .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
194 .attr('x', function(d,i,j) {
195 return stacked ? 0 : (j * x.rangeBand() / data.length )
197 .attr('y', function(d) { return y0(stacked ? d.y0 : 0) })
199 .attr('width', x.rangeBand() / (stacked ? 1 : data.length) )
200 .attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
203 .style('fill', function(d,i,j){ return color(d, j, i); })
204 .style('stroke', function(d,i,j){ return color(d, j, i); })
205 .on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
206 d3.select(this).classed('hover', true);
207 dispatch.elementMouseover({
210 series: data[d.series],
211 pos: [x(getX(d,i)) + (x.rangeBand() * (stacked ? data.length / 2 : d.series + .5) / data.length), y(getY(d,i) + (stacked ? d.y0 : 0))], // TODO: Figure out why the value appears to be shifted
213 seriesIndex: d.series,
217 .on('mouseout', function(d,i) {
218 d3.select(this).classed('hover', false);
219 dispatch.elementMouseout({
222 series: data[d.series],
224 seriesIndex: d.series,
228 .on('click', function(d,i) {
229 dispatch.elementClick({
232 series: data[d.series],
233 pos: [x(getX(d,i)) + (x.rangeBand() * (stacked ? data.length / 2 : d.series + .5) / data.length), y(getY(d,i) + (stacked ? d.y0 : 0))], // TODO: Figure out why the value appears to be shifted
235 seriesIndex: d.series,
238 d3.event.stopPropagation();
240 .on('dblclick', function(d,i) {
241 dispatch.elementDblClick({
244 series: data[d.series],
245 pos: [x(getX(d,i)) + (x.rangeBand() * (stacked ? data.length / 2 : d.series + .5) / data.length), y(getY(d,i) + (stacked ? d.y0 : 0))], // TODO: Figure out why the value appears to be shifted
247 seriesIndex: d.series,
250 d3.event.stopPropagation();
253 .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
255 .attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
258 if (!disabled) disabled = data.map(function() { return true });
260 .style('fill', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); })
261 .style('stroke', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); });
267 .delay(function(d,i) {
269 return i * delay / data[0].values.length;
271 .attr('y', function(d,i) {
273 return y((stacked ? d.y1 : 0));
275 .attr('height', function(d,i) {
276 return Math.max(Math.abs(y(d.y + (stacked ? d.y0 : 0)) - y((stacked ? d.y0 : 0))),1);
278 .attr('x', function(d,i) {
279 return stacked ? 0 : (d.series * x.rangeBand() / data.length )
281 .attr('width', x.rangeBand() / (stacked ? 1 : data.length) );
284 .delay(function(d,i) {
285 return i * delay/ data[0].values.length;
287 .attr('x', function(d,i) {
288 return d.series * x.rangeBand() / data.length
290 .attr('width', x.rangeBand() / data.length)
291 .attr('y', function(d,i) {
292 return getY(d,i) < 0 ?
294 y(0) - y(getY(d,i)) < 1 ?
298 .attr('height', function(d,i) {
299 return Math.max(Math.abs(y(getY(d,i)) - y(0)),1) || 0;
304 //store old scales for use in transitions on update
314 //============================================================
315 // Expose Public Variables
316 //------------------------------------------------------------
318 chart.dispatch = dispatch;
320 chart.options = nv.utils.optionsFunc.bind(chart);
322 chart.x = function(_) {
323 if (!arguments.length) return getX;
328 chart.y = function(_) {
329 if (!arguments.length) return getY;
334 chart.margin = function(_) {
335 if (!arguments.length) return margin;
336 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
337 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
338 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
339 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
343 chart.width = function(_) {
344 if (!arguments.length) return width;
349 chart.height = function(_) {
350 if (!arguments.length) return height;
355 chart.xScale = function(_) {
356 if (!arguments.length) return x;
361 chart.yScale = function(_) {
362 if (!arguments.length) return y;
367 chart.xDomain = function(_) {
368 if (!arguments.length) return xDomain;
373 chart.yDomain = function(_) {
374 if (!arguments.length) return yDomain;
379 chart.xRange = function(_) {
380 if (!arguments.length) return xRange;
385 chart.yRange = function(_) {
386 if (!arguments.length) return yRange;
391 chart.forceY = function(_) {
392 if (!arguments.length) return forceY;
397 chart.stacked = function(_) {
398 if (!arguments.length) return stacked;
403 chart.stackOffset = function(_) {
404 if (!arguments.length) return stackOffset;
409 chart.clipEdge = function(_) {
410 if (!arguments.length) return clipEdge;
415 chart.color = function(_) {
416 if (!arguments.length) return color;
417 color = nv.utils.getColor(_);
421 chart.barColor = function(_) {
422 if (!arguments.length) return barColor;
423 barColor = nv.utils.getColor(_);
427 chart.disabled = function(_) {
428 if (!arguments.length) return disabled;
433 chart.id = function(_) {
434 if (!arguments.length) return id;
439 chart.hideable = function(_) {
440 if (!arguments.length) return hideable;
445 chart.delay = function(_) {
446 if (!arguments.length) return delay;
451 chart.groupSpacing = function(_) {
452 if (!arguments.length) return groupSpacing;
457 //============================================================