Commit 60e7075a authored by mikhail samoilov's avatar mikhail samoilov

Bar chart rewrite, allow negative values in bar chart

parent 8fc877e3
...@@ -489,7 +489,8 @@ ...@@ -489,7 +489,8 @@
padding_top = isDefined(__axis_y2_padding.top) ? __axis_y2_padding.top : padding; padding_top = isDefined(__axis_y2_padding.top) ? __axis_y2_padding.top : padding;
padding_bottom = isDefined(__axis_y2_padding.bottom) ? __axis_y2_padding.bottom : padding; padding_bottom = isDefined(__axis_y2_padding.bottom) ? __axis_y2_padding.bottom : padding;
} }
return [hasBarType(yTargets) ? 0 : yDomainMin - padding_bottom, yDomainMax + padding_top];
return [yDomainMin - padding_bottom, yDomainMax + padding_top];
} }
function getXDomainRatio(isSub) { function getXDomainRatio(isSub) {
var domain, extent; var domain, extent;
...@@ -862,18 +863,24 @@ ...@@ -862,18 +863,24 @@
return d.x || d.x === 0 ? scale(d.x) - barW * (barTargetsNum / 2 - barIndex) : 0; return d.x || d.x === 0 ? scale(d.x) - barW * (barTargetsNum / 2 - barIndex) : 0;
}; };
} }
function getBarY(barH, barIndices, zeroBased, isSub) { function getBarY(isSub) {
return function (d, i) {
var scale = isSub ? getSubYScale(d.id) : getYScale(d.id);
return scale(d.value);
};
}
function getBarOffset(barIndices, isSub) {
var indicesIds = Object.keys(barIndices); var indicesIds = Object.keys(barIndices);
return function (d, i) { return function (d, i) {
var offset = 0; var offset = 0;
var scale = isSub ? getSubYScale(d.id) : getYScale(d.id); var scale = isSub ? getSubYScale(d.id) : getYScale(d.id);
getTargets(isBarType).forEach(function (t) { getTargets(isBarType).forEach(function (t) {
if (t.id === d.id || barIndices[t.id] !== barIndices[d.id]) { return; } if (t.id === d.id || barIndices[t.id] !== barIndices[d.id]) { return; }
if (indicesIds.indexOf(t.id) < indicesIds.indexOf(d.id)) { if (indicesIds.indexOf(t.id) < indicesIds.indexOf(d.id) && t.values[i].value * d.value > 0) {
offset += barH(t.values[i]); offset += scale(t.values[i].value);
} }
}); });
return zeroBased ? offset : scale(d.value) - offset; return offset;
}; };
} }
function getBarW(axis, barTargetsNum, isSub) { function getBarW(axis, barTargetsNum, isSub) {
...@@ -1784,7 +1791,7 @@ ...@@ -1784,7 +1791,7 @@
.classed(INCLUDED, false); .classed(INCLUDED, false);
dragging = false; dragging = false;
// TODO: add callback here // TODO: add callback here
} }
function redraw(options) { function redraw(options) {
...@@ -1895,26 +1902,57 @@ ...@@ -1895,26 +1902,57 @@
} }
// bars // bars
barW = getBarW(xAxis, barTargetsNum, false);
barH = getBarH(__axis_rotated ? null : height); var barOnMain = function (isSub) {
barX = getBarX(barW, barTargetsNum, barIndices); var barW = getBarW(xAxis, barTargetsNum, !!isSub),
barY = getBarY(barH, barIndices, __axis_rotated); barX = getBarX(barW, barTargetsNum, barIndices),
barY = getBarY(!!isSub),
barOffset = getBarOffset(barIndices);
return function (d, i) {
var y = barY;
var x = barX;
var y0 = getYScale(d.id)(0),
offset = barOffset(d, i) || y0; // offset is for stacked bar chart, by default
// 4 points that make a bar
var points = [
[x(d), offset],
[x(d), y(d) - (y0 - offset)],
[x(d) + barW, y(d) - (y0 - offset)],
[x(d) + barW, offset]
];
// switch points if axis is rotated, not applicable for sub chart
var indexX = isSub || !__axis_rotated ? 0 : 1;
var indexY = isSub || !__axis_rotated ? 1 : 0;
var path = 'M ' + points[0][ indexX] + ',' + points[0][indexY] + ' ' +
'L' + points[1][indexX] + ',' + points[1][indexY] + ' ' +
'L' + points[2][indexX] + ',' + points[2][indexY] + ' ' +
'L' + points[3][indexX] + ',' + points[3][indexY] + ' ' +
'z';
return path;
}
};
mainBar = main.selectAll('.-bars').selectAll('.-bar') mainBar = main.selectAll('.-bars').selectAll('.-bar')
.data(barData); .data(barData);
mainBar.transition().duration(duration)
.attr("x", __axis_rotated ? barY : barX) mainBar.enter().append('path')
.attr("y", __axis_rotated ? barX : barY) .attr('d', barOnMain(false))
.attr("width", __axis_rotated ? barH : barW) .style("stroke", 'none')
.attr("height", __axis_rotated ? barW : barH); .style("fill", function (d) { return color(d.id); })
mainBar.enter().append('rect') .attr("class", classBar);
.attr("class", classBar)
.attr("x", __axis_rotated ? barY : barX) mainBar
.attr("y", __axis_rotated ? barX : barY)
.attr("width", __axis_rotated ? barH : barW)
.attr("height", __axis_rotated ? barW : barH)
.style("opacity", 0) .style("opacity", 0)
.transition().duration(duration) .transition().duration(duration)
.attr('d', barOnMain(false))
.style('opacity', 1); .style('opacity', 1);
mainBar.exit().transition().duration(duration) mainBar.exit().transition().duration(duration)
.style('opacity', 0) .style('opacity', 0)
.remove(); .remove();
...@@ -1981,7 +2019,7 @@ ...@@ -1981,7 +2019,7 @@
// TODO: fix when rotated // TODO: fix when rotated
context.select('.x.axis').transition().duration(__axis_rotated ? duration : 0).call(__axis_rotated ? yAxis : subXAxis); context.select('.x.axis').transition().duration(__axis_rotated ? duration : 0).call(__axis_rotated ? yAxis : subXAxis);
// extent rect // extent rect
if (! brush.empty()) { if (!brush.empty()) {
brush.extent(x.orgDomain()).update(); brush.extent(x.orgDomain()).update();
} }
// bars // bars
...@@ -2004,7 +2042,7 @@ ...@@ -2004,7 +2042,7 @@
.remove(); .remove();
// lines // lines
context.selectAll('.-line') context.selectAll('.-line')
.transition().duration(duration) .transition().duration(duration)
.attr("d", lineOnSub); .attr("d", lineOnSub);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment