in mc/Scripts/nv.d3.js [7649:7976]
function chart(selection) {
renderWatch.reset();
selection.each(function(data) {
var availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
container = d3.select(this);
nv.utils.initSVG(container);
var nonStackableCount = 0;
// This function defines the requirements for render complete
var endFn = function(d, i) {
if (d.series === data.length - 1 && i === data[0].values.length - 1)
return true;
return false;
};
if(hideable && data.length) hideable = [{
values: data[0].values.map(function(d) {
return {
x: d.x,
y: 0,
series: d.series,
size: 0.01
};}
)}];
if (stacked) {
var parsed = d3.layout.stack()
.offset(stackOffset)
.values(function(d){ return d.values })
.y(getY)
(!data.length && hideable ? hideable : data);
parsed.forEach(function(series, i){
// if series is non-stackable, use un-parsed data
if (series.nonStackable) {
data[i].nonStackableSeries = nonStackableCount++;
parsed[i] = data[i];
} else {
// don't stack this seires on top of the nonStackable seriees
if (i > 0 && parsed[i - 1].nonStackable){
parsed[i].values.map(function(d,j){
d.y0 -= parsed[i - 1].values[j].y;
d.y1 = d.y0 + d.y;
});
}
}
});
data = parsed;
}
//add series index and key to each data point for reference
data.forEach(function(series, i) {
series.values.forEach(function(point) {
point.series = i;
point.key = series.key;
});
});
// HACK for negative value stacking
if (stacked) {
data[0].values.map(function(d,i) {
var posBase = 0, negBase = 0;
data.map(function(d, idx) {
if (!data[idx].nonStackable) {
var f = d.values[i]
f.size = Math.abs(f.y);
if (f.y<0) {
f.y1 = negBase;
negBase = negBase - f.size;
} else
{
f.y1 = f.size + posBase;
posBase = posBase + f.size;
}
}
});
});
}
// Setup Scales
// remap and flatten the data for use in calculating the scales' domains
var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
data.map(function(d, idx) {
return d.values.map(function(d,i) {
return { x: getX(d,i), y: getY(d,i), y0: d.y0, y1: d.y1, idx:idx }
})
});
x.domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
.rangeBands(xRange || [0, availableWidth], groupSpacing);
y.domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) {
var domain = d.y;
// increase the domain range if this series is stackable
if (stacked && !data[d.idx].nonStackable) {
if (d.y > 0){
domain = d.y1
} else {
domain = d.y1 + d.y
}
}
return domain;
}).concat(forceY)))
.range(yRange || [availableHeight, 0]);
// If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
if (x.domain()[0] === x.domain()[1])
x.domain()[0] ?
x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
: x.domain([-1,1]);
if (y.domain()[0] === y.domain()[1])
y.domain()[0] ?
y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
: y.domain([-1,1]);
x0 = x0 || x;
y0 = y0 || y;
// Setup containers and skeleton of chart
var wrap = container.selectAll('g.nv-wrap.nv-multibar').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multibar');
var defsEnter = wrapEnter.append('defs');
var gEnter = wrapEnter.append('g');
var g = wrap.select('g');
gEnter.append('g').attr('class', 'nv-groups');
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
defsEnter.append('clipPath')
.attr('id', 'nv-edge-clip-' + id)
.append('rect');
wrap.select('#nv-edge-clip-' + id + ' rect')
.attr('width', availableWidth)
.attr('height', availableHeight);
g.attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
var groups = wrap.select('.nv-groups').selectAll('.nv-group')
.data(function(d) { return d }, function(d,i) { return i });
groups.enter().append('g')
.style('stroke-opacity', 1e-6)
.style('fill-opacity', 1e-6);
var exitTransition = renderWatch
.transition(groups.exit().selectAll('rect.nv-bar'), 'multibarExit', Math.min(100, duration))
.attr('y', function(d, i, j) {
var yVal = y0(0) || 0;
if (stacked) {
if (data[d.series] && !data[d.series].nonStackable) {
yVal = y0(d.y0);
}
}
return yVal;
})
.attr('height', 0)
.remove();
if (exitTransition.delay)
exitTransition.delay(function(d,i) {
var delay = i * (duration / (last_datalength + 1)) - i;
return delay;
});
groups
.attr('class', function(d,i) { return 'nv-group nv-series-' + i })
.classed('hover', function(d) { return d.hover })
.style('fill', function(d,i){ return color(d, i) })
.style('stroke', function(d,i){ return color(d, i) });
groups
.style('stroke-opacity', 1)
.style('fill-opacity', 0.75);
var bars = groups.selectAll('rect.nv-bar')
.data(function(d) { return (hideable && !data.length) ? hideable.values : d.values });
bars.exit().remove();
var barsEnter = bars.enter().append('rect')
.attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
.attr('x', function(d,i,j) {
return stacked && !data[j].nonStackable ? 0 : (j * x.rangeBand() / data.length )
})
.attr('y', function(d,i,j) { return y0(stacked && !data[j].nonStackable ? d.y0 : 0) || 0 })
.attr('height', 0)
.attr('width', function(d,i,j) { return x.rangeBand() / (stacked && !data[j].nonStackable ? 1 : data.length) })
.attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
;
bars
.style('fill', function(d,i,j){ return color(d, j, i); })
.style('stroke', function(d,i,j){ return color(d, j, i); })
.on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
d3.select(this).classed('hover', true);
dispatch.elementMouseover({
data: d,
index: i,
color: d3.select(this).style("fill")
});
})
.on('mouseout', function(d,i) {
d3.select(this).classed('hover', false);
dispatch.elementMouseout({
data: d,
index: i,
color: d3.select(this).style("fill")
});
})
.on('mousemove', function(d,i) {
dispatch.elementMousemove({
data: d,
index: i,
color: d3.select(this).style("fill")
});
})
.on('click', function(d,i) {
dispatch.elementClick({
data: d,
index: i,
color: d3.select(this).style("fill")
});
d3.event.stopPropagation();
})
.on('dblclick', function(d,i) {
dispatch.elementDblClick({
data: d,
index: i,
color: d3.select(this).style("fill")
});
d3.event.stopPropagation();
});
bars
.attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
.attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
if (barColor) {
if (!disabled) disabled = data.map(function() { return true });
bars
.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(); })
.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(); });
}
var barSelection =
bars.watchTransition(renderWatch, 'multibar', Math.min(250, duration))
.delay(function(d,i) {
return i * duration / data[0].values.length;
});
if (stacked){
barSelection
.attr('y', function(d,i,j) {
var yVal = 0;
// if stackable, stack it on top of the previous series
if (!data[j].nonStackable) {
yVal = y(d.y1);
} else {
if (getY(d,i) < 0){
yVal = y(0);
} else {
if (y(0) - y(getY(d,i)) < -1){
yVal = y(0) - 1;
} else {
yVal = y(getY(d, i)) || 0;
}
}
}
return yVal;
})
.attr('height', function(d,i,j) {
if (!data[j].nonStackable) {
return Math.max(Math.abs(y(d.y+d.y0) - y(d.y0)), 1);
} else {
return Math.max(Math.abs(y(getY(d,i)) - y(0)),1) || 0;
}
})
.attr('x', function(d,i,j) {
var width = 0;
if (data[j].nonStackable) {
width = d.series * x.rangeBand() / data.length;
if (data.length !== nonStackableCount){
width = data[j].nonStackableSeries * x.rangeBand()/(nonStackableCount*2);
}
}
return width;
})
.attr('width', function(d,i,j){
if (!data[j].nonStackable) {
return x.rangeBand();
} else {
// if all series are nonStacable, take the full width
var width = (x.rangeBand() / nonStackableCount);
// otherwise, nonStackable graph will be only taking the half-width
// of the x rangeBand
if (data.length !== nonStackableCount) {
width = x.rangeBand()/(nonStackableCount*2);
}
return width;
}
});
}
else {
barSelection
.attr('x', function(d,i) {
return d.series * x.rangeBand() / data.length;
})
.attr('width', x.rangeBand() / data.length)
.attr('y', function(d,i) {
return getY(d,i) < 0 ?
y(0) :
y(0) - y(getY(d,i)) < 1 ?
y(0) - 1 :
y(getY(d,i)) || 0;
})
.attr('height', function(d,i) {
return Math.max(Math.abs(y(getY(d,i)) - y(0)),1) || 0;
});
}
//store old scales for use in transitions on update
x0 = x.copy();
y0 = y.copy();
// keep track of the last data value length for transition calculations
if (data[0] && data[0].values) {
last_datalength = data[0].values.length;
}
});
renderWatch.renderEnd('multibar immediate');
return chart;
}