Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup scales export for better import strategy #5953

Merged
merged 7 commits into from Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/chart.js
Expand Up @@ -17,6 +17,7 @@ Chart.Element = require('./core/core.element');
Chart.elements = require('./elements/index');
Chart.Interaction = require('./core/core.interaction');
Chart.layouts = require('./core/core.layouts');
Chart.LinearScaleBase = require('./scales/scale.linearbase');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment to remove this at v3 since it should be internal only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Chart.platform = require('./platforms/platform');
Chart.plugins = require('./core/core.plugins');
Chart.Scale = require('./core/core.scale');
Expand All @@ -26,12 +27,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
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;