Skip to content

Commit

Permalink
Cleanup scales export for better import strategy (#5953)
Browse files Browse the repository at this point in the history
Scales now export their class and associated defaults (`_defaults`), the registration being done globally in `src/chart.js`.
  • Loading branch information
simonbrunel committed Jan 5, 2019
1 parent 26b7375 commit 87e44fa
Show file tree
Hide file tree
Showing 10 changed files with 1,524 additions and 1,475 deletions.
20 changes: 14 additions & 6 deletions src/chart.js
Expand Up @@ -26,12 +26,11 @@ Chart.Tooltip = require('./core/core.tooltip');

require('./core/core.controller')(Chart);

require('./scales/scale.linearbase')(Chart);
require('./scales/scale.category')(Chart);
require('./scales/scale.linear')(Chart);
require('./scales/scale.logarithmic')(Chart);
require('./scales/scale.radialLinear')(Chart);
require('./scales/scale.time')(Chart);
// Register built-in scales
var scales = require('./scales');
Chart.helpers.each(scales, function(scale, type) {
Chart.scaleService.registerScaleType(type, scale, scale._defaults);
});

// Loading built-in plugins
var plugins = require('./plugins');
Expand Down Expand Up @@ -105,6 +104,15 @@ Chart.canvasHelpers = Chart.helpers.canvas;
*/
Chart.layoutService = Chart.layouts;

/**
* Provided for backward compatibility, not available anymore.
* @namespace Chart.LinearScaleBase
* @deprecated since version 2.8
* @todo remove at version 3
* @private
*/
Chart.LinearScaleBase = require('./scales/scale.linearbase');

/**
* Provided for backward compatibility, instead we should create a new Chart
* by setting the type in the config (`new Chart(id, {type: '{chart-type}'}`).
Expand Down
29 changes: 19 additions & 10 deletions src/controllers/index.js
@@ -1,18 +1,27 @@
'use strict';

var bar = require('./controller.bar');
var bubble = require('./controller.bubble');
var doughnut = require('./controller.doughnut');
var horizontalBar = require('./controller.horizontalBar');
var line = require('./controller.line');
var polarArea = require('./controller.polarArea');
var pie = require('./controller.pie');
var radar = require('./controller.radar');
var scatter = require('./controller.scatter');

// NOTE export a map in which the key represents the controller type, not
// the class, and so must be CamelCase in order to be correctly retrieved
// by the controller in core.controller.js (`controllers[meta.type]`).

/* eslint-disable global-require */
module.exports = {
bar: require('./controller.bar'),
bubble: require('./controller.bubble'),
doughnut: require('./controller.doughnut'),
horizontalBar: require('./controller.horizontalBar'),
line: require('./controller.line'),
polarArea: require('./controller.polarArea'),
pie: require('./controller.pie'),
radar: require('./controller.radar'),
scatter: require('./controller.scatter')
bar: bar,
bubble: bubble,
doughnut: doughnut,
horizontalBar: horizontalBar,
line: line,
polarArea: polarArea,
pie: pie,
radar: radar,
scatter: scatter
};
15 changes: 15 additions & 0 deletions src/scales/index.js
@@ -0,0 +1,15 @@
'use strict';

var category = require('./scale.category');
var linear = require('./scale.linear');
var logarithmic = require('./scale.logarithmic');
var radialLinear = require('./scale.radialLinear');
var time = require('./scale.time');

module.exports = {
category: category,
linear: linear,
logarithmic: logarithmic,
radialLinear: radialLinear,
time: time
};
229 changes: 114 additions & 115 deletions src/scales/scale.category.js
@@ -1,135 +1,134 @@
'use strict';

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

module.exports = function() {

// Default config for a category scale
var defaultConfig = {
position: 'bottom'
};

var DatasetScale = 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
* @private
*/
getLabels: function() {
var data = this.chart.data;
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
},

determineDataLimits: function() {
var me = this;
var labels = me.getLabels();
me.minIndex = 0;
me.maxIndex = labels.length - 1;
var findIndex;

if (me.options.ticks.min !== undefined) {
// user specified min value
findIndex = labels.indexOf(me.options.ticks.min);
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex;
}

if (me.options.ticks.max !== undefined) {
// user specified max value
findIndex = labels.indexOf(me.options.ticks.max);
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
}
var defaultConfig = {
position: 'bottom'
};

me.min = labels[me.minIndex];
me.max = labels[me.maxIndex];
},
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
* @private
*/
getLabels: function() {
var data = this.chart.data;
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
},

determineDataLimits: function() {
var me = this;
var labels = me.getLabels();
me.minIndex = 0;
me.maxIndex = labels.length - 1;
var findIndex;

if (me.options.ticks.min !== undefined) {
// user specified min value
findIndex = labels.indexOf(me.options.ticks.min);
me.minIndex = findIndex !== -1 ? findIndex : me.minIndex;
}

buildTicks: function() {
var me = this;
var labels = me.getLabels();
// If we are viewing some subset of labels, slice the original array
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
},
if (me.options.ticks.max !== undefined) {
// user specified max value
findIndex = labels.indexOf(me.options.ticks.max);
me.maxIndex = findIndex !== -1 ? findIndex : me.maxIndex;
}

getLabelForIndex: function(index, datasetIndex) {
var me = this;
var data = me.chart.data;
var isHorizontal = me.isHorizontal();
me.min = labels[me.minIndex];
me.max = labels[me.maxIndex];
},

if (data.yLabels && !isHorizontal) {
return me.getRightValue(data.datasets[datasetIndex].data[index]);
}
return me.ticks[index - me.minIndex];
},

// Used to get data value locations. Value can either be an index or a numerical value
getPixelForValue: function(value, index) {
var me = this;
var offset = me.options.offset;
// 1 is added because we need the length but we have the indexes
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1);

// If value is a data object, then index is the index in the data array,
// not the index of the scale. We need to change that.
var valueCategory;
if (value !== undefined && value !== null) {
valueCategory = me.isHorizontal() ? value.x : value.y;
}
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
var labels = me.getLabels();
value = valueCategory || value;
var idx = labels.indexOf(value);
index = idx !== -1 ? idx : index;
}
buildTicks: function() {
var me = this;
var labels = me.getLabels();
// If we are viewing some subset of labels, slice the original array
me.ticks = (me.minIndex === 0 && me.maxIndex === labels.length - 1) ? labels : labels.slice(me.minIndex, me.maxIndex + 1);
},

if (me.isHorizontal()) {
var valueWidth = me.width / offsetAmt;
var widthOffset = (valueWidth * (index - me.minIndex));
getLabelForIndex: function(index, datasetIndex) {
var me = this;
var data = me.chart.data;
var isHorizontal = me.isHorizontal();

if (offset) {
widthOffset += (valueWidth / 2);
}
if (data.yLabels && !isHorizontal) {
return me.getRightValue(data.datasets[datasetIndex].data[index]);
}
return me.ticks[index - me.minIndex];
},

// Used to get data value locations. Value can either be an index or a numerical value
getPixelForValue: function(value, index) {
var me = this;
var offset = me.options.offset;
// 1 is added because we need the length but we have the indexes
var offsetAmt = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1);

// If value is a data object, then index is the index in the data array,
// not the index of the scale. We need to change that.
var valueCategory;
if (value !== undefined && value !== null) {
valueCategory = me.isHorizontal() ? value.x : value.y;
}
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
var labels = me.getLabels();
value = valueCategory || value;
var idx = labels.indexOf(value);
index = idx !== -1 ? idx : index;
}

return me.left + widthOffset;
}
var valueHeight = me.height / offsetAmt;
var heightOffset = (valueHeight * (index - me.minIndex));
if (me.isHorizontal()) {
var valueWidth = me.width / offsetAmt;
var widthOffset = (valueWidth * (index - me.minIndex));

if (offset) {
heightOffset += (valueHeight / 2);
widthOffset += (valueWidth / 2);
}

return me.top + heightOffset;
},
getPixelForTick: function(index) {
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null);
},
getValueForPixel: function(pixel) {
var me = this;
var offset = me.options.offset;
var value;
var offsetAmt = Math.max((me._ticks.length - (offset ? 0 : 1)), 1);
var horz = me.isHorizontal();
var valueDimension = (horz ? me.width : me.height) / offsetAmt;

pixel -= horz ? me.left : me.top;
return me.left + widthOffset;
}
var valueHeight = me.height / offsetAmt;
var heightOffset = (valueHeight * (index - me.minIndex));

