Skip to content

Commit

Permalink
Refactor scales
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jun 18, 2019
1 parent b2aa8c7 commit e90e91d
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 174 deletions.
11 changes: 4 additions & 7 deletions src/controllers/controller.bar.js
Expand Up @@ -32,7 +32,7 @@ defaults._set('bar', {
* @private
*/
function computeMinSampleSize(scale, pixels) {
var min = scale.isHorizontal() ? scale.width : scale.height;
var min = scale._length;
var ticks = scale.getTicks();
var prev, curr, i, ilen;

Expand All @@ -42,7 +42,7 @@ function computeMinSampleSize(scale, pixels) {

for (i = 0, ilen = ticks.length; i < ilen; ++i) {
curr = scale.getPixelForTick(i);
min = i > 0 ? Math.min(min, curr - prev) : min;
min = i > 0 ? Math.min(min, Math.abs(curr - prev)) : min;
prev = curr;
}

Expand Down Expand Up @@ -262,9 +262,6 @@ module.exports = DatasetController.extend({
var scale = me._getIndexScale();
var stackCount = me.getStackCount();
var datasetIndex = me.index;
var isHorizontal = scale.isHorizontal();
var start = isHorizontal ? scale.left : scale.top;
var end = start + (isHorizontal ? scale.width : scale.height);
var pixels = [];
var i, ilen, min;

Expand All @@ -279,8 +276,8 @@ module.exports = DatasetController.extend({
return {
min: min,
pixels: pixels,
start: start,
end: end,
start: scale._start,
end: scale._end,
stackCount: stackCount,
scale: scale
};
Expand Down
3 changes: 3 additions & 0 deletions src/controllers/controller.horizontalBar.js
Expand Up @@ -23,6 +23,9 @@ defaults._set('horizontalBar', {
offset: true,
gridLines: {
offsetGridLines: true
},
ticks: {
reverse: true
}
}]
},
Expand Down
4 changes: 4 additions & 0 deletions src/core/core.layouts.js
Expand Up @@ -376,6 +376,10 @@ module.exports = {
// Move to next point
left = box.right;
}

if (box._configure) {
box._configure();
}
}

helpers.each(leftBoxes.concat(topBoxes), placeBox);
Expand Down
67 changes: 38 additions & 29 deletions src/core/core.scale.js
Expand Up @@ -267,6 +267,8 @@ var Scale = Element.extend({
me.setDimensions();
me.afterSetDimensions();

me._configure();

// Data min/max
me.beforeDataLimits();
me.determineDataLimits();
Expand Down Expand Up @@ -331,6 +333,25 @@ var Scale = Element.extend({
return me.minSize;

},

/**
* @private
*/
_configure: function() {
var me = this;

if (me.isHorizontal()) {
me._start = me.left;
me._end = me.right;
me._reverse = me.options.ticks.reverse;
} else {
me._start = me.top;
me._end = me.bottom;
me._reverse = !me.options.ticks.reverse;
}
me._length = me._end - me._start;
},

afterUpdate: function() {
helpers.callback(this.options.afterUpdate, [this]);
},
Expand Down Expand Up @@ -581,7 +602,7 @@ var Scale = Element.extend({
return this.options.position === 'top' || this.options.position === 'bottom';
},
isFullWidth: function() {
return (this.options.fullWidth);
return this.options.fullWidth;
},

// Get the correct value. NaN bad inputs, If the value type is object get the x or y based on whether we are horizontal or not
Expand Down Expand Up @@ -679,21 +700,9 @@ var Scale = Element.extend({
getPixelForTick: function(index) {
var me = this;
var offset = me.options.offset;
if (me.isHorizontal()) {
var innerWidth = me.width - (me.paddingLeft + me.paddingRight);
var tickWidth = innerWidth / Math.max((me._ticks.length - (offset ? 0 : 1)), 1);
var pixel = (tickWidth * index) + me.paddingLeft;

if (offset) {
pixel += tickWidth / 2;
}

var finalVal = me.left + pixel;
finalVal += me.isFullWidth() ? me.margins.left : 0;
return finalVal;
}
var innerHeight = me.height - (me.paddingTop + me.paddingBottom);
return me.top + (index * (innerHeight / (me._ticks.length - 1)));
var tickWidth = 1 / Math.max((me._ticks.length - (offset ? 0 : 1)), 1);
var decimal = index * tickWidth + (offset ? tickWidth / 2 : 0);
return me.getPixelForDecimal(decimal);
},

/**
Expand All @@ -702,15 +711,19 @@ var Scale = Element.extend({
*/
getPixelForDecimal: function(decimal) {
var me = this;
if (me.isHorizontal()) {
var innerWidth = me.width - (me.paddingLeft + me.paddingRight);
var valueOffset = (innerWidth * decimal) + me.paddingLeft;

var finalVal = me.left + valueOffset;
finalVal += me.isFullWidth() ? me.margins.left : 0;
return finalVal;
if (me._reverse) {
decimal = 1 - decimal;
}
return me.top + (decimal * me.height);

return me._start + decimal * me._length;
},

getDecimalForPixel: function(pixel) {
var me = this;
var decimal = (pixel - me._start) / me._length;

return Math.min(1, Math.max(0, me._reverse ? 1 - decimal : decimal));
},

/**
Expand Down Expand Up @@ -738,7 +751,6 @@ var Scale = Element.extend({
*/
_autoSkip: function(ticks) {
var me = this;
var isHorizontal = me.isHorizontal();
var optionTicks = me.options.ticks;
var tickCount = ticks.length;
var skipRatio = false;
Expand All @@ -749,9 +761,7 @@ var Scale = Element.extend({
var ticksLength = me._tickSize() * (tickCount - 1);

// Axis length
var axisLength = isHorizontal
? me.width - (me.paddingLeft + me.paddingRight)
: me.height - (me.paddingTop + me.PaddingBottom);
var axisLength = me._length;

var result = [];
var i, tick;
Expand Down Expand Up @@ -783,7 +793,6 @@ var Scale = Element.extend({
*/
_tickSize: function() {
var me = this;
var isHorizontal = me.isHorizontal();
var optionTicks = me.options.ticks;

// Calculate space needed by label in axis direction.
Expand All @@ -797,7 +806,7 @@ var Scale = Element.extend({
var h = labelSizes ? labelSizes.highest.height + padding : 0;

// Calculate space needed for 1 tick in axis direction.
return isHorizontal
return me.isHorizontal()
? h * cos > w * sin ? w / cos : h / sin
: h * sin < w * cos ? h / cos : w / sin;
},
Expand Down
74 changes: 29 additions & 45 deletions src/scales/scale.category.js
@@ -1,6 +1,7 @@
'use strict';

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

var defaultConfig = {
position: 'bottom'
Expand Down Expand Up @@ -48,71 +49,54 @@ module.exports = Scale.extend({
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) {
_getParams: function() {
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);
var range = Math.max((me.maxIndex + 1 - me.minIndex - (offset ? 0 : 1)), 1);
var start = me.minIndex - (offset ? 0.5 : 0);
return {
start: start,
range: range
};
},

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

// 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) {
if (!helpers.isNullOrUndef(value)) {
valueCategory = me.isHorizontal() ? value.x : value.y;
}
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
var labels = me._getLabels();
labels = me._getLabels();
value = valueCategory || value;
var idx = labels.indexOf(value);
idx = labels.indexOf(value);
index = idx !== -1 ? idx : index;
}

if (me.isHorizontal()) {
var valueWidth = me.width / offsetAmt;
var widthOffset = (valueWidth * (index - me.minIndex));

if (offset) {
widthOffset += (valueWidth / 2);
if (isNaN(index)) {
index = value;
}

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

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

return me.top + heightOffset;
return me.getPixelForDecimal((index - params.start) / params.range);
},

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

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);
}

if (pixel <= 0) {
value = 0;
} else {
value = Math.round(pixel / valueDimension);
}

return value + me.minIndex;
var params = me._getParams();
return Math.round(params.start + me.getDecimalForPixel(pixel) * params.range);
},

getBasePixel: function() {
Expand Down
21 changes: 6 additions & 15 deletions src/scales/scale.linear.js
Expand Up @@ -161,26 +161,17 @@ module.exports = LinearScaleBase.extend({
// This must be called after fit has been run so that
// this.left, this.top, this.right, and this.bottom have been defined
var me = this;
var start = me.start;

var start = me.min;
var rightValue = +me.getRightValue(value);
var pixel;
var range = me.end - start;

if (me.isHorizontal()) {
pixel = me.left + (me.width / range * (rightValue - start));
} else {
pixel = me.bottom - (me.height / range * (rightValue - start));
}
return pixel;
var range = me.max - start;
return me.getPixelForDecimal((rightValue - start) / range);
},

getValueForPixel: function(pixel) {
var me = this;
var isHorizontal = me.isHorizontal();
var innerDimension = isHorizontal ? me.width : me.height;
var offset = (isHorizontal ? pixel - me.left : me.bottom - pixel) / innerDimension;
return me.start + ((me.end - me.start) * offset);
var start = me.min;
var range = me.max - start;
return start + me.getDecimalForPixel(pixel) * range;
},

getPixelForTick: function(index) {
Expand Down

0 comments on commit e90e91d

Please sign in to comment.