Skip to content

Commit

Permalink
Convert all scales to use ES6 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Nov 16, 2019
1 parent b174806 commit 4b4b4b7
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 170 deletions.
44 changes: 22 additions & 22 deletions src/scales/scale.category.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
'use strict';

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

var defaultConfig = {
const defaultConfig = {
position: 'bottom'
};

module.exports = Scale.extend({

_parse: function(raw, index) {
class CategroyScale extends Scale {
_parse(raw, index) {
var labels = this._getLabels();
var first = labels.indexOf(raw);
var last = labels.lastIndexOf(raw);
return first === -1 || first !== last ? index : first;
},
}

determineDataLimits: function() {
determineDataLimits() {
var me = this;
var max = me._getLabels().length - 1;

me.min = Math.max(me._userMin || 0, 0);
me.max = Math.min(me._userMax || max, max);
},
}

buildTicks: function() {
buildTicks() {
var me = this;
var labels = me._getLabels();
var min = me.min;
Expand All @@ -34,19 +33,19 @@ module.exports = Scale.extend({
return labels.map(function(l) {
return {value: l};
});
},
}

getLabelForValue: function(value) {
getLabelForValue(value) {
var me = this;
var labels = me._getLabels();

if (value >= 0 && value < labels.length) {
return labels[value];
}
return value;
},
}

_configure: function() {
_configure() {
var me = this;
var offset = me.options.offset;
var ticks = me.ticks;
Expand All @@ -64,36 +63,37 @@ module.exports = Scale.extend({

me._startValue = me.min - (offset ? 0.5 : 0);
me._valueRange = Math.max(ticks.length - (offset ? 0 : 1), 1);
},
}

// Used to get data value locations. Value can either be an index or a numerical value
getPixelForValue: function(value) {
getPixelForValue(value) {
var me = this;

if (typeof value !== 'number') {
value = me._parse(value);
}

return me.getPixelForDecimal((value - me._startValue) / me._valueRange);
},
}

getPixelForTick: function(index) {
getPixelForTick(index) {
var ticks = this.ticks;
return index < 0 || index > ticks.length - 1
? null
: this.getPixelForValue(index + this.min);
},
}

getValueForPixel: function(pixel) {
getValueForPixel(pixel) {
var me = this;
var value = Math.round(me._startValue + me.getDecimalForPixel(pixel) * me._valueRange);
return Math.min(Math.max(value, 0), me.ticks.length - 1);
},
}

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

module.exports = CategroyScale;
// INTERNAL: static default options, registered in src/index.js
module.exports._defaults = defaultConfig;
35 changes: 18 additions & 17 deletions src/scales/scale.linear.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

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

var defaultConfig = {
const defaultConfig = {
position: 'left',
ticks: {
callback: Ticks.formatters.linear
}
};

module.exports = LinearScaleBase.extend({
determineDataLimits: function() {
class LinearScale extends LinearScaleBase {
determineDataLimits() {
var me = this;
var DEFAULT_MIN = 0;
var DEFAULT_MAX = 1;
Expand All @@ -30,10 +30,10 @@ module.exports = LinearScaleBase.extend({

// Common base implementation to handle min, max, beginAtZero
me.handleTickRangeOptions();
},
}

// Returns the maximum number of ticks based on the scale dimension
_computeTickLimit: function() {
_computeTickLimit() {
var me = this;
var tickFont;

Expand All @@ -42,35 +42,36 @@ module.exports = LinearScaleBase.extend({
}
tickFont = helpers.options._parseFont(me.options.ticks);
return Math.ceil(me.height / tickFont.lineHeight);
},
}

/**
* Called after the ticks are built
* @private
*/
_handleDirectionalChanges: function(ticks) {
_handleDirectionalChanges(ticks) {
// If we are in a vertical orientation the top value is the highest so reverse the array
return this.isHorizontal() ? ticks : ticks.reverse();
},
}

// Utils
getPixelForValue: function(value) {
getPixelForValue(value) {
var me = this;
return me.getPixelForDecimal((value - me._startValue) / me._valueRange);
},
}

getValueForPixel: function(pixel) {
getValueForPixel(pixel) {
return this._startValue + this.getDecimalForPixel(pixel) * this._valueRange;
},
}

getPixelForTick: function(index) {
getPixelForTick(index) {
var ticks = this._tickValues;
if (index < 0 || index > ticks.length - 1) {
return null;
}
return this.getPixelForValue(ticks[index]);
}
});
}

module.exports = LinearScale;
// INTERNAL: static default options, registered in src/index.js
module.exports._defaults = defaultConfig;
42 changes: 22 additions & 20 deletions src/scales/scale.linearbase.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var helpers = require('../helpers/index');
var Scale = require('../core/core.scale');
const helpers = require('../helpers/index');
const Scale = require('../core/core.scale');

var isNullOrUndef = helpers.isNullOrUndef;
const isNullOrUndef = helpers.isNullOrUndef;

/**
* Generate a set of linear ticks
Expand Down Expand Up @@ -83,8 +83,8 @@ function generateTicks(generationOptions, dataRange) {
return ticks;
}

module.exports = Scale.extend({
_parse: function(raw, index) { // eslint-disable-line no-unused-vars
class LinearScaleBase extends Scale {
_parse(raw, index) { // eslint-disable-line no-unused-vars
if (helpers.isNullOrUndef(raw)) {
return NaN;
}
Expand All @@ -93,9 +93,9 @@ module.exports = Scale.extend({
}

return +raw;
},
}

handleTickRangeOptions: function() {
handleTickRangeOptions() {
var me = this;
var opts = me.options;

Expand Down Expand Up @@ -159,9 +159,9 @@ module.exports = Scale.extend({
me.min--;
}
}
},
}

getTickLimit: function() {
getTickLimit() {
var me = this;
var tickOpts = me.options.ticks;
var stepSize = tickOpts.stepSize;
Expand All @@ -180,17 +180,17 @@ module.exports = Scale.extend({
}

return maxTicks;
},
}

_computeTickLimit: function() {
_computeTickLimit() {
return Number.POSITIVE_INFINITY;
},
}

_handleDirectionalChanges: function(ticks) {
_handleDirectionalChanges(ticks) {
return ticks;
},
}

buildTicks: function() {
buildTicks() {
var me = this;
var opts = me.options;
var tickOpts = opts.ticks;
Expand Down Expand Up @@ -228,15 +228,15 @@ module.exports = Scale.extend({
}

return ticks;
},
}

generateTickLabels: function(ticks) {
generateTickLabels(ticks) {
var me = this;
me._tickValues = ticks.map(t => t.value);
Scale.prototype.generateTickLabels.call(me, ticks);
},
}

_configure: function() {
_configure() {
var me = this;
var ticks = me.getTicks();
var start = me.min;
Expand All @@ -254,4 +254,6 @@ module.exports = Scale.extend({
me._endValue = end;
me._valueRange = end - start;
}
});
}

module.exports = LinearScaleBase;
Loading

0 comments on commit 4b4b4b7

Please sign in to comment.