Skip to content

Commit

Permalink
Refactor based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nagix committed Jun 17, 2019
1 parent 6384fa4 commit 62442ff
Showing 1 changed file with 27 additions and 42 deletions.
69 changes: 27 additions & 42 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ defaults._set('scale', {
}
});

function getPixelForGridLine(scale, index, isRightmost) {
var lineValue = scale.getPixelForTick(index);
function getPixelForGridLine(scale, index, offsetGridLines) {
var length = scale.getTicks().length;
var validIndex = Math.min(index, length - 1);
var lineValue = scale.getPixelForTick(validIndex);
var epsilon = 1e-6; // 1e-6 is margin in pixels for accumulated error.
var start, end, offset;

if (scale.options.gridLines.offsetGridLines) {
if (offsetGridLines) {
if (scale.isHorizontal()) {
start = scale.left;
end = scale.right;
Expand All @@ -79,14 +81,14 @@ function getPixelForGridLine(scale, index, isRightmost) {
end = scale.bottom;
}

if (scale.getTicks().length === 1) {
if (length === 1) {
offset = Math.max(lineValue - start, end - lineValue);
} else if (index === 0) {
offset = (scale.getPixelForTick(1) - lineValue) / 2;
} else {
offset = (lineValue - scale.getPixelForTick(index - 1)) / 2;
offset = (lineValue - scale.getPixelForTick(validIndex - 1)) / 2;
}
lineValue += isRightmost ? offset : -offset;
lineValue += validIndex < index ? offset : -offset;

// Return undefined if the pixel is out of the range
if (lineValue < start - epsilon || lineValue > end + epsilon) {
Expand Down Expand Up @@ -892,13 +894,19 @@ var Scale = Element.extend({
tickEnd = me.left + tl;
}

if (offsetGridLines) {
ticks = ticks.concat([{extra: true}]);
}

helpers.each(ticks, function(tick, index) {
var label = tick.label;
var extra = tick.extra;

// autoskipper skipped this tick (#4635)
if (helpers.isNullOrUndef(tick.label)) {
if (helpers.isNullOrUndef(label) && !extra) {
return;
}

var label = tick.label;
var tickFont = tick.major ? tickFonts.major : tickFonts.minor;
var lineHeight = tickFont.lineHeight;
var lineWidth, lineColor, borderDash, borderDashOffset;
Expand All @@ -918,7 +926,7 @@ var Scale = Element.extend({
// Common properties
var tx1, ty1, tx2, ty2, x1, y1, x2, y2, labelX, labelY, textOffset, textAlign;
var labelCount = helpers.isArray(label) ? label.length : 1;
var lineValue = getPixelForGridLine(me, index);
var lineValue = getPixelForGridLine(me, index, offsetGridLines);

if (isHorizontal) {
var labelYOffset = tl + tickPadding;
Expand Down Expand Up @@ -980,40 +988,17 @@ var Scale = Element.extend({
});
}

if (index === ticks.length - 1 && offsetGridLines) {
lineValue = getPixelForGridLine(me, index, true);
if (lineValue !== undefined) {
if (isHorizontal) {
tx1 = tx2 = x1 = x2 = alignPixel(chart, lineValue, lineWidth);
} else {
ty1 = ty2 = y1 = y2 = alignPixel(chart, lineValue, lineWidth);
}
gridLineItems.push({
tx1: tx1,
ty1: ty1,
tx2: tx2,
ty2: ty2,
x1: x1,
y1: y1,
x2: x2,
y2: y2,
width: valueAtIndexOrDefault(gridLines.lineWidth, index + 1, 1),
color: valueAtIndexOrDefault(gridLines.color, index + 1, 'rgba(0,0,0,0.1)'),
borderDash: gridLines.borderDash || [],
borderDashOffset: gridLines.borderDashOffset || 0.0,
});
}
if (!extra) {
labelItems.push({
x: labelX,
y: labelY,
rotation: -labelRotationRadians,
label: label,
font: tick.major ? tickFonts.major : tickFonts.minor,
textOffset: textOffset,
textAlign: textAlign
});
}

labelItems.push({
x: labelX,
y: labelY,
rotation: -labelRotationRadians,
label: label,
font: tick.major ? tickFonts.major : tickFonts.minor,
textOffset: textOffset,
textAlign: textAlign
});
});

gridLineItems.ticksLength = ticks.length;
Expand Down

0 comments on commit 62442ff

Please sign in to comment.