Skip to content

Commit

Permalink
fix: make labels take up less pie chart space
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom committed Jan 9, 2025
1 parent c9667fc commit 83b92b9
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 133 deletions.
4 changes: 4 additions & 0 deletions src/hooks/useShapes/area/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ export const prepareAreaData = (args: {
const labelItems = points.map<LabelData>((p) => getLabelData(p, s, xMax));
if (s.dataLabels.html) {
const htmlLabels = labelItems.map((l) => {
const style = l.style ?? s.dataLabels.style;
const labelSize = getLabelsSize({labels: [l.text], style, html: true});

return {
x: l.x - l.size.width / 2,
y: l.y,
content: l.text,
size: {width: labelSize.maxWidth, height: labelSize.maxHeight},
};
});
htmlElements.push(...htmlLabels);
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useShapes/bar-x/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export const prepareBarXData = (args: {
x: label.x,
y: label.y,
content: label.text,
size: label.size,
});
} else {
barData.label = getLabelData(barData);
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useShapes/bar-y/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function setLabel(prepared: PreparedBarYData) {
x,
y: y - height / 2,
content,
size: {width, height},
});
} else {
prepared.label = {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useShapes/line/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function getHtmlLabel(point: PointData, series: PreparedLineSeries, xMax: number
x: Math.min(xMax - size.maxWidth, Math.max(0, point.x)),
y: Math.max(0, point.y - series.dataLabels.padding - size.maxHeight),
content,
size: {width: size.maxWidth, height: size.maxHeight},
};
}

Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useShapes/pie/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ export function PieSeriesShapes(args: PreparePieSeriesArgs) {
};
}, [dispatcher, preparedData, seriesOptions]);

const htmlElements = preparedData.map((d) => d.htmlLabels).flat();

return (
<React.Fragment>
<g ref={ref} className={b()} style={{zIndex: 9}} />
<HtmlLayer preparedData={preparedData} htmlLayout={htmlLayout} />
<HtmlLayer preparedData={{htmlElements}} htmlLayout={htmlLayout} />
</React.Fragment>
);
}
Loading

0 comments on commit 83b92b9

Please sign in to comment.