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 88ad9cd
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 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 @@ -72,6 +78,14 @@ export const draw = (txt, id, _version, diagObj) => {
Object.keys(data).forEach(function (key) {
sum += data[key];
});
const legendShowData = diagObj.db.getShowData() || conf.showData || conf.pie.showData;
const longestLegendText = Object.keys(data).map(key => {
if (!legendShowData) return key;

Check failure on line 83 in packages/mermaid/src/diagrams/pie/pieRenderer.js

View workflow job for this annotation

GitHub Actions / lint (18.x)

Expected { after 'if' condition
return key + ' [' + data[key] + ']';
}).sort((a, b) => a.length - b.length).pop();
const maxLegendTextWidth = getTextWidth(longestLegendText);
const newWidth = width + margin + legendRectSize + legendSpacing + maxLegendTextWidth;
elem.setAttribute("viewBox", "0 0 " + newWidth + " " + height);

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

0 comments on commit 88ad9cd

Please sign in to comment.