diff --git a/src/diagrams/class/classRenderer.js b/src/diagrams/class/classRenderer.js index 3943b889b..b59788740 100644 --- a/src/diagrams/class/classRenderer.js +++ b/src/diagrams/class/classRenderer.js @@ -9,6 +9,7 @@ import svgDraw from './svgDraw'; parser.yy = classDb; let idCache = {}; +const padding = 20; const conf = { dividerMargin: 10, @@ -225,9 +226,22 @@ export const draw = function(text, id) { } }); - diagram.attr('height', g.graph().height + 40); - diagram.attr('width', g.graph().width * 1.5 + 20); - diagram.attr('viewBox', '-10 -10 ' + (g.graph().width + 20) + ' ' + (g.graph().height + 20)); + const svgBounds = diagram.node().getBBox(); + const width = svgBounds.width + padding * 2; + const height = svgBounds.height + padding * 2; + + if (conf.useMaxWidth) { + diagram.attr('width', '100%'); + diagram.attr('style', `max-width: ${width}px;`); + } else { + diagram.attr('height', height); + diagram.attr('width', width); + } + + // Ensure the viewBox includes the whole svgBounds area with extra space for padding + const vBox = `${svgBounds.x - padding} ${svgBounds.y - padding} ${width} ${height}`; + logger.debug(`viewBox ${vBox}`); + diagram.attr('viewBox', vBox); }; export default {