Skip to content

Commit

Permalink
Fix for minor tick alignment issues when using the exactTick feature …
Browse files Browse the repository at this point in the history
…with negative values. Corrects the tick delta by using a correct modulus function, and prevents overflow past the maxValue.
  • Loading branch information
brblord committed Aug 1, 2017
1 parent 7e17166 commit 3cd48ba
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gauge.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gauge.min.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions lib/BaseGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,16 @@ export default class BaseGauge extends EventEmitter {

return value;
}

/**
* Corrects javascript modulus bug
* @param {number} n
* @param {number} m
* @return {number}
*/
static mod(n,m) {
return ((n % m) + m) % m;
}
}


Expand Down
6 changes: 4 additions & 2 deletions lib/LinearGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,12 @@ function drawLinearMinorTicks(context, options) {

if (minTicks) {
if (options.exactTicks) {
let delta = (options.majorTicks[0] % minTicks) || 0;
let delta = BaseGauge.mod(options.majorTicks[0], minTicks) || 0;

for (; i < options.maxValue; i += minTicks) {
ticks.push(delta + i);
if ((delta+i) < options.maxValue) {
ticks.push(delta + i);
}
}
}

Expand Down
15 changes: 8 additions & 7 deletions lib/RadialGauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function drawRadialMinorTicks(context, options) {
if (options.exactTicks) {
range = options.maxValue - options.minValue;
s = minTicks ? range / minTicks : 0;
delta = ((options.majorTicks[0] % minTicks) || 0) * ratio;
delta = (BaseGauge.mod(options.majorTicks[0], minTicks) || 0) * ratio;
}

else {
Expand All @@ -265,13 +265,14 @@ function drawRadialMinorTicks(context, options) {

for (; i < s; ++i) {
angle = options.startAngle + delta + i * (options.ticksAngle / s);
if (angle <= (options.ticksAngle + options.startAngle )) {
context.rotate(drawings.radians(angle));

context.rotate(drawings.radians(angle));

context.beginPath();
context.moveTo(0, radius);
context.lineTo(0, radius - context.max * 0.075);
closeStrokedPath(context);
context.beginPath();
context.moveTo(0, radius);
context.lineTo(0, radius - context.max * 0.075);
closeStrokedPath(context);
}
}
}

Expand Down

0 comments on commit 3cd48ba

Please sign in to comment.