var isHorizontal = box.isHorizontal();
if (isHorizontal) {
- minSize = box.update(chartWidth, horizontalBoxHeight);
+ minSize = box.update(box.options.fullWidth ? chartWidth : chartAreaWidth, horizontalBoxHeight);
} else {
minSize = box.update(verticalBoxWidth, chartAreaHeight);
}
+
minBoxSizes.push({
horizontal: isHorizontal,
minSize: minSize,
// Set the Left and Right margins for the horizontal boxes
helpers.each(topBoxes.concat(bottomBoxes), fitBox);
+ // Function to fit a box
function fitBox(box) {
-
var minBoxSize = helpers.findNextWhere(minBoxSizes, function(minBoxSize) {
return minBoxSize.box === box;
});
- if (box.isHorizontal() && minBoxSize) {
- var scaleMargin = {
- left: totalLeftBoxesWidth,
- right: totalRightBoxesWidth,
- top: 0,
- bottom: 0,
- };
-
- box.update(chartWidth, minBoxSize.minSize.height, scaleMargin);
- } else {
- box.update(minBoxSize.minSize.width, maxChartAreaHeight);
+ if (minBoxSize) {
+ if (box.isHorizontal()) {
+ var scaleMargin = {
+ left: totalLeftBoxesWidth,
+ right: totalRightBoxesWidth,
+ top: 0,
+ bottom: 0,
+ };
+
+ box.update(box.options.fullWidth ? chartWidth : maxChartAreaWidth, minBoxSize.minSize.height, scaleMargin);
+ } else {
+ box.update(minBoxSize.minSize.width, maxChartAreaHeight);
+ }
}
}
function placeBox(box) {
if (box.isHorizontal()) {
- box.left = xPadding;
- box.right = width - xPadding;
+ box.left = box.options.fullWidth ? xPadding : totalLeftBoxesWidth;
+ box.right = box.options.fullWidth ? width - xPadding : totalLeftBoxesWidth + maxChartAreaWidth;
box.top = top;
box.bottom = top + box.height;
// Width
if (this.isHorizontal()) {
- // subtract the margins to line up with the chartArea
- this.minSize.width = this.maxWidth - this.margins.left - this.margins.right;
+ // subtract the margins to line up with the chartArea if we are a full width scale
+ this.minSize.width = this.isFullWidth() ? this.maxWidth - this.margins.left - this.margins.right : this.maxWidth;
} else {
this.minSize.width = this.options.gridLines.show && this.options.display ? 10 : 0;
}
isHorizontal: function() {
return this.options.position == "top" || this.options.position == "bottom";
},
+ isFullWidth: function() {
+ return (this.options.fullWidth);
+ },
// Get the correct value. NaN bad inputs, If the value type is object get the x or y based on whether we are horizontal or not
getRightValue: function getRightValue(rawValue) {
if (includeOffset) {
pixel += tickWidth / 2;
}
- return this.left + this.margins.left + Math.round(pixel);
+
+ var finalVal = this.left + Math.round(pixel);
+ finalVal += this.isFullWidth() ? this.margins.left : 0;
+ return finalVal;
} else {
var innerHeight = this.height - (this.paddingTop + this.paddingBottom);
return this.top + (index * (innerHeight / (this.ticks.length - 1)));
var innerWidth = this.width - (this.paddingLeft + this.paddingRight);
var valueOffset = (innerWidth * decimal) + this.paddingLeft;
- return this.left + this.margins.left + Math.round(valueOffset);
+ var finalVal = this.left + Math.round(valueOffset);
+ finalVal += this.isFullWidth() ? this.margins.left : 0;
+ return finalVal;
} else {
return this.top + (decimal * this.height);
}
describe('Test the layout service', function() {
it('should fit a simple chart with 2 scales', function() {
var chartInstance = {
- scales: [],
+ boxes: [],
};
var xScaleID = 'xScale';
id: yScaleID
});
- chartInstance.scales.push(xScale);
- chartInstance.scales.push(yScale);
+ chartInstance.boxes.push(xScale);
+ chartInstance.boxes.push(yScale);
var canvasWidth = 250;
var canvasHeight = 150;
it('should fit scales that are in the top and right positions', function() {
var chartInstance = {
- scales: [],
+ boxes: [],
};
var xScaleID = 'xScale';
id: yScaleID
});
- chartInstance.scales.push(xScale);
- chartInstance.scales.push(yScale);
+ chartInstance.boxes.push(xScale);
+ chartInstance.boxes.push(yScale);
var canvasWidth = 250;
var canvasHeight = 150;
it('should fit multiple axes in the same position', function() {
var chartInstance = {
- scales: [],
+ boxes: [],
};
var xScaleID = 'xScale';
id: yScaleID2
});
- chartInstance.scales.push(xScale);
- chartInstance.scales.push(yScale1);
- chartInstance.scales.push(yScale2);
+ chartInstance.boxes.push(xScale);
+ chartInstance.boxes.push(yScale1);
+ chartInstance.boxes.push(yScale2);
var canvasWidth = 250;
var canvasHeight = 150;
// due to the lack of label rotation
it('should fit scales that overlap the chart area', function() {
var chartInstance = {
- scales: [],
+ boxes: [],
};
var scaleID = 'scaleID';
id: scaleID
});
- chartInstance.scales.push(scale);
+ chartInstance.boxes.push(scale);
var canvasWidth = 300;
var canvasHeight = 350;
expect(scale.width).toBe(290);
expect(scale.height).toBe(340)
});
+
+ it ('should fix a full width box correctly', function() {
+ var chartInstance = {
+ boxes: [],
+ };
+
+ var xScaleID1= 'xScale1';
+ var xScaleID2 = 'xScale2';
+ var yScaleID = 'yScale2';
+
+ var mockData = {
+ datasets: [{
+ xAxisID: xScaleID1,
+ data: [10, 5, 0, 25, 78, -10]
+ }, {
+ xAxisID: xScaleID2,
+ data: [-19, -20, 0, -99, -50, 0]
+ }],
+ labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
+ };
+ var mockContext = window.createMockContext();
+
+ var xScaleConfig = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('category'));
+ var XConstructor = Chart.scaleService.getScaleConstructor('category');
+ var xScale1 = new XConstructor({
+ ctx: mockContext,
+ options: xScaleConfig,
+ chart: {
+ data: mockData
+ },
+ id: xScaleID1
+ });
+ var xScale2 = new XConstructor({
+ ctx: mockContext,
+ options: Chart.helpers.extend(Chart.helpers.clone(xScaleConfig), {
+ position: 'top',
+ fullWidth: true
+ }),
+ chart: {
+ data: mockData,
+ },
+ id: xScaleID2
+ });
+
+ var yScaleConfig = Chart.helpers.clone(Chart.scaleService.getScaleDefaults('linear'));
+ var YConstructor = Chart.scaleService.getScaleConstructor('linear');
+ var yScale = new YConstructor({
+ ctx: mockContext,
+ options: yScaleConfig,
+ chart: {
+ data: mockData
+ },
+ id: yScaleID
+ });
+
+ chartInstance.boxes.push(xScale1);
+ chartInstance.boxes.push(xScale2);
+ chartInstance.boxes.push(yScale);
+
+ var canvasWidth = 250;
+ var canvasHeight = 150;
+ Chart.layoutService.update(chartInstance, canvasWidth, canvasHeight);
+
+ expect(chartInstance.chartArea).toEqual({
+ left: 45,
+ right: 245,
+ top: 45,
+ bottom: 105,
+ });
+
+ // Are xScales at the right spot
+ expect(xScale1.left).toBe(45);
+ expect(xScale1.right).toBe(245);
+ expect(xScale1.top).toBe(105);
+ expect(xScale1.bottom).toBe(145);
+
+ expect(xScale2.left).toBe(5);
+ expect(xScale2.right).toBe(245);
+ expect(xScale2.top).toBe(5);
+ expect(xScale2.bottom).toBe(45);
+
+ // Is yScale at the right spot
+ expect(yScale.left).toBe(5);
+ expect(yScale.right).toBe(45);
+ expect(yScale.top).toBe(45);
+ expect(yScale.bottom).toBe(105);
+ });
});
\ No newline at end of file