mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-21 06:53:17 +08:00
clean up pieRenderer.ts
This commit is contained in:
parent
f2338f5b66
commit
8794fa0b38
@ -2,10 +2,10 @@
|
|||||||
import { select, scaleOrdinal, pie as d3pie, arc } from 'd3';
|
import { select, scaleOrdinal, pie as d3pie, arc } from 'd3';
|
||||||
import { log } from '../../logger.js';
|
import { log } from '../../logger.js';
|
||||||
import { configureSvgSize } from '../../setupGraphViewbox.js';
|
import { configureSvgSize } from '../../setupGraphViewbox.js';
|
||||||
import * as configApi from '../../config.js';
|
import { getConfig } from '../../config.js';
|
||||||
import { parseFontSize } from '../../utils.js';
|
import { parseFontSize } from '../../utils.js';
|
||||||
|
import { DrawDefinition } from '../../diagram-api/types.js';
|
||||||
let conf = configApi.getConfig();
|
import { PieDb, Sections } from './pieTypes.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draws a Pie Chart with the data given in text.
|
* Draws a Pie Chart with the data given in text.
|
||||||
@ -13,15 +13,15 @@ let conf = configApi.getConfig();
|
|||||||
* @param text - pie chart code
|
* @param text - pie chart code
|
||||||
* @param id - diagram id
|
* @param id - diagram id
|
||||||
*/
|
*/
|
||||||
let width;
|
export const draw: DrawDefinition = (txt, id, _version, diagramObject) => {
|
||||||
const height = 450;
|
|
||||||
export const draw = (txt, id, _version, diagObj) => {
|
|
||||||
try {
|
try {
|
||||||
conf = configApi.getConfig();
|
log.debug('rendering pie chart\n' + txt);
|
||||||
log.debug('Rendering info diagram\n' + txt);
|
|
||||||
|
|
||||||
const securityLevel = configApi.getConfig().securityLevel;
|
let width: number;
|
||||||
// Handle root and Document for when rendering in sandbox mode
|
const height = 450;
|
||||||
|
const config = getConfig();
|
||||||
|
const { securityLevel } = config;
|
||||||
|
// handle root and document for when rendering in sandbox mode
|
||||||
let sandboxElement;
|
let sandboxElement;
|
||||||
if (securityLevel === 'sandbox') {
|
if (securityLevel === 'sandbox') {
|
||||||
sandboxElement = select('#i' + id);
|
sandboxElement = select('#i' + id);
|
||||||
@ -31,32 +31,29 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
? select(sandboxElement.nodes()[0].contentDocument.body)
|
? select(sandboxElement.nodes()[0].contentDocument.body)
|
||||||
: select('body');
|
: select('body');
|
||||||
const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document;
|
const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document;
|
||||||
|
|
||||||
// Parse the Pie Chart definition
|
|
||||||
diagObj.db.clear();
|
|
||||||
diagObj.parser.parse(txt);
|
|
||||||
log.debug('Parsed info diagram');
|
|
||||||
const elem = doc.getElementById(id);
|
const elem = doc.getElementById(id);
|
||||||
width = elem.parentElement.offsetWidth;
|
width = elem.parentElement.offsetWidth;
|
||||||
|
|
||||||
|
// Parse the Pie Chart definition
|
||||||
|
const db = diagramObject.db as PieDb;
|
||||||
|
db.clear();
|
||||||
|
|
||||||
|
log.debug('parsing pie chart');
|
||||||
|
diagramObject.parser.parse(txt);
|
||||||
|
|
||||||
if (width === undefined) {
|
if (width === undefined) {
|
||||||
width = 1200;
|
width = 1200;
|
||||||
}
|
}
|
||||||
|
if (config.pie?.useWidth !== undefined) {
|
||||||
if (conf.useWidth !== undefined) {
|
width = config.pie.useWidth;
|
||||||
width = conf.useWidth;
|
|
||||||
}
|
|
||||||
if (conf.pie.useWidth !== undefined) {
|
|
||||||
width = conf.pie.useWidth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const diagram = root.select('#' + id);
|
const diagram = root.select('#' + id);
|
||||||
configureSvgSize(diagram, height, width, conf.pie.useMaxWidth);
|
configureSvgSize(diagram, height, width, config.pie?.useMaxWidth ?? true);
|
||||||
|
|
||||||
// Set viewBox
|
// Set viewBox
|
||||||
elem.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
|
elem.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
|
||||||
|
|
||||||
// Fetch the default direction, use TD if none was found
|
|
||||||
const margin = 40;
|
const margin = 40;
|
||||||
const legendRectSize = 18;
|
const legendRectSize = 18;
|
||||||
const legendSpacing = 4;
|
const legendSpacing = 4;
|
||||||
@ -67,13 +64,13 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
.append('g')
|
.append('g')
|
||||||
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
|
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
|
||||||
|
|
||||||
const data = diagObj.db.getSections();
|
const sections: Sections = db.getSections();
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
Object.keys(data).forEach(function (key) {
|
Object.keys(sections).forEach((key: string): void => {
|
||||||
sum += data[key];
|
sum += sections[key];
|
||||||
});
|
});
|
||||||
|
|
||||||
const themeVariables = conf.themeVariables;
|
const themeVariables = config.themeVariables;
|
||||||
const myGeneratedColors = [
|
const myGeneratedColors = [
|
||||||
themeVariables.pie1,
|
themeVariables.pie1,
|
||||||
themeVariables.pie2,
|
themeVariables.pie2,
|
||||||
@ -89,7 +86,7 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
themeVariables.pie12,
|
themeVariables.pie12,
|
||||||
];
|
];
|
||||||
|
|
||||||
const textPosition = conf.pie?.textPosition ?? 0.75;
|
const textPosition = config.pie?.textPosition ?? 0.75;
|
||||||
let [outerStrokeWidth] = parseFontSize(themeVariables.pieOuterStrokeWidth);
|
let [outerStrokeWidth] = parseFontSize(themeVariables.pieOuterStrokeWidth);
|
||||||
outerStrokeWidth ??= 2;
|
outerStrokeWidth ??= 2;
|
||||||
|
|
||||||
@ -97,7 +94,7 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
const color = scaleOrdinal().range(myGeneratedColors);
|
const color = scaleOrdinal().range(myGeneratedColors);
|
||||||
|
|
||||||
// Compute the position of each group on the pie:
|
// Compute the position of each group on the pie:
|
||||||
const pieData = Object.entries(data).map(function (el, idx) {
|
const pieData = Object.entries(sections).map(function (el, idx) {
|
||||||
return {
|
return {
|
||||||
order: idx,
|
order: idx,
|
||||||
name: el[0],
|
name: el[0],
|
||||||
@ -105,10 +102,10 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
const pie = d3pie()
|
const pie = d3pie()
|
||||||
.value(function (d) {
|
.value((d): number => {
|
||||||
return d.value;
|
return d.value;
|
||||||
})
|
})
|
||||||
.sort(function (a, b) {
|
.sort((a, b): number => {
|
||||||
// Sort slices in clockwise direction
|
// Sort slices in clockwise direction
|
||||||
return a.order - b.order;
|
return a.order - b.order;
|
||||||
});
|
});
|
||||||
@ -134,7 +131,7 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
.enter()
|
.enter()
|
||||||
.append('path')
|
.append('path')
|
||||||
.attr('d', arcGenerator)
|
.attr('d', arcGenerator)
|
||||||
.attr('fill', function (d) {
|
.attr('fill', (d) => {
|
||||||
return color(d.data.name);
|
return color(d.data.name);
|
||||||
})
|
})
|
||||||
.attr('class', 'pieCircle');
|
.attr('class', 'pieCircle');
|
||||||
@ -146,10 +143,10 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
.data(dataReady)
|
.data(dataReady)
|
||||||
.enter()
|
.enter()
|
||||||
.append('text')
|
.append('text')
|
||||||
.text(function (d) {
|
.text((d): string => {
|
||||||
return ((d.data.value / sum) * 100).toFixed(0) + '%';
|
return ((d.data.value / sum) * 100).toFixed(0) + '%';
|
||||||
})
|
})
|
||||||
.attr('transform', function (d) {
|
.attr('transform', (d): string => {
|
||||||
return 'translate(' + labelArcGenerator.centroid(d) + ')';
|
return 'translate(' + labelArcGenerator.centroid(d) + ')';
|
||||||
})
|
})
|
||||||
.style('text-anchor', 'middle')
|
.style('text-anchor', 'middle')
|
||||||
@ -157,7 +154,7 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
|
|
||||||
svg
|
svg
|
||||||
.append('text')
|
.append('text')
|
||||||
.text(diagObj.db.getDiagramTitle())
|
.text(db.getDiagramTitle())
|
||||||
.attr('x', 0)
|
.attr('x', 0)
|
||||||
.attr('y', -(height - 50) / 2)
|
.attr('y', -(height - 50) / 2)
|
||||||
.attr('class', 'pieTitleText');
|
.attr('class', 'pieTitleText');
|
||||||
@ -169,11 +166,11 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
.enter()
|
.enter()
|
||||||
.append('g')
|
.append('g')
|
||||||
.attr('class', 'legend')
|
.attr('class', 'legend')
|
||||||
.attr('transform', function (d, i) {
|
.attr('transform', (d, index: number): string => {
|
||||||
const height = legendRectSize + legendSpacing;
|
const height = legendRectSize + legendSpacing;
|
||||||
const offset = (height * color.domain().length) / 2;
|
const offset = (height * color.domain().length) / 2;
|
||||||
const horizontal = 12 * legendRectSize;
|
const horizontal = 12 * legendRectSize;
|
||||||
const vertical = i * height - offset;
|
const vertical = index * height - offset;
|
||||||
return 'translate(' + horizontal + ',' + vertical + ')';
|
return 'translate(' + horizontal + ',' + vertical + ')';
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -189,16 +186,15 @@ export const draw = (txt, id, _version, diagObj) => {
|
|||||||
.append('text')
|
.append('text')
|
||||||
.attr('x', legendRectSize + legendSpacing)
|
.attr('x', legendRectSize + legendSpacing)
|
||||||
.attr('y', legendRectSize - legendSpacing)
|
.attr('y', legendRectSize - legendSpacing)
|
||||||
.text(function (d) {
|
.text((d): string => {
|
||||||
if (diagObj.db.getShowData() || conf.showData || conf.pie.showData) {
|
if (db.getShowData()) {
|
||||||
return d.data.name + ' [' + d.data.value + ']';
|
return d.data.name + ' [' + d.data.value + ']';
|
||||||
} else {
|
} else {
|
||||||
return d.data.name;
|
return d.data.name;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
log.error('Error while rendering info diagram');
|
log.error('error while rendering pie chart\n', e);
|
||||||
log.error(e);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user