Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Full Title in Pie Chart SVG #5596

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions cypress/integration/rendering/pie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@ describe('pie chart', () => {
);
});

it('should render a simple pie diagram with long title', () => {
renderGraph(
`pie title Sports in Sweden that are my favorite to watch
"Bandy": 40
"Ice-Hockey": 80
"Football": 90
`
);
cy.get('svg').should((svg) => {
expect(svg).to.have.attr('width', '100%');
const style = svg.attr('style');
expect(style).to.match(/^max-width: [\d.]+px;$/);
const maxWidthValue = parseFloat(style.match(/[\d.]+/g).join(''));
expect(maxWidthValue).to.be.within(730, 750); // depends on installed fonts
});
});

it('should render a simple pie diagram with long title and long labels', () => {
renderGraph(
`pie title Time usage on NETFLIX the last few movie nights
"Time spent looking for movie": 90
"Time spent watching it": 10
`
);
cy.get('svg').should((svg) => {
expect(svg).to.have.attr('width', '100%');
const style = svg.attr('style');
expect(style).to.match(/^max-width: [\d.]+px;$/);
const maxWidthValue = parseFloat(style.match(/[\d.]+/g).join(''));
expect(maxWidthValue).to.be.within(900, 920); // depends on installed fonts
});
});

it('should render a simple pie diagram that is centered with long title', () => {
renderGraph(
`pie title Sports in Sweden that are my favorite to watch
"Bandy": 40
"Ice-Hockey": 80
"Football": 90
`
);
cy.get('g')
.eq(1)
.should((g) => {
const transform = g.attr('transform');
expect(transform).to.match(/translate\(\d+(\.\d+)?,\d+(\.\d+)?\)/);

const translateValues = transform.match(/translate\((\d+(\.\d+)?),(\d+(\.\d+)?)\)/);
const translateX = parseFloat(translateValues[1]);

expect(translateX).to.be.within(280, 300);
});
});

it('should render a simple pie diagram with capital letters for labels', () => {
imgSnapshotTest(
`pie title What Voldemort doesn't have?
Expand All @@ -44,7 +98,7 @@ describe('pie chart', () => {
const style = svg.attr('style');
expect(style).to.match(/^max-width: [\d.]+px;$/);
const maxWidthValue = parseFloat(style.match(/[\d.]+/g).join(''));
expect(maxWidthValue).to.be.within(590, 600); // depends on installed fonts: 596.2 on my PC, 597.5 on CI
expect(maxWidthValue).to.be.within(590, 610); // depends on installed fonts: 596.2 on my PC, 597.5 on CI
});
});

Expand All @@ -59,7 +113,7 @@ describe('pie chart', () => {
);
cy.get('svg').should((svg) => {
const width = parseFloat(svg.attr('width'));
expect(width).to.be.within(590, 600); // depends on installed fonts: 596.2 on my PC, 597.5 on CI
expect(width).to.be.within(590, 610); // depends on installed fonts: 596.2 on my PC, 597.5 on CI
expect(svg).to.not.have.attr('style');
});
});
Expand Down
24 changes: 21 additions & 3 deletions packages/mermaid/src/diagrams/pie/pieRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,32 @@ export const draw: DrawDefinition = (text, id, _version, diagObj) => {
return label;
});

const longestTextWidth = Math.max(
const titleTextElement = group.select('.pieTitleText');
const titleTextLeft = (titleTextElement.node() as Element).getBoundingClientRect().left;
const titleTextWidth = (titleTextElement.node() as Element).getBoundingClientRect().width;

// Adjust chart placement.
if (titleTextLeft < MARGIN && titleTextElement.text()) {
group.attr(
'transform',
'translate(' + (pieWidth / 2 - (titleTextLeft - MARGIN)) + ',' + height / 2 + ')'
);
}

const circleOffset = (group.select('circle').node() as Element).getBoundingClientRect().left;

const furthestTextPos = Math.max(
...legend
.selectAll('text')
.nodes()
.map((node) => (node as Element)?.getBoundingClientRect().width ?? 0)
.map((node) => (node as Element)?.getBoundingClientRect().right ?? 0)
);

const totalWidth = pieWidth + MARGIN + LEGEND_RECT_SIZE + LEGEND_SPACING + longestTextWidth;
const totalWidth = Math.max(
pieWidth,
circleOffset + furthestTextPos,
MARGIN * 2 + titleTextWidth
);

// Set viewBox
svg.attr('viewBox', `0 0 ${totalWidth} ${height}`);
Expand Down
Loading