Skip to content

Commit

Permalink
Better scale export (class with static _defaults)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrunel committed Jan 4, 2019
1 parent 1a7a36d commit 1d00765
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/chart.js
Expand Up @@ -29,8 +29,8 @@ require('./core/core.controller')(Chart);

// Register built-in scales
var scales = require('./scales');
Chart.helpers.each(scales, function(register) {
register();
Chart.helpers.each(scales, function(scale, type) {
Chart.scaleService.registerScaleType(type, scale, scale._defaults);
});

// Loading built-in plugins
Expand Down
8 changes: 3 additions & 5 deletions src/scales/scale.category.js
@@ -1,13 +1,12 @@
'use strict';

var Scale = require('../core/core.scale');
var scales = require('../core/core.scaleService');

var defaultConfig = {
position: 'bottom'
};

var CategoryScale = Scale.extend({
module.exports = Scale.extend({
/**
* Internal function to get the correct labels. If data.xLabels or data.yLabels are defined, use those
* else fall back to data.labels
Expand Down Expand Up @@ -131,6 +130,5 @@ var CategoryScale = Scale.extend({
}
});

module.exports = function() {
scales.registerScaleType('category', CategoryScale, defaultConfig);
};
// INTERNAL: static default options, registered in src/chart.js
module.exports._defaults = defaultConfig;
8 changes: 3 additions & 5 deletions src/scales/scale.linear.js
Expand Up @@ -2,7 +2,6 @@

var helpers = require('../helpers/index');
var LinearScaleBase = require('./scale.linearbase');
var scales = require('../core/core.scaleService');
var Ticks = require('../core/core.ticks');

var defaultConfig = {
Expand All @@ -12,7 +11,7 @@ var defaultConfig = {
}
};

var LinearScale = LinearScaleBase.extend({
module.exports = LinearScaleBase.extend({
determineDataLimits: function() {
var me = this;
var opts = me.options;
Expand Down Expand Up @@ -187,6 +186,5 @@ var LinearScale = LinearScaleBase.extend({
}
});

module.exports = function() {
scales.registerScaleType('linear', LinearScale, defaultConfig);
};
// INTERNAL: static default options, registered in src/chart.js
module.exports._defaults = defaultConfig;
9 changes: 4 additions & 5 deletions src/scales/scale.logarithmic.js
Expand Up @@ -3,7 +3,6 @@
var defaults = require('../core/core.defaults');
var helpers = require('../helpers/index');
var Scale = require('../core/core.scale');
var scales = require('../core/core.scaleService');
var Ticks = require('../core/core.ticks');

/**
Expand Down Expand Up @@ -53,6 +52,7 @@ function generateTicks(generationOptions, dataRange) {
return ticks;
}

// INTERNAL: static default options, registered in src/chart.js
var defaultConfig = {
position: 'left',

Expand All @@ -62,7 +62,7 @@ var defaultConfig = {
}
};

var LogarithmicScale = Scale.extend({
module.exports = Scale.extend({
determineDataLimits: function() {
var me = this;
var opts = me.options;
Expand Down Expand Up @@ -347,6 +347,5 @@ var LogarithmicScale = Scale.extend({
}
});

module.exports = function() {
scales.registerScaleType('logarithmic', LogarithmicScale, defaultConfig);
};
// INTERNAL: static default options, registered in src/chart.js
module.exports._defaults = defaultConfig;
8 changes: 3 additions & 5 deletions src/scales/scale.radialLinear.js
Expand Up @@ -3,7 +3,6 @@
var defaults = require('../core/core.defaults');
var helpers = require('../helpers/index');
var LinearScaleBase = require('./scale.linearbase');
var scales = require('../core/core.scaleService');
var Ticks = require('../core/core.ticks');

var defaultConfig = {
Expand Down Expand Up @@ -316,7 +315,7 @@ function numberOrZero(param) {
return helpers.isNumber(param) ? param : 0;
}

var LinearRadialScale = LinearScaleBase.extend({
module.exports = LinearScaleBase.extend({
setDimensions: function() {
var me = this;

Expand Down Expand Up @@ -529,6 +528,5 @@ var LinearRadialScale = LinearScaleBase.extend({
}
});

module.exports = function() {
scales.registerScaleType('radialLinear', LinearRadialScale, defaultConfig);
};
// INTERNAL: static default options, registered in src/chart.js
module.exports._defaults = defaultConfig;
7 changes: 3 additions & 4 deletions src/scales/scale.time.js
Expand Up @@ -488,7 +488,7 @@ var defaultConfig = {
}
};

var TimeScale = Scale.extend({
module.exports = Scale.extend({
initialize: function() {
if (!moment) {
throw new Error('Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com');
Expand Down Expand Up @@ -788,6 +788,5 @@ var TimeScale = Scale.extend({
}
});

module.exports = function() {
scales.registerScaleType('time', TimeScale, defaultConfig);
};
// INTERNAL: static default options, registered in src/chart.js
module.exports._defaults = defaultConfig;

0 comments on commit 1d00765

Please sign in to comment.