mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-25 10:20:06 +02:00
fix: adjust piechart viewbox for mobile devices with small width
This commit is contained in:
@@ -6,6 +6,12 @@ import * as configApi from '../../config.js';
|
||||
import { parseFontSize } from '../../utils.js';
|
||||
|
||||
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.
|
||||
@@ -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,
|
||||
|
Reference in New Issue
Block a user