Skip to content

Commit

Permalink
Refactor scales
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jun 20, 2019
1 parent 5ccb900 commit b24d251
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 164 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 @@ -186,6 +186,10 @@ function placeBoxes(boxes, chartArea, params) {
box.height = box.bottom - box.top;
x = box.right;
}

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

chartArea.x = x;
Expand Down
72 changes: 44 additions & 28 deletions src/core/core.scale.js
Expand Up @@ -70,9 +70,7 @@ function getPixelForGridLine(scale, index, offsetGridLines) {

if (offsetGridLines) {
if (scale.getTicks().length === 1) {
lineValue -= scale.isHorizontal() ?
Math.max(lineValue - scale.left, scale.right - lineValue) :
Math.max(lineValue - scale.top, scale.bottom - lineValue);
lineValue -= Math.max(lineValue - scale._start, scale._end - lineValue);
} else if (index === 0) {
lineValue -= (scale.getPixelForTick(1) - lineValue) / 2;
} else {
Expand Down Expand Up @@ -267,6 +265,8 @@ var Scale = Element.extend({
me.setDimensions();
me.afterSetDimensions();

me._configure();

// Data min/max
me.beforeDataLimits();
me.determineDataLimits();
Expand Down Expand Up @@ -331,6 +331,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 @@ -580,7 +599,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,20 +698,11 @@ var Scale = Element.extend({
var me = this;
var offset = me.options.offset;
var numTicks = me._ticks.length;
if (index < 0 || index > numTicks - 1) {
return null;
}
if (me.isHorizontal()) {
var tickWidth = me.width / Math.max((numTicks - (offset ? 0 : 1)), 1);
var pixel = (tickWidth * index);
var tickWidth = 1 / Math.max((numTicks - (offset ? 0 : 1)), 1);

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

return me.left + pixel;
}
return me.top + (index * (me.height / (numTicks - 1)));
return index < 0 || index > numTicks - 1
? null
: me.getPixelForDecimal(index * tickWidth + (offset ? tickWidth / 2 : 0));
},

/**
Expand All @@ -701,9 +711,19 @@ var Scale = Element.extend({
*/
getPixelForDecimal: function(decimal) {
var me = this;
return me.isHorizontal()
? me.left + decimal * me.width
: me.top + decimal * me.height;

if (me._reverse) {
decimal = 1 - decimal;
}

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 @@ -731,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 @@ -741,9 +760,7 @@ var Scale = Element.extend({
// drawn as their center at end of axis, so tickCount-1
var ticksLength = me._tickSize() * (tickCount - 1);

// Axis length
var axisLength = isHorizontal ? me.width : me.height;

var axisLength = me._length;
var result = [];
var i, tick;

Expand Down Expand Up @@ -774,7 +791,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 @@ -788,7 +804,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 Expand Up @@ -1116,7 +1132,7 @@ var Scale = Element.extend({
var scaleLabelX, scaleLabelY;

if (me.isHorizontal()) {
scaleLabelX = me.left + ((me.right - me.left) / 2); // midpoint of the width
scaleLabelX = me.left + me.width / 2; // midpoint of the width
scaleLabelY = position === 'bottom'
? me.bottom - halfLineHeight - scaleLabelPadding.bottom
: me.top + halfLineHeight + scaleLabelPadding.top;
Expand All @@ -1125,7 +1141,7 @@ var Scale = Element.extend({
scaleLabelX = isLeft
? me.left + halfLineHeight + scaleLabelPadding.top
: me.right - halfLineHeight - scaleLabelPadding.top;
scaleLabelY = me.top + ((me.bottom - me.top) / 2);
scaleLabelY = me.top + me.height / 2;
rotation = isLeft ? -0.5 * Math.PI : 0.5 * Math.PI;
}

Expand Down
54 changes: 19 additions & 35 deletions src/scales/scale.category.js
Expand Up @@ -63,17 +63,21 @@ 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, datasetIndex) {
_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);
return {
start: me.minIndex - (offset ? 0.5 : 0),
range: Math.max(me.ticks.length - (offset ? 0 : 1), 1)
};
},

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

if (!isNullOrUndef(index) && !isNullOrUndef(datasetIndex)) {
value = me.chart.data.datasets[datasetIndex].data[index];
Expand All @@ -82,22 +86,18 @@ module.exports = Scale.extend({
// 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.
if (!isNullOrUndef(value)) {
valueCategory = isHorizontal ? value.x : value.y;
valueCategory = me.isHorizontal() ? value.x : value.y;
}
if (valueCategory !== undefined || (value !== undefined && isNaN(index))) {
labels = me._getLabels();
value = helpers.valueOrDefault(valueCategory, value);
idx = labels.indexOf(value);
index = idx !== -1 ? idx : index;
if (isNaN(index)) {
index = value;
}
}

pixel = valueDimension * (index - me.minIndex);

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

return (isHorizontal ? me.left : me.top) + pixel;
return me.getPixelForDecimal((index - params.start) / params.range);
},

getPixelForTick: function(index) {
Expand All @@ -110,25 +110,9 @@ module.exports = Scale.extend({

getValueForPixel: function(pixel) {
var me = this;
var offset = me.options.offset;
var offsetAmt = Math.max(me._ticks.length - (offset ? 0 : 1), 1);
var isHorizontal = me.isHorizontal();
var valueDimension = (isHorizontal ? me.width : me.height) / offsetAmt;
var value;

pixel -= isHorizontal ? 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();
var value = Math.round(params.start + me.getDecimalForPixel(pixel) * params.range);
return Math.min(Math.max(value, 0), me.ticks.length - 1);
},

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 b24d251

Please sign in to comment.