Skip to content

Commit

Permalink
Consistent _getLabels()
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed May 11, 2019
1 parent 4b5c2dc commit 8d21f50
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/axes/cartesian/time.md
Expand Up @@ -149,7 +149,7 @@ The `ticks.source` property controls the ticks generation.

* `'auto'`: generates "optimal" ticks based on scale size and time options
* `'data'`: generates ticks from data (including labels from data `{t|x|y}` objects)
* `'labels'`: generates ticks from user given `scale.labels` or `data.labels` values ONLY
* `'labels'`: generates ticks from user given `labels` ONLY

### Parser
If this property is defined as a string, it is interpreted as a custom format to be used by Moment.js to parse the date.
Expand Down
9 changes: 9 additions & 0 deletions src/core/core.scale.js
Expand Up @@ -214,6 +214,15 @@ var Scale = Element.extend({
return this._ticks;
},


/**
* @private
*/
_getLabels: function() {
var data = this.chart.data;
return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels;
},

// These methods are ordered by lifecyle. Utilities then follow.
// Any function defined here is inherited by all scale types.
// Any function can be extended by the scale type
Expand Down
16 changes: 3 additions & 13 deletions src/scales/scale.category.js
Expand Up @@ -7,19 +7,9 @@ var defaultConfig = {
};

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();
var labels = me._getLabels();
me.minIndex = 0;
me.maxIndex = labels.length - 1;
var findIndex;
Expand All @@ -42,7 +32,7 @@ module.exports = Scale.extend({

buildTicks: function() {
var me = this;
var labels = me.getLabels();
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);
},
Expand Down Expand Up @@ -72,7 +62,7 @@ module.exports = Scale.extend({
valueCategory = me.isHorizontal() ? value.x : value.y;
}
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
var labels = me.getLabels();
var labels = me._getLabels();
value = valueCategory || value;
var idx = labels.indexOf(value);
index = idx !== -1 ? idx : index;
Expand Down
2 changes: 1 addition & 1 deletion src/scales/scale.time.js
Expand Up @@ -523,7 +523,7 @@ module.exports = Scale.extend({
var datasets = [];
var labels = [];
var i, j, ilen, jlen, data, timestamp;
var dataLabels = options.labels || chart.data.labels || [];
var dataLabels = me._getLabels();

// Convert labels to timestamps
for (i = 0, ilen = dataLabels.length; i < ilen; ++i) {
Expand Down

0 comments on commit 8d21f50

Please sign in to comment.