]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
when axes are in the wrong place, update the config position
authoretimberg <evert.timberg@gmail.com>
Sat, 11 Feb 2017 01:37:31 +0000 (20:37 -0500)
committerEvert Timberg <evert.timberg+github@gmail.com>
Sun, 12 Feb 2017 16:23:17 +0000 (11:23 -0500)
src/core/core.controller.js
test/core.controller.tests.js
test/scale.logarithmic.tests.js

index 0d4c128bad75d16cd00e998f41f78a43599d2002..02433ce5292d9c69480d5c63f0d484d6f171a917 100644 (file)
@@ -56,6 +56,10 @@ module.exports = function(Chart) {
                chart.tooltip._options = newOptions.tooltips;
        }
 
+       function positionIsHorizontal(position) {
+               return position === 'top' || position === 'bottom';
+       }
+
        helpers.extend(Chart.prototype, /** @lends Chart */ {
                /**
                 * @private
@@ -220,16 +224,21 @@ module.exports = function(Chart) {
                        if (options.scales) {
                                items = items.concat(
                                        (options.scales.xAxes || []).map(function(xAxisOptions) {
-                                               return {options: xAxisOptions, dtype: 'category'};
+                                               return {options: xAxisOptions, dtype: 'category', dposition: 'bottom'};
                                        }),
                                        (options.scales.yAxes || []).map(function(yAxisOptions) {
-                                               return {options: yAxisOptions, dtype: 'linear'};
+                                               return {options: yAxisOptions, dtype: 'linear', dposition: 'left'};
                                        })
                                );
                        }
 
                        if (options.scale) {
-                               items.push({options: options.scale, dtype: 'radialLinear', isDefault: true});
+                               items.push({
+                                       options: options.scale,
+                                       dtype: 'radialLinear',
+                                       isDefault: true,
+                                       dposition: 'chartArea'
+                               });
                        }
 
                        helpers.each(items, function(item) {
@@ -240,6 +249,10 @@ module.exports = function(Chart) {
                                        return;
                                }
 
+                               if (positionIsHorizontal(scaleOptions.position) !== positionIsHorizontal(item.dposition)) {
+                                       scaleOptions.position = item.dposition;
+                               }
+
                                var scale = new scaleClass({
                                        id: scaleOptions.id,
                                        options: scaleOptions,
index f29542cbf591d2f74188cd2117e4eab5ec2c973f..7b5e4933216e233f616cd2c1617a9743b79eb6cc 100644 (file)
@@ -105,6 +105,26 @@ describe('Chart', function() {
                        expect(options.hover.mode).toBe('dataset');
                        expect(options.title.position).toBe('bottom');
                });
+
+               it('should override axis positions that are incorrect', function() {
+                       var chart = acquireChart({
+                               type: 'line',
+                               options: {
+                                       scales: {
+                                               xAxes: [{
+                                                       position: 'left',
+                                               }],
+                                               yAxes: [{
+                                                       position: 'bottom'
+                                               }]
+                                       }
+                               }
+                       });
+
+                       var scaleOptions = chart.options.scales;
+                       expect(scaleOptions.xAxes[0].position).toBe('bottom');
+                       expect(scaleOptions.yAxes[0].position).toBe('left');
+               });
        });
 
        describe('config.options.responsive: false', function() {
index 016737c71ee21880c9b1285a14ebb8a04c31e76b..4562aa384f5027b436642d427d03bb42132e9934 100644 (file)
@@ -690,22 +690,21 @@ describe('Logarithmic Scale tests', function() {
 
        it('should get the correct pixel value for a point', function() {
                var chart = window.acquireChart({
-                       type: 'bar',
+                       type: 'line',
                        data: {
                                datasets: [{
                                        xAxisID: 'xScale', // for the horizontal scale
                                        yAxisID: 'yScale',
-                                       data: [10, 5, 1, 25, 78]
+                                       data: [{x: 10, y: 10}, {x: 5, y: 5}, {x: 1, y: 1}, {x: 25, y: 25}, {x: 78, y: 78}]
                                }],
-                               labels: []
                        },
                        options: {
                                scales: {
-                                       yAxes: [{
+                                       xAxes: [{
                                                id: 'xScale',
-                                               type: 'logarithmic',
-                                               position: 'bottom'
-                                       }, {
+                                               type: 'logarithmic'
+                                       }],
+                                       yAxes: [{
                                                id: 'yScale',
                                                type: 'logarithmic'
                                        }]
@@ -714,24 +713,24 @@ describe('Logarithmic Scale tests', function() {
                });
 
                var xScale = chart.scales.xScale;
-               expect(xScale.getPixelForValue(80, 0, 0)).toBeCloseToPixel(482);  // right - paddingRight
+               expect(xScale.getPixelForValue(80, 0, 0)).toBeCloseToPixel(495);  // right - paddingRight
                expect(xScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(37);   // left + paddingLeft
-               expect(xScale.getPixelForValue(10, 0, 0)).toBeCloseToPixel(270);  // halfway
+               expect(xScale.getPixelForValue(10, 0, 0)).toBeCloseToPixel(278);  // halfway
                expect(xScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(37);   // 0 is invalid, put it on the left.
 
-               expect(xScale.getValueForPixel(481.5)).toBeCloseToPixel(80);
+               expect(xScale.getValueForPixel(495)).toBeCloseToPixel(80);
                expect(xScale.getValueForPixel(48)).toBeCloseTo(1, 1e-4);
-               expect(xScale.getValueForPixel(270)).toBeCloseTo(10, 1e-4);
+               expect(xScale.getValueForPixel(278)).toBeCloseTo(10, 1e-4);
 
                var yScale = chart.scales.yScale;
                expect(yScale.getPixelForValue(80, 0, 0)).toBeCloseToPixel(32);   // top + paddingTop
-               expect(yScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(456);  // bottom - paddingBottom
-               expect(yScale.getPixelForValue(10, 0, 0)).toBeCloseToPixel(234);  // halfway
+               expect(yScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(484);  // bottom - paddingBottom
+               expect(yScale.getPixelForValue(10, 0, 0)).toBeCloseToPixel(246);  // halfway
                expect(yScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(32);   // 0 is invalid. force it on top
 
                expect(yScale.getValueForPixel(32)).toBeCloseTo(80, 1e-4);
-               expect(yScale.getValueForPixel(456)).toBeCloseTo(1, 1e-4);
-               expect(yScale.getValueForPixel(234)).toBeCloseTo(10, 1e-4);
+               expect(yScale.getValueForPixel(484)).toBeCloseTo(1, 1e-4);
+               expect(yScale.getValueForPixel(246)).toBeCloseTo(10, 1e-4);
        });
 
        it('should get the correct pixel value for a point when 0 values are present', function() {