Skip to content

Commit 2b71f8c

Browse files
committed
chore(charts): integrate pie chart
1 parent a3c12b6 commit 2b71f8c

4 files changed

Lines changed: 25 additions & 8 deletions

File tree

src/charts/geometry.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ export const polarToCartesian = (cx, cy, r, angleDeg) => {
1111
export const describeSegment = (cx, cy, innerR, outerR, startAngle, endAngle) => {
1212
const startOuter = polarToCartesian(cx, cy, outerR, endAngle);
1313
const endOuter = polarToCartesian(cx, cy, outerR, startAngle);
14+
const largeArcFlag = endAngle - startAngle <= 180 ? '0' : '1';
15+
16+
if (innerR === 0) {
17+
return `
18+
M ${cx} ${cy}
19+
L ${startOuter.x} ${startOuter.y}
20+
A ${outerR} ${outerR} 0 ${largeArcFlag} 0 ${endOuter.x} ${endOuter.y}
21+
Z
22+
`.trim();
23+
}
24+
1425
const startInner = polarToCartesian(cx, cy, innerR, startAngle);
1526
const endInner = polarToCartesian(cx, cy, innerR, endAngle);
16-
const largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
1727

1828
return `
1929
M ${startOuter.x} ${startOuter.y}
@@ -28,9 +38,9 @@ export const createDonutSegments = (languages, cx, geometry, colours, stroke) =>
2838
let currentAngle = -0.1;
2939

3040
return languages.map((lang, i) => {
31-
let angle = (lang.pct / 100) * 360;
41+
let angle = (lang.pct / 100) * 360;
3242

33-
const segmentAngle = Math.min(currentAngle + angle + 0.1, FULL_CIRCLE_ANGLE);
43+
const segmentAngle = Math.min(currentAngle + angle + 0.1, FULL_CIRCLE_ANGLE);
3444
const path = describeSegment(
3545
cx,
3646
geometry.CENTER_Y,
@@ -39,9 +49,9 @@ export const createDonutSegments = (languages, cx, geometry, colours, stroke) =>
3949
currentAngle,
4050
segmentAngle
4151
);
42-
52+
4353
currentAngle += angle;
44-
const fillColour = colours[i % colours.length];
54+
const fillColour = colours[i % colours.length];
4555
const strokeAttr = stroke
4656
? ` stroke="#000" stroke-width="0.5" stroke-linejoin="round"`
4757
: ` stroke="${fillColour}" stroke-width="0.2"`;

src/constants/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const VALID_TYPES = ["donut"];
1+
export const VALID_TYPES = ["donut", "pie"];

src/render/chart.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { generateDonutChart } from "../charts/donut.js";
2+
import { generatePieChart } from "../charts/pie.js";
23

34
const CHART_GENERATORS = {
45
donut: generateDonutChart,
6+
pie: generatePieChart,
57
}
68

7-
export function generateChartData(data, selectedTheme, chartType, width) {
9+
export function generateChartData(data, theme, chartType, width, stroke) {
810
const generator = CHART_GENERATORS[chartType] || CHART_GENERATORS.donut;
9-
return generator(data, selectedTheme, width);
11+
return generator(data, theme, width, stroke);
1012
}

tests/charts/geometry.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ describe("donut geometry", () => {
2222
expect(path).toMatch(/Z$/);
2323
});
2424

25+
it("describeSegment: filled pie starts at center", () => {
26+
const path = describeSegment(100, 100, 0, 50, 0, 90);
27+
expect(path.startsWith("M 100 100")).toBe(true);
28+
});
29+
2530
it("describeSegment: segments have stroke when enabled", () => {
2631
const paths = createDonutSegments(
2732
[{ pct: 50 }],

0 commit comments

Comments
 (0)