if (offset) {
pixel -= (valueDimension / 2);
}
if (offset) {
heightOffset += (valueHeight / 2);
}

if (pixel <= 0) {
value = 0;
} else {
value = Math.round(pixel / valueDimension);
}
return me.top + heightOffset;
},

getPixelForTick: function(index) {
return this.getPixelForValue(this.ticks[index], index + this.minIndex, null);
},

return value + me.minIndex;
},
getBasePixel: function() {
return this.bottom;
getValueForPixel: function(pixel) {
var me = this;
var offset = me.options.offset;
var value;
var offsetAmt = Math.max((me._ticks.length - (offset ? 0 : 1)), 1);
var horz = me.isHorizontal();
var valueDimension = (horz ? me.width : me.height) / offsetAmt;

pixel -= horz ? me.left : me.top;

if (offset) {
pixel -= (valueDimension / 2);
}
});

scaleService.registerScaleType('category', DatasetScale, defaultConfig);
};
if (pixel <= 0) {
value = 0;
} else {
value = Math.round(pixel / valueDimension);
}

return value + me.minIndex;
},

getBasePixel: function() {
return this.bottom;
}
});

// INTERNAL: static default options, registered in src/chart.js
module.exports._defaults = defaultConfig;

0 comments on commit 87e44fa

Please sign in to comment.