Browse Source

Fix padding for data label when rotated axis - #45

pull/66/head
Masayuki Tanaka 11 years ago
parent
commit
db37719fc7
  1. 24
      c3.js
  2. 4
      c3.min.js

24
c3.js

@ -385,8 +385,8 @@
subYMax = __axis_rotated ? width2 : 1; subYMax = __axis_rotated ? width2 : 1;
// update scales // update scales
x = getX(xMin, xMax, x ? x.domain() : undefined, function () { return xAxis.tickOffset(); }); x = getX(xMin, xMax, x ? x.domain() : undefined, function () { return xAxis.tickOffset(); });
y = getY(yMin, yMax, y ? y.domain() : undefined); y = getY(yMin, yMax);
y2 = getY(yMin, yMax, y2 ? y2.domain() : undefined); y2 = getY(yMin, yMax);
subX = getX(xMin, xMax, orgXDomain, function (d) { return d % 1 ? 0 : subXAxis.tickOffset(); }); subX = getX(xMin, xMax, orgXDomain, function (d) { return d % 1 ? 0 : subXAxis.tickOffset(); });
subY = getY(subYMin, subYMax); subY = getY(subYMin, subYMax);
subY2 = getY(subYMin, subYMax); subY2 = getY(subYMin, subYMax);
@ -689,7 +689,8 @@
yMax = axisId === 'y2' ? __axis_y2_max : __axis_y_max, yMax = axisId === 'y2' ? __axis_y2_max : __axis_y_max,
yDomainMin = isValue(yMin) ? yMin : getYDomainMin(yTargets), yDomainMin = isValue(yMin) ? yMin : getYDomainMin(yTargets),
yDomainMax = isValue(yMax) ? yMax : getYDomainMax(yTargets), yDomainMax = isValue(yMax) ? yMax : getYDomainMax(yTargets),
padding = Math.abs(yDomainMax - yDomainMin) * (hasDataLabel() && __axis_rotated ? 0.125 : 0.1), domainLength = Math.abs(yDomainMax - yDomainMin),
padding = domainLength * 0.1, // TODO: should be an option
padding_top = padding, padding_bottom = padding, padding_top = padding, padding_bottom = padding,
center = axisId === 'y2' ? __axis_y2_center : __axis_y_center; center = axisId === 'y2' ? __axis_y2_center : __axis_y_center;
if (center) { if (center) {
@ -705,6 +706,13 @@
padding_top = isValue(__axis_y2_padding.top) ? __axis_y2_padding.top : padding; padding_top = isValue(__axis_y2_padding.top) ? __axis_y2_padding.top : padding;
padding_bottom = isValue(__axis_y2_padding.bottom) ? __axis_y2_padding.bottom : padding; padding_bottom = isValue(__axis_y2_padding.bottom) ? __axis_y2_padding.bottom : padding;
} }
// add padding for data label
if (hasDataLabel() && __axis_rotated) {
var widths = getDataLabelWidth(yDomainMin, yDomainMax), diff = diffDomain(y.range());
var ratio = [widths[0] / diff, widths[1] / diff];
padding_top += domainLength * (ratio[1] / (1 - ratio[0] - ratio[1]));
padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1]));
}
// Bar chart with only positive values should be 0-based // Bar chart with only positive values should be 0-based
if (hasBarType(yTargets) && !hasNegativeValueInTargets(yTargets)) { if (hasBarType(yTargets) && !hasNegativeValueInTargets(yTargets)) {
padding_bottom = yDomainMin; padding_bottom = yDomainMin;
@ -1019,6 +1027,16 @@
} }
return false; return false;
} }
function getDataLabelWidth(min, max) {
var widths = [], paddingCoef = 1.3;
d3.select('svg').selectAll('.dummy')
.data([min, max])
.enter().append('text')
.text(function (d) { return d; })
.each(function (d, i) { var box = this.getBBox(); widths[i] = box.width * paddingCoef; })
.remove();
return widths;
}
function xx(d) { function xx(d) {
return d ? x(d.x) : null; return d ? x(d.x) : null;

4
c3.min.js vendored

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save