Skip to content

Commit

Permalink
fix min angle
Browse files Browse the repository at this point in the history
  • Loading branch information
briangann committed Sep 25, 2023
1 parent 96e819d commit e85ede7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

All changes noted here.

## v2.0.0 - 2023-09-21
## v2.0.0 - 2023-09-23

- Rewritten from Angular to React
- NEW: Needle Width can now be set
- NEW: Needle Width can now be specified
- NEW: Thresholds now use the standard Grafana threshold mechanics
- NEW: Needle can optionally exceed the tick mark (min and max) to show values
that are outside of limits
- NEW: Preset gauge types can be selected and then modified
- NEW: Needle Center can use all marker types, with arrow-inverse added to options

## v0.0.9 - 2021-04-21

Expand Down
4 changes: 2 additions & 2 deletions src/components/Gauge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,10 @@ export const Gauge: React.FC<GaugeOptions> = (options) => {
needleAngleOld = 0;
}
if (needleAngleNew + options.zeroNeedleAngle > options.maxTickAngle) {
needleAngleNew = getNeedleAngleMaximum(options.allowNeedleCrossLimits, needleAngleNew, options.zeroTickAngle, options.maxTickAngle, options.needleCrossLimitDegrees);
needleAngleNew = getNeedleAngleMaximum(options.allowNeedleCrossLimits, needleAngleNew, options.zeroTickAngle, options.zeroNeedleAngle, options.maxTickAngle, options.needleCrossLimitDegrees);
}
if (needleAngleNew + options.zeroNeedleAngle < options.zeroTickAngle) {
needleAngleNew = getNeedleAngleMinimum(options.allowNeedleCrossLimits, needleAngleNew, options.zeroTickAngle, options.needleCrossLimitDegrees);
needleAngleNew = getNeedleAngleMinimum(options.allowNeedleCrossLimits, needleAngleNew, options.zeroTickAngle, options.zeroNeedleAngle, options.needleCrossLimitDegrees);
}
const needleCentre = originX + ',' + originY;
return interpolateString(
Expand Down
30 changes: 15 additions & 15 deletions src/components/needle_utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,49 @@ describe('Needle Utils', () => {
const crossLimitDegree = 5;
describe('Check Min Needle Angle with cross limits disabled', () => {
it('minimum angle should be at min', () => {
const atZero = getNeedleAngleMinimum(false, 0, 90, 5);
const atZero = getNeedleAngleMinimum(false, 0, 90, 90, 5);
expect(atZero).toEqual(0);
const aboveZero = getNeedleAngleMinimum(false, 30, 90, 5);
const aboveZero = getNeedleAngleMinimum(false, 30, 90, 90, 5);
expect(aboveZero).toEqual(30);
const belowZero = getNeedleAngleMinimum(false, -10, 90, 5);
const belowZero = getNeedleAngleMinimum(false, -10, 90, 90, 5);
expect(belowZero).toEqual(90);
// this will return a value beyond the max (270), and will be caught by code logic
const aboveMax = getNeedleAngleMinimum(false, 300, 90, 5);
const aboveMax = getNeedleAngleMinimum(false, 300, 90, 90, 5);
expect(aboveMax).toEqual(300);
});
});
describe('Check Min Needle Angle with cross limits enabled', () => {
it('minimum angle should be bound by min with limit cross of 5 degrees', () => {
const belowZero = getNeedleAngleMinimum(true, -10, 90, crossLimitDegree);
const belowZero = getNeedleAngleMinimum(true, -10, 90, 90, crossLimitDegree);
expect(belowZero).toEqual(-5);
const atZero = getNeedleAngleMinimum(true, 0, 90, crossLimitDegree);
const atZero = getNeedleAngleMinimum(true, 0, 90, 90, crossLimitDegree);
expect(atZero).toEqual(0);
const aboveZero = getNeedleAngleMinimum(true, 30, 90, crossLimitDegree);
const aboveZero = getNeedleAngleMinimum(true, 30, 90, 90, crossLimitDegree);
expect(aboveZero).toEqual(30);
const aboveMax = getNeedleAngleMinimum(true, 300, 90, crossLimitDegree);
const aboveMax = getNeedleAngleMinimum(true, 300, 90, 90, crossLimitDegree);
expect(aboveMax).toEqual(300);
});
});

describe('Check Max Needle Angle with cross limits disabled', () => {
it('max angle should be bound by maxTickAngle', () => {
const atMax = getNeedleAngleMaximum(false, 270, 90, 270, crossLimitDegree);
const atMax = getNeedleAngleMaximum(false, 270, 90, 90, 270, crossLimitDegree);
expect(atMax).toEqual(180);
const belowMax = getNeedleAngleMaximum(false, 30, 90, 270, crossLimitDegree);
const belowMax = getNeedleAngleMaximum(false, 30, 90, 90, 270, crossLimitDegree);
expect(belowMax).toEqual(-60);
const aboveMax = getNeedleAngleMaximum(false, 300, 90, 270, crossLimitDegree);
const aboveMax = getNeedleAngleMaximum(false, 300, 90, 90, 270, crossLimitDegree);
expect(aboveMax).toEqual(180);
});
});
describe('Check Max Needle Angle with cross limits enabled', () => {
it('max angle should be bound by max with limit cross of 5 degrees', () => {
const atMax = getNeedleAngleMaximum(true, 270, 90, 270, crossLimitDegree);
const atMax = getNeedleAngleMaximum(true, 270, 90, 90, 270, crossLimitDegree);
expect(atMax).toEqual(185);
const belowMax = getNeedleAngleMaximum(true, 30, 90, 270, crossLimitDegree);
const belowMax = getNeedleAngleMaximum(true, 30, 90, 90, 270, crossLimitDegree);
expect(belowMax).toEqual(-60);
const aboveMax = getNeedleAngleMaximum(true, 275, 90, 270, crossLimitDegree);
const aboveMax = getNeedleAngleMaximum(true, 275, 90, 90, 270, crossLimitDegree);
expect(aboveMax).toEqual(185);
const aboveMax2 = getNeedleAngleMaximum(true, 320, 90, 270, crossLimitDegree);
const aboveMax2 = getNeedleAngleMaximum(true, 320, 90, 90, 270, crossLimitDegree);
expect(aboveMax2).toEqual(185);
});
});
Expand Down
29 changes: 13 additions & 16 deletions src/components/needle_utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,29 @@ import React from 'react';
* @param allowNeedleCrossLimits boolean
* @param needleAngle angle to test
* @param zeroTickAngle angle where the tick label starts
* @param zeroNeedleAngle angle where the needle starts
* @param crossLimitDegree how far to cross limits
* @returns angle to be used (relative to the zeroNeedleAnge)
* @returns angle to be used (absolute angle, no longer relative)
*/
export const getNeedleAngleMinimum =
(allowNeedleCrossLimits: boolean, needleAngle: number, zeroTickAngle: number, crossLimitDegree: number) => {
// the angle is relative to the zeroNeedleAngle
// check if the needleAngle is below the zeroNeedleAngle
if (needleAngle + zeroTickAngle < zeroTickAngle) {
(allowNeedleCrossLimits: boolean, needleAngle: number, zeroTickAngle: number, zeroNeedleAngle: number, crossLimitDegree: number) => {
// check if the needleAngle is below the zeroTickAngle
if (needleAngle + zeroNeedleAngle < zeroTickAngle) {
// check if burying the needle is enabled
if (allowNeedleCrossLimits) {
// make sure it is not below zero when accounting for the zeroNeedleAngle
if (zeroTickAngle >= crossLimitDegree) {
// allow it to be set to zeroTickAngle minus 5 degrees, without going below zero
if (needleAngle < zeroTickAngle) {
return (-crossLimitDegree);
} else {
return (zeroTickAngle);
return (zeroNeedleAngle);
}
} else {
return (zeroTickAngle);
return (zeroNeedleAngle);
}
}
return needleAngle;
};

export const getNeedleAngleMaximum = (allowNeedleCrossLimits: boolean, needleAngle: number, zeroTickAngle: number, maxTickAngle: number, crossLimitDegree: number) => {
export const getNeedleAngleMaximum = (allowNeedleCrossLimits: boolean, needleAngle: number, zeroTickAngle: number, zeroNeedleAngle: number, maxTickAngle: number, crossLimitDegree: number) => {
// angle passed in is relative to zeroTickAngle
if (needleAngle + zeroTickAngle > maxTickAngle) {
if (allowNeedleCrossLimits) {
Expand All @@ -50,16 +48,15 @@ export const getNeedleAngleMaximum = (allowNeedleCrossLimits: boolean, needleAng
// make sure it is not above 360 minus cross limit
if (maxTickAngle < (360 - crossLimitDegree)) {
// allow it to be set to maxTickAngle plus 5 degrees, without going below zero
return(maxTickAngle + crossLimitDegree - zeroTickAngle);
return (maxTickAngle + crossLimitDegree - zeroNeedleAngle);
} else {
// console.log(`needle cannot be buried beyond maxNeedleAngle ${testMaxAngle}`);
return(maxTickAngle - zeroTickAngle);
return (maxTickAngle - zeroNeedleAngle);
}
}
} else {
return (maxTickAngle - zeroTickAngle);
return (maxTickAngle - zeroNeedleAngle);
}
}
// remove the zeroTickAngle to get the relative value again
return (needleAngle - zeroTickAngle);
return (needleAngle - zeroNeedleAngle);
};

0 comments on commit e85ede7

Please sign in to comment.