Skip to content

Commit

Permalink
fix: adjust piechart viewbox for mobile devices with small width
Browse files Browse the repository at this point in the history
  • Loading branch information
iwestlin committed Apr 10, 2023
1 parent f08778d commit 534dedc
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/mermaid/src/diagrams/pie/pieRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import * as configApi from '../../config';
import { parseFontSize } from '../../utils';

let conf = configApi.getConfig();
// https://stackoverflow.com/a/35373030/3469145
const getTextWidth = (function () {
const canvas = document.createElement('canvas')
const context = canvas.getContext('2d')
return text => context.measureText(text).width * window.devicePixelRatio
})();

/**
* Draws a Pie Chart with the data given in text.
Expand Down Expand Up @@ -73,6 +79,18 @@ export const draw = (txt, id, _version, diagObj) => {
sum += data[key];
});

const legendShowData = diagObj.db.getShowData() || conf.showData || conf.pie.showData;
const legendTexts = Object.keys(data).map(key => {
if (!legendShowData) {
return key;
}
return key + ' [' + data[key] + ']';
})
const legendTextWidths = legendTexts.map(v => getTextWidth(v)).sort((a, b) => a - b);
const longestTextWidth = parseInt(legendTextWidths.pop());
const newWidth = width + margin + legendRectSize + legendSpacing + longestTextWidth;
elem.setAttribute("viewBox", "0 0 " + newWidth + " " + height);

const themeVariables = conf.themeVariables;
var myGeneratedColors = [
themeVariables.pie1,
Expand Down

0 comments on commit 534dedc

Please sign in to comment.