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

fix: Adjust piechart viewbox for mobile devices with small width #4288

Merged
merged 12 commits into from
Nov 28, 2023
12 changes: 12 additions & 0 deletions packages/mermaid/src/diagrams/pie/pieRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { log } from '../../logger.js';
import { configureSvgSize } from '../../setupGraphViewbox.js';
import * as configApi from '../../config.js';
import { parseFontSize } from '../../utils.js';
import { getTextWidth } from '../../rendering-util/getTextWidth.js';

let conf = configApi.getConfig();

Expand Down Expand Up @@ -70,6 +71,17 @@ export const draw = (txt, id, _version, diagObj) => {
sum += data[key];
});

const legendShowData = diagObj.db.getShowData() || conf.showData || conf.pie.showData || false;
iwestlin marked this conversation as resolved.
Show resolved Hide resolved
const legendTexts = Object.keys(data).map(key => {
if (!legendShowData) {
return key;
}
return `${key} [${data[key]}]`;
})
const longestTextWidth = Math.max(...(legendTexts.map(v => getTextWidth(v))));
const newWidth = width + margin + legendRectSize + legendSpacing + longestTextWidth;
elem.setAttribute("viewBox", "0 0 " + newWidth + " " + height);
iwestlin marked this conversation as resolved.
Show resolved Hide resolved

const themeVariables = conf.themeVariables;
var myGeneratedColors = [
themeVariables.pie1,
Expand Down
7 changes: 7 additions & 0 deletions packages/mermaid/src/rendering-util/getTextWidth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// https://stackoverflow.com/a/35373030/3469145
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');

const getTextWidth = (text) => context.measureText(text).width * window.devicePixelRatio;

export { getTextWidth };
sidharthv96 marked this conversation as resolved.
Show resolved Hide resolved