From 4b4b4b79e1e24d2f8e253abbe365a34021a29b79 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sat, 16 Nov 2019 13:49:35 -0500 Subject: [PATCH] Convert all scales to use ES6 classes --- src/scales/scale.category.js | 44 ++++++++-------- src/scales/scale.linear.js | 35 ++++++------- src/scales/scale.linearbase.js | 42 +++++++-------- src/scales/scale.logarithmic.js | 59 +++++++++++----------- src/scales/scale.radialLinear.js | 79 +++++++++++++++-------------- src/scales/scale.time.js | 87 ++++++++++++++++---------------- 6 files changed, 176 insertions(+), 170 deletions(-) diff --git a/src/scales/scale.category.js b/src/scales/scale.category.js index 1bf406fece5..c11008b9797 100644 --- a/src/scales/scale.category.js +++ b/src/scales/scale.category.js @@ -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; @@ -34,9 +33,9 @@ module.exports = Scale.extend({ return labels.map(function(l) { return {value: l}; }); - }, + } - getLabelForValue: function(value) { + getLabelForValue(value) { var me = this; var labels = me._getLabels(); @@ -44,9 +43,9 @@ module.exports = Scale.extend({ return labels[value]; } return value; - }, + } - _configure: function() { + _configure() { var me = this; var offset = me.options.offset; var ticks = me.ticks; @@ -64,10 +63,10 @@ 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') { @@ -75,25 +74,26 @@ module.exports = Scale.extend({ } 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; diff --git a/src/scales/scale.linear.js b/src/scales/scale.linear.js index 36a0fa62ce2..f3015b2609b 100644 --- a/src/scales/scale.linear.js +++ b/src/scales/scale.linear.js @@ -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; @@ -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; @@ -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; diff --git a/src/scales/scale.linearbase.js b/src/scales/scale.linearbase.js index a41e8c5843f..2b529cc5fc3 100644 --- a/src/scales/scale.linearbase.js +++ b/src/scales/scale.linearbase.js @@ -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 @@ -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; } @@ -93,9 +93,9 @@ module.exports = Scale.extend({ } return +raw; - }, + } - handleTickRangeOptions: function() { + handleTickRangeOptions() { var me = this; var opts = me.options; @@ -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; @@ -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; @@ -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; @@ -254,4 +254,6 @@ module.exports = Scale.extend({ me._endValue = end; me._valueRange = end - start; } -}); +} + +module.exports = LinearScaleBase; diff --git a/src/scales/scale.logarithmic.js b/src/scales/scale.logarithmic.js index 561af5449d5..21d5ac4ae19 100644 --- a/src/scales/scale.logarithmic.js +++ b/src/scales/scale.logarithmic.js @@ -1,13 +1,13 @@ 'use strict'; -var defaults = require('../core/core.defaults'); -var helpers = require('../helpers/index'); -var Scale = require('../core/core.scale'); -var LinearScaleBase = require('./scale.linearbase'); -var Ticks = require('../core/core.ticks'); +const defaults = require('../core/core.defaults'); +const helpers = require('../helpers/index'); +const Scale = require('../core/core.scale'); +const LinearScaleBase = require('./scale.linearbase'); +const Ticks = require('../core/core.ticks'); -var valueOrDefault = helpers.valueOrDefault; -var log10 = helpers.math.log10; +const valueOrDefault = helpers.valueOrDefault; +const log10 = helpers.math.log10; /** * Generate a set of logarithmic ticks @@ -55,7 +55,7 @@ function generateTicks(generationOptions, dataRange) { return ticks; } -var defaultConfig = { +const defaultConfig = { position: 'left', // label settings @@ -64,13 +64,13 @@ var defaultConfig = { } }; -module.exports = Scale.extend({ - _parse: function(raw, index) { // eslint-disable-line no-unused-vars +class LogarithmicScale extends Scale { + _parse(raw, index) { // eslint-disable-line no-unused-vars const value = LinearScaleBase.prototype._parse.apply(this, arguments); return helpers.isFinite(value) && value >= 0 ? value : undefined; - }, + } - determineDataLimits: function() { + determineDataLimits() { var me = this; var minmax = me._getMinMax(true); var min = minmax.min; @@ -82,9 +82,9 @@ module.exports = Scale.extend({ me.minNotZero = helpers.isFinite(minPositive) ? minPositive : null; me.handleTickRangeOptions(); - }, + } - handleTickRangeOptions: function() { + handleTickRangeOptions() { var me = this; var DEFAULT_MIN = 1; var DEFAULT_MAX = 10; @@ -119,9 +119,9 @@ module.exports = Scale.extend({ } me.min = min; me.max = max; - }, + } - buildTicks: function() { + buildTicks() { var me = this; var opts = me.options; var reverse = !me.isHorizontal(); @@ -148,21 +148,21 @@ module.exports = Scale.extend({ ticks.reverse(); } return ticks; - }, + } - generateTickLabels: function(ticks) { + generateTickLabels(ticks) { this._tickValues = ticks.map(t => t.value); return Scale.prototype.generateTickLabels.call(this, ticks); - }, + } - getPixelForTick: function(index) { + getPixelForTick(index) { var ticks = this._tickValues; if (index < 0 || index > ticks.length - 1) { return null; } return this.getPixelForValue(ticks[index]); - }, + } /** * Returns the value of the first tick. @@ -170,14 +170,14 @@ module.exports = Scale.extend({ * @return {number} The first tick value. * @private */ - _getFirstTickValue: function(value) { + _getFirstTickValue(value) { var exp = Math.floor(log10(value)); var significand = Math.floor(value / Math.pow(10, exp)); return significand * Math.pow(10, exp); - }, + } - _configure: function() { + _configure() { var me = this; var start = me.min; var offset = 0; @@ -192,9 +192,9 @@ module.exports = Scale.extend({ me._startValue = log10(start); me._valueOffset = offset; me._valueRange = (log10(me.max) - log10(start)) / (1 - offset); - }, + } - getPixelForValue: function(value) { + getPixelForValue(value) { var me = this; var decimal = 0; @@ -202,16 +202,17 @@ module.exports = Scale.extend({ decimal = (log10(value) - me._startValue) / me._valueRange + me._valueOffset; } return me.getPixelForDecimal(decimal); - }, + } - getValueForPixel: function(pixel) { + getValueForPixel(pixel) { var me = this; var decimal = me.getDecimalForPixel(pixel); return decimal === 0 && me.min === 0 ? 0 : Math.pow(10, me._startValue + (decimal - me._valueOffset) * me._valueRange); } -}); +} +module.exports = LogarithmicScale; // INTERNAL: static default options, registered in src/index.js module.exports._defaults = defaultConfig; diff --git a/src/scales/scale.radialLinear.js b/src/scales/scale.radialLinear.js index 50d95ff8129..746c05b7559 100644 --- a/src/scales/scale.radialLinear.js +++ b/src/scales/scale.radialLinear.js @@ -1,15 +1,15 @@ 'use strict'; -var defaults = require('../core/core.defaults'); -var helpers = require('../helpers/index'); -var LinearScaleBase = require('./scale.linearbase'); -var Ticks = require('../core/core.ticks'); +const defaults = require('../core/core.defaults'); +const helpers = require('../helpers/index'); +const LinearScaleBase = require('./scale.linearbase'); +const Ticks = require('../core/core.ticks'); -var valueOrDefault = helpers.valueOrDefault; -var valueAtIndexOrDefault = helpers.valueAtIndexOrDefault; -var resolve = helpers.options.resolve; +const valueOrDefault = helpers.valueOrDefault; +const valueAtIndexOrDefault = helpers.valueAtIndexOrDefault; +const resolve = helpers.options.resolve; -var defaultConfig = { +const defaultConfig = { display: true, // Boolean - Whether to animate scaling the chart from the centre @@ -290,8 +290,8 @@ function numberOrZero(param) { return helpers.isNumber(param) ? param : 0; } -module.exports = LinearScaleBase.extend({ - setDimensions: function() { +class RadialLinearScale extends LinearScaleBase { + setDimensions() { var me = this; // Set the unconstrained dimension before label rotation @@ -301,9 +301,9 @@ module.exports = LinearScaleBase.extend({ me.xCenter = Math.floor(me.width / 2); me.yCenter = Math.floor((me.height - me.paddingTop) / 2); me.drawingArea = Math.min(me.height - me.paddingTop, me.width) / 2; - }, + } - determineDataLimits: function() { + determineDataLimits() { var me = this; var minmax = me._getMinMax(false); var min = minmax.min; @@ -314,14 +314,14 @@ 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() { return Math.ceil(this.drawingArea / getTickBackdropHeight(this.options)); - }, + } - generateTickLabels: function(ticks) { + generateTickLabels(ticks) { var me = this; LinearScaleBase.prototype.generateTickLabels.call(me, ticks); @@ -331,9 +331,9 @@ module.exports = LinearScaleBase.extend({ var label = helpers.callback(me.options.pointLabels.callback, arguments, me); return label || label === 0 ? label : ''; }); - }, + } - fit: function() { + fit() { var me = this; var opts = me.options; @@ -342,13 +342,13 @@ module.exports = LinearScaleBase.extend({ } else { me.setCenterPoint(0, 0, 0, 0); } - }, + } /** * Set radius reductions and determine new radius and center point * @private */ - setReductions: function(largestPossibleRadius, furthestLimits, furthestAngles) { + setReductions(largestPossibleRadius, furthestLimits, furthestAngles) { var me = this; var radiusReductionLeft = furthestLimits.l / Math.sin(furthestAngles.l); var radiusReductionRight = Math.max(furthestLimits.r - me.width, 0) / Math.sin(furthestAngles.r); @@ -364,9 +364,9 @@ module.exports = LinearScaleBase.extend({ Math.floor(largestPossibleRadius - (radiusReductionLeft + radiusReductionRight) / 2), Math.floor(largestPossibleRadius - (radiusReductionTop + radiusReductionBottom) / 2)); me.setCenterPoint(radiusReductionLeft, radiusReductionRight, radiusReductionTop, radiusReductionBottom); - }, + } - setCenterPoint: function(leftMovement, rightMovement, topMovement, bottomMovement) { + setCenterPoint(leftMovement, rightMovement, topMovement, bottomMovement) { var me = this; var maxRight = me.width - rightMovement - me.drawingArea; var maxLeft = leftMovement + me.drawingArea; @@ -375,9 +375,9 @@ module.exports = LinearScaleBase.extend({ me.xCenter = Math.floor(((maxLeft + maxRight) / 2) + me.left); me.yCenter = Math.floor(((maxTop + maxBottom) / 2) + me.top + me.paddingTop); - }, + } - getIndexAngle: function(index) { + getIndexAngle(index) { var chart = this.chart; var angleMultiplier = 360 / chart.data.labels.length; var options = chart.options || {}; @@ -387,9 +387,9 @@ module.exports = LinearScaleBase.extend({ var angle = (index * angleMultiplier + startAngle) % 360; return (angle < 0 ? angle + 360 : angle) * Math.PI * 2 / 360; - }, + } - getDistanceFromCenterForValue: function(value) { + getDistanceFromCenterForValue(value) { var me = this; if (helpers.isNullOrUndef(value)) { @@ -402,22 +402,22 @@ module.exports = LinearScaleBase.extend({ return (me.max - value) * scalingFactor; } return (value - me.min) * scalingFactor; - }, + } - getPointPosition: function(index, distanceFromCenter) { + getPointPosition(index, distanceFromCenter) { var me = this; var thisAngle = me.getIndexAngle(index) - (Math.PI / 2); return { x: Math.cos(thisAngle) * distanceFromCenter + me.xCenter, y: Math.sin(thisAngle) * distanceFromCenter + me.yCenter }; - }, + } - getPointPositionForValue: function(index, value) { + getPointPositionForValue(index, value) { return this.getPointPosition(index, this.getDistanceFromCenterForValue(value)); - }, + } - getBasePosition: function(index) { + getBasePosition(index) { var me = this; var min = me.min; var max = me.max; @@ -427,12 +427,12 @@ module.exports = LinearScaleBase.extend({ min < 0 && max < 0 ? max : min > 0 && max > 0 ? min : 0); - }, + } /** * @private */ - _drawGrid: function() { + _drawGrid() { var me = this; var ctx = me.ctx; var opts = me.options; @@ -475,12 +475,12 @@ module.exports = LinearScaleBase.extend({ ctx.restore(); } - }, + } /** * @private */ - _drawLabels: function() { + _drawLabels() { var me = this; var ctx = me.ctx; var opts = me.options; @@ -526,13 +526,14 @@ module.exports = LinearScaleBase.extend({ }); ctx.restore(); - }, + } /** * @private */ - _drawTitle: helpers.noop -}); + _drawTitle() {} +} +module.exports = RadialLinearScale; // INTERNAL: static default options, registered in src/index.js module.exports._defaults = defaultConfig; diff --git a/src/scales/scale.time.js b/src/scales/scale.time.js index 89ad6bb54da..0b9e4240f5a 100644 --- a/src/scales/scale.time.js +++ b/src/scales/scale.time.js @@ -1,17 +1,17 @@ 'use strict'; -var adapters = require('../core/core.adapters'); -var defaults = require('../core/core.defaults'); -var helpers = require('../helpers/index'); -var Scale = require('../core/core.scale'); +const adapters = require('../core/core.adapters'); +const defaults = require('../core/core.defaults'); +const helpers = require('../helpers/index'); +const Scale = require('../core/core.scale'); -var resolve = helpers.options.resolve; -var valueOrDefault = helpers.valueOrDefault; +const resolve = helpers.options.resolve; +const valueOrDefault = helpers.valueOrDefault; // Integer constants are from the ES6 spec. -var MAX_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; +const MAX_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991; -var INTERVALS = { +const INTERVALS = { millisecond: { common: true, size: 1, @@ -58,7 +58,7 @@ var INTERVALS = { } }; -var UNITS = Object.keys(INTERVALS); +const UNITS = Object.keys(INTERVALS); function sorter(a, b) { return a - b; @@ -477,7 +477,7 @@ function filterBetween(timestamps, min, max) { : timestamps; } -var defaultConfig = { +const defaultConfig = { position: 'bottom', /** @@ -526,15 +526,15 @@ var defaultConfig = { } }; -module.exports = Scale.extend({ - _parse: function(raw, index) { // eslint-disable-line no-unused-vars +class TimeScale extends Scale { + _parse(raw, index) { // eslint-disable-line no-unused-vars if (raw === undefined) { return NaN; } return parse(this, raw); - }, + } - _parseObject: function(obj, axis, index) { + _parseObject(obj, axis, index) { if (obj && obj.t) { return this._parse(obj.t, index); } @@ -542,13 +542,15 @@ module.exports = Scale.extend({ return this._parse(obj[axis], index); } return null; - }, + } - _invalidateCaches: function() { + _invalidateCaches() { this._cache = {}; - }, + } + + constructor(props) { + super(props); - initialize: function() { var me = this; var options = me.options; var time = options.time || (options.time = {}); @@ -563,11 +565,9 @@ module.exports = Scale.extend({ // missing formats on update helpers.mergeIf(time.displayFormats, adapter.formats()); + } - Scale.prototype.initialize.call(me); - }, - - determineDataLimits: function() { + determineDataLimits() { var me = this; var options = me.options; var adapter = me._adapter; @@ -601,9 +601,9 @@ module.exports = Scale.extend({ // Make sure that max is strictly higher than min (required by the lookup table) me.min = Math.min(min, max); me.max = Math.max(min + 1, max); - }, + } - buildTicks: function() { + buildTicks() { var me = this; var options = me.options; var timeOpts = options.time; @@ -640,9 +640,9 @@ module.exports = Scale.extend({ } return ticksFromTimestamps(me, ticks, me._majorUnit); - }, + } - getLabelForValue: function(value) { + getLabelForValue(value) { var me = this; var adapter = me._adapter; var timeOpts = me.options.time; @@ -651,13 +651,13 @@ module.exports = Scale.extend({ return adapter.format(value, timeOpts.tooltipFormat); } return adapter.format(value, timeOpts.displayFormats.datetime); - }, + } /** * Function to format an individual tick mark * @private */ - _tickFormatFunction: function(time, index, ticks, format) { + _tickFormatFunction(time, index, ticks, format) { var me = this; var adapter = me._adapter; var options = me.options; @@ -676,28 +676,28 @@ module.exports = Scale.extend({ ]); return formatter ? formatter(label, index, ticks) : label; - }, + } - generateTickLabels: function(ticks) { + generateTickLabels(ticks) { var i, ilen, tick; for (i = 0, ilen = ticks.length; i < ilen; ++i) { tick = ticks[i]; tick.label = this._tickFormatFunction(tick.value, i, ticks); } - }, + } /** * @private */ - _getPixelForOffset: function(time) { + _getPixelForOffset(time) { var me = this; var offsets = me._offsets; var pos = interpolate(me._table, 'time', time, 'pos'); return me.getPixelForDecimal((offsets.start + pos) * offsets.factor); - }, + } - getPixelForValue: function(value) { + getPixelForValue(value) { var me = this; if (typeof value !== 'number') { @@ -707,26 +707,26 @@ module.exports = Scale.extend({ if (value !== null) { return me._getPixelForOffset(value); } - }, + } - getPixelForTick: function(index) { + getPixelForTick(index) { var ticks = this.getTicks(); return index >= 0 && index < ticks.length ? this._getPixelForOffset(ticks[index].value) : null; - }, + } - getValueForPixel: function(pixel) { + getValueForPixel(pixel) { var me = this; var offsets = me._offsets; var pos = me.getDecimalForPixel(pixel) / offsets.factor - offsets.end; return interpolate(me._table, 'pos', pos, 'time'); - }, + } /** * @private */ - _getLabelSize: function(label) { + _getLabelSize(label) { var me = this; var ticksOpts = me.options.ticks; var tickLabelWidth = me.ctx.measureText(label).width; @@ -739,12 +739,12 @@ module.exports = Scale.extend({ w: (tickLabelWidth * cosRotation) + (tickFontSize * sinRotation), h: (tickLabelWidth * sinRotation) + (tickFontSize * cosRotation) }; - }, + } /** * @private */ - _getLabelCapacity: function(exampleTime) { + _getLabelCapacity(exampleTime) { var me = this; var timeOpts = me.options.time; var displayFormats = timeOpts.displayFormats; @@ -761,7 +761,8 @@ module.exports = Scale.extend({ return capacity > 0 ? capacity : 1; } -}); +} +module.exports = TimeScale; // INTERNAL: static default options, registered in src/index.js module.exports._defaults = defaultConfig;