Skip to content

Commit d73c154

Browse files
committed
Internal lib - Fix edge cases for arc generation
1 parent 5551328 commit d73c154

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/lib.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ export function makeDonut(item, cx, cy, rx, ry, piProportion = 1.99999, piMult =
1414
center: {},
1515
};
1616
const sum = [...series]
17-
.map((serie) => serie.value)
17+
.map((serie) => {
18+
return serie.value
19+
})
1820
.reduce((a, b) => a + b, 0);
1921

2022
const ratios = [];
2123
let acc = 0;
2224
for (let i = 0; i < series.length; i += 1) {
23-
let proportion = series[i].value / sum;
25+
const val = Math.min(series[i].value - 0.0001, sum);
26+
let proportion = val / sum;
2427
const ratio = proportion * (Math.PI * piProportion); // (Math.PI * 2) fails to display a donut with only one value > 0 as it goes full circle again
2528
// midProportion & midRatio are used to find the midpoint of the arc to display markers
26-
const midProportion = series[i].value / 2 / sum;
29+
const midProportion = val / 2 / sum;
2730
const midRatio = midProportion * (Math.PI * piMult);
2831
const { startX, startY, endX, endY, path } = createArc(
2932
[cx, cy],

tests/lib.test.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -462,65 +462,65 @@ describe("makeDonut", () => {
462462
expect(donut).toStrictEqual([
463463
{
464464
arcSlice:
465-
"M100.06646055814842 90.00022085272826 A 10 10 6030.380793751915 0 1 108.69326717473096 95.05762144016212 L 108.69326717473096 95.05762144016212 M108.69326717473096 95.05762144016212 A 10 10 6030.380793751915 0 0 100.06646055814842 90.00022085272826 L 100.06646055814842 90.00022085272826",
465+
"M100.06646055814842 90.00022085272826 A 10 10 6030.380793751915 0 1 108.69274956498154 95.0567111150052 L 108.69274956498154 95.0567111150052 M108.69274956498154 95.0567111150052 A 10 10 6030.380793751915 0 0 100.06646055814842 90.00022085272826 L 100.06646055814842 90.00022085272826",
466466
cx: 100,
467467
cy: 100,
468468
value: 1,
469-
proportion: 0.16666666666666666,
470-
ratio: 1.0471923152088416,
471-
path: "M100.06646055814842 90.00022085272826 A 10 10 6030.380793751915 0 1 108.69326717473096 95.05762144016212",
469+
proportion: 0.16665,
470+
ratio: 1.0470875959773207,
471+
path: "M100.06646055814842 90.00022085272826 A 10 10 6030.380793751915 0 1 108.69274956498154 95.0567111150052",
472472
startX: 100.06646055814842,
473473
startY: 90.00022085272826,
474-
endX: 108.69326717473096,
475-
endY: 95.05762144016212,
474+
endX: 108.69274956498154,
475+
endY: 95.0567111150052,
476476
center: {
477477
startX: 100.09636780931521,
478478
startY: 85.50032023645599,
479-
endX: 107.33329685274603,
480-
endY: 87.49109288268934,
481-
path: "M100.09636780931521 85.50032023645599 A 14.5 14.5 6030.380793751915 0 1 107.33329685274603 87.49109288268934",
479+
endX: 107.33264187784894,
480+
endY: 87.49070892931114,
481+
path: "M100.09636780931521 85.50032023645599 A 14.5 14.5 6030.380793751915 0 1 107.33264187784894 87.49070892931114",
482482
},
483483
},
484484
{
485485
arcSlice:
486-
"M108.69326717473096 95.05762144016212 A 10 10 6030.380793751915 0 1 99.93369651802331 109.99978018999806 L 99.93369651802331 109.99978018999806 M99.93369651802331 109.99978018999806 A 10 10 6030.380793751915 0 0 108.69326717473096 95.05762144016212 L 108.69326717473096 95.05762144016212",
486+
"M108.69274956498154 95.0567111150052 A 10 10 6030.380793751915 0 1 99.93579085805592 109.9997938571798 L 99.93579085805592 109.9997938571798 M99.93579085805592 109.9997938571798 A 10 10 6030.380793751915 0 0 108.69274956498154 95.0567111150052 L 108.69274956498154 95.0567111150052",
487487
cx: 100,
488488
cy: 100,
489489
value: 2,
490-
proportion: 0.3333333333333333,
491-
ratio: 2.094384630417683,
492-
path: "M108.69326717473096 95.05762144016212 A 10 10 6030.380793751915 0 1 99.93369651802331 109.99978018999806",
493-
startX: 108.69326717473096,
494-
startY: 95.05762144016212,
495-
endX: 99.93369651802331,
496-
endY: 109.99978018999806,
490+
proportion: 0.33331666666666665,
491+
ratio: 2.0942799111861623,
492+
path: "M108.69274956498154 95.0567111150052 A 10 10 6030.380793751915 0 1 99.93579085805592 109.9997938571798",
493+
startX: 108.69274956498154,
494+
startY: 95.0567111150052,
495+
endX: 99.93579085805592,
496+
endY: 109.9997938571798,
497497
center: {
498-
startX: 112.6052374033599,
499-
startY: 92.83355108823508,
500-
endX: 112.50894551419171,
501-
endY: 107.33323135616101,
502-
path: "M112.6052374033599 92.83355108823508 A 14.5 14.5 6030.380793751915 0 1 112.50894551419171 107.33323135616101",
498+
startX: 112.60448686922324,
499+
startY: 92.83223111675754,
500+
endX: 112.51009725731319,
501+
endY: 107.33126637168266,
502+
path: "M112.60448686922324 92.83223111675754 A 14.5 14.5 6030.380793751915 0 1 112.51009725731319 107.33126637168266",
503503
},
504504
},
505505
{
506506
arcSlice:
507-
"M99.93369651802331 109.99978018999806 A 10 10 6030.380793751915 0 1 100.06614640578861 90.00021876974294 L 100.06614640578861 90.00021876974294 M100.06614640578861 90.00021876974294 A 10 10 6030.380793751915 0 0 99.93369651802331 109.99978018999806 L 99.93369651802331 109.99978018999806",
507+
"M99.93579085805592 109.9997938571798 A 10 10 6030.380793751915 0 1 100.06300489435868 90.00019848280543 L 100.06300489435868 90.00019848280543 M100.06300489435868 90.00019848280543 A 10 10 6030.380793751915 0 0 99.93579085805592 109.9997938571798 L 99.93579085805592 109.9997938571798",
508508
cx: 100,
509509
cy: 100,
510510
value: 3,
511-
proportion: 0.5,
512-
ratio: 3.1415769456265252,
513-
path: "M99.93369651802331 109.99978018999806 A 10 10 6030.380793751915 0 1 100.06614640578861 90.00021876974294",
514-
startX: 99.93369651802331,
515-
startY: 109.99978018999806,
516-
endX: 100.06614640578861,
517-
endY: 90.00021876974294,
511+
proportion: 0.4999833333333333,
512+
ratio: 3.141472226395004,
513+
path: "M99.93579085805592 109.9997938571798 A 10 10 6030.380793751915 0 1 100.06300489435868 90.00019848280543",
514+
startX: 99.93579085805592,
515+
startY: 109.9997938571798,
516+
endX: 100.06300489435868,
517+
endY: 90.00019848280543,
518518
center: {
519-
startX: 99.9038599511338,
520-
startY: 114.4996812754972,
521-
endX: 85.5003187245028,
522-
endY: 99.9038599511338,
523-
path: "M99.9038599511338 114.4996812754972 A 14.5 14.5 6030.380793751915 0 1 85.5003187245028 99.9038599511338",
519+
startX: 99.9068967441811,
520+
startY: 114.4997010929107,
521+
endX: 85.5002940520901,
522+
endY: 99.90765594688226,
523+
path: "M99.9068967441811 114.4997010929107 A 14.5 14.5 6030.380793751915 0 1 85.5002940520901 99.90765594688226",
524524
},
525525
},
526526
]);

0 commit comments

Comments
 (0)