Skip to content

Commit 5eedc81

Browse files
committed
fix: sectors not shown when min is negative
Fixes #404
1 parent 8d1a647 commit 5eedc81

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/rendering/svg.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ export class SVGRenderer {
165165

166166
/**
167167
* Create gauge path
168-
* @param {number|{from: number, to: number}} sector
168+
* @param {number|{from: number, to: number}} sectorPctOrValue
169169
* @param {number} min
170170
* @param {number} max
171171
* @param {number} widgetW
@@ -178,7 +178,7 @@ export class SVGRenderer {
178178
* @returns {string} SVG path data for the gauge
179179
*/
180180
createGaugePath(
181-
sector,
181+
sectorPctOrValue,
182182
min,
183183
max,
184184
widgetW,
@@ -198,21 +198,54 @@ export class SVGRenderer {
198198
let Xstart, Ystart, XstartInner, YstartInner; // start point coordinates
199199
let path; // SVG path string
200200

201+
// Track if sector was originally a number (absolute value)
202+
const sectorWasNumber = typeof sectorPctOrValue === 'number';
203+
201204
if (min < 0 && !isDiff) {
202205
max -= min;
203-
sector -= min;
206+
if (sectorWasNumber) {
207+
sectorPctOrValue -= min;
208+
}
204209
min = 0;
205210
}
206211

207-
if (typeof sector === 'number') {
208-
sector = { from: min, to: sector };
212+
if (sectorWasNumber) {
213+
sectorPctOrValue = { from: min, to: sectorPctOrValue };
209214
}
210215

211-
const deltaVStart = sector.from - min;
212-
const deltaVEnd = sector.to - min;
216+
// Calculate range (needed for differential gauge middle calculation)
213217
const range = max - min;
214-
const pctStart = deltaVStart / range;
215-
const pctEnd = deltaVEnd / range;
218+
219+
// Determine percentage values based on input format
220+
let pctStart, pctEnd;
221+
222+
if (sectorWasNumber) {
223+
// When sector was a number, it's an absolute value, so convert it
224+
const deltaVStart = sectorPctOrValue.from - min;
225+
const deltaVEnd = sectorPctOrValue.to - min;
226+
pctStart = deltaVStart / range;
227+
pctEnd = deltaVEnd / range;
228+
} else if (
229+
typeof sectorPctOrValue.from === 'number' &&
230+
typeof sectorPctOrValue.to === 'number'
231+
) {
232+
// Sector is an object, check if values are normalized (0-1) or percentage (0-100)
233+
if (sectorPctOrValue.from <= 1 && sectorPctOrValue.to <= 1) {
234+
// Already normalized (0-1 range)
235+
pctStart = sectorPctOrValue.from;
236+
pctEnd = sectorPctOrValue.to;
237+
} else {
238+
// Percentage format (0-100 range), convert to 0-1
239+
pctStart = sectorPctOrValue.from / 100;
240+
pctEnd = sectorPctOrValue.to / 100;
241+
}
242+
} else {
243+
// Fallback: treat as absolute values (legacy behavior)
244+
const deltaVStart = sectorPctOrValue.from - min;
245+
const deltaVEnd = sectorPctOrValue.to - min;
246+
pctStart = deltaVStart / range;
247+
pctEnd = deltaVEnd / range;
248+
}
216249

217250
if (donut) {
218251
// Calculate end point angle
@@ -296,9 +329,9 @@ export class SVGRenderer {
296329
Xi = Cx + Ri * Math.cos(alpha);
297330
Yi = Cy - Ri * Math.sin(alpha);
298331

299-
const middle = min + range / 2;
300-
const So = sector.to < middle ? 1 : 0; // sweep flag for outer arc, use opposite direction if value < middle
301-
const Si = sector.to < middle ? 0 : 1; // sweep flag for inner arc, use opposite direction if value < middle
332+
const middlePct = 0.5; // 50% is the middle of the differential gauge
333+
const So = pctEnd < middlePct ? 1 : 0; // sweep flag for outer arc, use opposite direction if value < middle
334+
const Si = pctEnd < middlePct ? 0 : 1; // sweep flag for inner arc, use opposite direction if value < middle
302335

303336
path = 'M' + Cx + ',' + (Cy - Ri) + ' '; // start at bottom center
304337
path += 'L' + Cx + ',' + (Cy - Ro) + ' '; // line to top center (Cx, Cy - Ro)

0 commit comments

Comments
 (0)