Commit 716a2c5c authored by Masayuki Tanaka's avatar Masayuki Tanaka

Assume axis.y.padding as pixels - #799

parent dceeaf28
...@@ -1369,16 +1369,16 @@ ...@@ -1369,16 +1369,16 @@
padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1])); padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1]));
} else if (showVerticalDataLabel) { } else if (showVerticalDataLabel) {
lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, axisId, 'height'); lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, axisId, 'height');
padding_top += domainLength * (lengths[1] / $$.height); padding_top += this.convertPixelsToAxisPadding(lengths[1], domainLength);
padding_bottom += domainLength * (lengths[0] / $$.height); padding_bottom += this.convertPixelsToAxisPadding(lengths[0], domainLength);
} }
if (axisId === 'y' && notEmpty(config.axis_y_padding)) { if (axisId === 'y' && notEmpty(config.axis_y_padding)) {
padding_top = $$.getAxisPadding(config.axis_y_padding, 'top', padding, domainLength); padding_top = $$.getAxisPadding(config.axis_y_padding, 'top', padding_top, domainLength);
padding_bottom = $$.getAxisPadding(config.axis_y_padding, 'bottom', padding, domainLength); padding_bottom = $$.getAxisPadding(config.axis_y_padding, 'bottom', padding_bottom, domainLength);
} }
if (axisId === 'y2' && notEmpty(config.axis_y2_padding)) { if (axisId === 'y2' && notEmpty(config.axis_y2_padding)) {
padding_top = $$.getAxisPadding(config.axis_y2_padding, 'top', padding, domainLength); padding_top = $$.getAxisPadding(config.axis_y2_padding, 'top', padding_top, domainLength);
padding_bottom = $$.getAxisPadding(config.axis_y2_padding, 'bottom', padding, domainLength); padding_bottom = $$.getAxisPadding(config.axis_y2_padding, 'bottom', padding_bottom, domainLength);
} }
// Bar/Area chart should be 0-based if all positive|negative // Bar/Area chart should be 0-based if all positive|negative
if (isZeroBased) { if (isZeroBased) {
...@@ -3233,7 +3233,15 @@ ...@@ -3233,7 +3233,15 @@
} else { } else {
xPos = $$.hasType('bar') ? (points[2][0] + points[0][0]) / 2 : points[0][0]; xPos = $$.hasType('bar') ? (points[2][0] + points[0][0]) / 2 : points[0][0];
} }
return d.value !== null ? xPos : xPos > $$.width ? $$.width - box.width : xPos; // show labels regardless of the domain if value is null
if (d.value === null) {
if (xPos > $$.width) {
xPos = $$.width - box.width;
} else if (xPos < 0) {
xPos = 4;
}
}
return xPos;
}; };
c3_chart_internal_fn.getYForText = function (points, d, textElement) { c3_chart_internal_fn.getYForText = function (points, d, textElement) {
var $$ = this, var $$ = this,
...@@ -3243,7 +3251,15 @@ ...@@ -3243,7 +3251,15 @@
} else { } else {
yPos = points[2][1] + (d.value < 0 ? box.height : $$.isBarType(d) ? -3 : -6); yPos = points[2][1] + (d.value < 0 ? box.height : $$.isBarType(d) ? -3 : -6);
} }
return d.value !== null ? yPos : yPos < box.height ? box.height : yPos; // show labels regardless of the domain if value is null
if (d.value === null && !$$.config.axis_rotated) {
if (yPos < box.height) {
yPos = box.height;
} else if (yPos > this.height) {
yPos = this.height - 4;
}
}
return yPos;
}; };
c3_chart_internal_fn.setTargetType = function (targetIds, type) { c3_chart_internal_fn.setTargetType = function (targetIds, type) {
...@@ -4315,9 +4331,19 @@ ...@@ -4315,9 +4331,19 @@
.text($$.textForY2AxisLabel.bind($$)); .text($$.textForY2AxisLabel.bind($$));
}; };
c3_chart_internal_fn.getAxisPadding = function (padding, key, defaultValue, all) { c3_chart_internal_fn.getAxisPadding = function (padding, key, defaultValue, domainLength) {
var ratio = padding.unit === 'ratio' ? all : 1; if (!isValue(padding[key])) {
return isValue(padding[key]) ? padding[key] * ratio : defaultValue; return defaultValue;
}
if (padding.unit === 'ratio') {
return padding[key] * domainLength;
}
// assume padding is pixels if unit is not specified
return this.convertPixelsToAxisPadding(padding[key], domainLength);
};
c3_chart_internal_fn.convertPixelsToAxisPadding = function (pixels, domainLength) {
var length = this.config.axis_rotated ? this.width : this.height;
return domainLength * (pixels / length);
}; };
c3_chart_internal_fn.generateTickValues = function (values, tickCount, forTimeSeries) { c3_chart_internal_fn.generateTickValues = function (values, tickCount, forTimeSeries) {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -3,8 +3,6 @@ var describe = window.describe, ...@@ -3,8 +3,6 @@ var describe = window.describe,
it = window.it, it = window.it,
beforeEach = window.beforeEach; beforeEach = window.beforeEach;
var initDom = window.initDom;
describe('c3 chart axis', function () { describe('c3 chart axis', function () {
'use strict'; 'use strict';
...@@ -24,16 +22,8 @@ describe('c3 chart axis', function () { ...@@ -24,16 +22,8 @@ describe('c3 chart axis', function () {
}; };
beforeEach(function (done) { beforeEach(function (done) {
if (typeof chart === 'undefined') { chart = window.initChart(chart, args, done);
initDom();
}
chart = window.c3.generate(args);
d3 = chart.internal.d3; d3 = chart.internal.d3;
chart.internal.d3.select('.jasmine_html-reporter').style('display', 'none');
window.setTimeout(function () {
done();
}, 10);
}); });
describe('axis.y.min', function () { describe('axis.y.min', function () {
...@@ -93,4 +83,34 @@ describe('c3 chart axis', function () { ...@@ -93,4 +83,34 @@ describe('c3 chart axis', function () {
}); });
describe('axis.y.padding', function () {
it('should change axis.y.max to 1000', function () {
args = {
data: {
columns: [
['data1', 10, 20, 10, 40, 15, 25],
['data2', 50, 40, 30, 45, 25, 45]
]
},
axis: {
y: {
padding: {
top: 200,
bottom: 200
}
}
}
};
expect(true).toBeTruthy();
});
it('should be set properly when bigger than min of data', function () {
var domain = chart.internal.y.domain();
expect(domain[0]).toBeCloseTo(-9, -1);
expect(domain[1]).toBeCloseTo(69, -1);
});
});
}); });
...@@ -315,9 +315,19 @@ c3_chart_internal_fn.updateAxisLabels = function (withTransition) { ...@@ -315,9 +315,19 @@ c3_chart_internal_fn.updateAxisLabels = function (withTransition) {
.text($$.textForY2AxisLabel.bind($$)); .text($$.textForY2AxisLabel.bind($$));
}; };
c3_chart_internal_fn.getAxisPadding = function (padding, key, defaultValue, all) { c3_chart_internal_fn.getAxisPadding = function (padding, key, defaultValue, domainLength) {
var ratio = padding.unit === 'ratio' ? all : 1; if (!isValue(padding[key])) {
return isValue(padding[key]) ? padding[key] * ratio : defaultValue; return defaultValue;
}
if (padding.unit === 'ratio') {
return padding[key] * domainLength;
}
// assume padding is pixels if unit is not specified
return this.convertPixelsToAxisPadding(padding[key], domainLength);
};
c3_chart_internal_fn.convertPixelsToAxisPadding = function (pixels, domainLength) {
var length = this.config.axis_rotated ? this.width : this.height;
return domainLength * (pixels / length);
}; };
c3_chart_internal_fn.generateTickValues = function (values, tickCount, forTimeSeries) { c3_chart_internal_fn.generateTickValues = function (values, tickCount, forTimeSeries) {
......
...@@ -126,16 +126,16 @@ c3_chart_internal_fn.getYDomain = function (targets, axisId, xDomain) { ...@@ -126,16 +126,16 @@ c3_chart_internal_fn.getYDomain = function (targets, axisId, xDomain) {
padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1])); padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1]));
} else if (showVerticalDataLabel) { } else if (showVerticalDataLabel) {
lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, axisId, 'height'); lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, axisId, 'height');
padding_top += domainLength * (lengths[1] / $$.height); padding_top += this.convertPixelsToAxisPadding(lengths[1], domainLength);
padding_bottom += domainLength * (lengths[0] / $$.height); padding_bottom += this.convertPixelsToAxisPadding(lengths[0], domainLength);
} }
if (axisId === 'y' && notEmpty(config.axis_y_padding)) { if (axisId === 'y' && notEmpty(config.axis_y_padding)) {
padding_top = $$.getAxisPadding(config.axis_y_padding, 'top', padding, domainLength); padding_top = $$.getAxisPadding(config.axis_y_padding, 'top', padding_top, domainLength);
padding_bottom = $$.getAxisPadding(config.axis_y_padding, 'bottom', padding, domainLength); padding_bottom = $$.getAxisPadding(config.axis_y_padding, 'bottom', padding_bottom, domainLength);
} }
if (axisId === 'y2' && notEmpty(config.axis_y2_padding)) { if (axisId === 'y2' && notEmpty(config.axis_y2_padding)) {
padding_top = $$.getAxisPadding(config.axis_y2_padding, 'top', padding, domainLength); padding_top = $$.getAxisPadding(config.axis_y2_padding, 'top', padding_top, domainLength);
padding_bottom = $$.getAxisPadding(config.axis_y2_padding, 'bottom', padding, domainLength); padding_bottom = $$.getAxisPadding(config.axis_y2_padding, 'bottom', padding_bottom, domainLength);
} }
// Bar/Area chart should be 0-based if all positive|negative // Bar/Area chart should be 0-based if all positive|negative
if (isZeroBased) { if (isZeroBased) {
......
...@@ -80,7 +80,15 @@ c3_chart_internal_fn.getXForText = function (points, d, textElement) { ...@@ -80,7 +80,15 @@ c3_chart_internal_fn.getXForText = function (points, d, textElement) {
} else { } else {
xPos = $$.hasType('bar') ? (points[2][0] + points[0][0]) / 2 : points[0][0]; xPos = $$.hasType('bar') ? (points[2][0] + points[0][0]) / 2 : points[0][0];
} }
return d.value !== null ? xPos : xPos > $$.width ? $$.width - box.width : xPos; // show labels regardless of the domain if value is null
if (d.value === null) {
if (xPos > $$.width) {
xPos = $$.width - box.width;
} else if (xPos < 0) {
xPos = 4;
}
}
return xPos;
}; };
c3_chart_internal_fn.getYForText = function (points, d, textElement) { c3_chart_internal_fn.getYForText = function (points, d, textElement) {
var $$ = this, var $$ = this,
...@@ -90,5 +98,13 @@ c3_chart_internal_fn.getYForText = function (points, d, textElement) { ...@@ -90,5 +98,13 @@ c3_chart_internal_fn.getYForText = function (points, d, textElement) {
} else { } else {
yPos = points[2][1] + (d.value < 0 ? box.height : $$.isBarType(d) ? -3 : -6); yPos = points[2][1] + (d.value < 0 ? box.height : $$.isBarType(d) ? -3 : -6);
} }
return d.value !== null ? yPos : yPos < box.height ? box.height : yPos; // show labels regardless of the domain if value is null
if (d.value === null && !$$.config.axis_rotated) {
if (yPos < box.height) {
yPos = box.height;
} else if (yPos > this.height) {
yPos = this.height - 4;
}
}
return yPos;
}; };
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