mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-21 06:53:17 +08:00
rename PieDb interface and more types with null sefety
This commit is contained in:
parent
ce9d0e2e6a
commit
09c4a26509
@ -12,7 +12,7 @@ import {
|
|||||||
clear as commonClear,
|
clear as commonClear,
|
||||||
} from '../../commonDb.js';
|
} from '../../commonDb.js';
|
||||||
import type { ParseDirectiveDefinition } from '../../diagram-api/types.js';
|
import type { ParseDirectiveDefinition } from '../../diagram-api/types.js';
|
||||||
import type { PieFields, PieDb, Sections, PieDiagramConfig } from './pieTypes.js';
|
import type { PieFields, PieDB, Sections, PieDiagramConfig } from './pieTypes.js';
|
||||||
import type { RequiredDeep } from 'type-fest';
|
import type { RequiredDeep } from 'type-fest';
|
||||||
|
|
||||||
export const DEFAULT_PIE_CONFIG: Required<PieDiagramConfig> = {
|
export const DEFAULT_PIE_CONFIG: Required<PieDiagramConfig> = {
|
||||||
@ -84,7 +84,7 @@ const setShowData = (toggle: boolean): void => {
|
|||||||
|
|
||||||
const getShowData = (): boolean => showData;
|
const getShowData = (): boolean => showData;
|
||||||
|
|
||||||
export const db: PieDb = {
|
export const db: PieDB = {
|
||||||
setConfig,
|
setConfig,
|
||||||
getConfig,
|
getConfig,
|
||||||
reset,
|
reset,
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
// @ts-nocheck - placeholder to be handled
|
// @ts-nocheck - placeholder to be handled
|
||||||
import { select, scaleOrdinal, pie as d3pie, arc } from 'd3';
|
import d3, { 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 { getConfig } from '../../config.js';
|
import { getConfig } from '../../config.js';
|
||||||
import { parseFontSize } from '../../utils.js';
|
import { parseFontSize } from '../../utils.js';
|
||||||
import { DrawDefinition, HTML } from '../../diagram-api/types.js';
|
import type { DrawDefinition, HTML } from '../../diagram-api/types.js';
|
||||||
import type { D3Sections, PieDb, PieDiagramConfig, Sections } from './pieTypes.js';
|
import type { D3Sections, PieDB, PieDiagramConfig, Sections } from './pieTypes.js';
|
||||||
import { MermaidConfig } from '../../config.type.js';
|
import { MermaidConfig } from '../../config.type.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,12 +17,12 @@ import { MermaidConfig } from '../../config.type.js';
|
|||||||
export const draw: DrawDefinition = (txt, id, _version, diagramObject) => {
|
export const draw: DrawDefinition = (txt, id, _version, diagramObject) => {
|
||||||
try {
|
try {
|
||||||
log.debug('rendering pie chart\n' + txt);
|
log.debug('rendering pie chart\n' + txt);
|
||||||
const db = diagramObject.db as PieDb;
|
const db: PieDB = diagramObject.db as PieDB;
|
||||||
db.clear();
|
db.clear();
|
||||||
const globalConfig: MermaidConfig = getConfig();
|
const globalConfig: MermaidConfig = getConfig();
|
||||||
const config: Required<PieDiagramConfig> = db.getConfig();
|
const config: Required<PieDiagramConfig> = db.getConfig();
|
||||||
|
|
||||||
const height = 450;
|
const height: number = 450;
|
||||||
const { securityLevel } = globalConfig;
|
const { securityLevel } = globalConfig;
|
||||||
// handle root and document for when rendering in sandbox mode
|
// handle root and document for when rendering in sandbox mode
|
||||||
let sandboxElement: HTML | undefined;
|
let sandboxElement: HTML | undefined;
|
||||||
@ -31,9 +31,9 @@ export const draw: DrawDefinition = (txt, id, _version, diagramObject) => {
|
|||||||
}
|
}
|
||||||
const root =
|
const root =
|
||||||
securityLevel === 'sandbox'
|
securityLevel === 'sandbox'
|
||||||
? select(sandboxElement.nodes()[0].contentDocument.body)
|
? select(sandboxElement?.node()?.contentDocument?.body as HTMLIFrameElement)
|
||||||
: select('body');
|
: select('body');
|
||||||
const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document;
|
const doc = securityLevel === 'sandbox' ? sandboxElement?.nodes()[0].contentDocument : document;
|
||||||
const elem = doc?.getElementById(id);
|
const elem = doc?.getElementById(id);
|
||||||
const width: number = elem?.parentElement?.offsetWidth ?? config.useWidth;
|
const width: number = elem?.parentElement?.offsetWidth ?? config.useWidth;
|
||||||
|
|
||||||
@ -48,18 +48,18 @@ export const draw: DrawDefinition = (txt, id, _version, diagramObject) => {
|
|||||||
// Set viewBox
|
// Set viewBox
|
||||||
elem?.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
|
elem?.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
|
||||||
|
|
||||||
const margin = 40;
|
const margin: number = 40;
|
||||||
const legendRectSize = 18;
|
const legendRectSize: number = 18;
|
||||||
const legendSpacing = 4;
|
const legendSpacing: number = 4;
|
||||||
|
|
||||||
const radius = Math.min(width, height) / 2 - margin;
|
const radius: number = Math.min(width, height) / 2 - margin;
|
||||||
|
|
||||||
const svg = diagram
|
const svg = diagram
|
||||||
.append('g')
|
.append('g')
|
||||||
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
|
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')');
|
||||||
|
|
||||||
const sections: Sections = db.getSections();
|
const sections: Sections = db.getSections();
|
||||||
let sum = 0;
|
let sum: number = 0;
|
||||||
Object.keys(sections).forEach((key: string): void => {
|
Object.keys(sections).forEach((key: string): void => {
|
||||||
sum += sections[key];
|
sum += sections[key];
|
||||||
});
|
});
|
||||||
@ -80,12 +80,12 @@ export const draw: DrawDefinition = (txt, id, _version, diagramObject) => {
|
|||||||
themeVariables.pie12,
|
themeVariables.pie12,
|
||||||
];
|
];
|
||||||
|
|
||||||
const textPosition = config.textPosition;
|
const textPosition: number = config.textPosition;
|
||||||
let [outerStrokeWidth] = parseFontSize(themeVariables.pieOuterStrokeWidth);
|
let [outerStrokeWidth] = parseFontSize(themeVariables.pieOuterStrokeWidth);
|
||||||
outerStrokeWidth ??= 2;
|
outerStrokeWidth ??= 2;
|
||||||
|
|
||||||
// Set the color scale
|
// Set the color scale
|
||||||
const color = scaleOrdinal().range(myGeneratedColors);
|
const color: d3.ScaleOrdinal<string, unknown, never> = scaleOrdinal().range(myGeneratedColors);
|
||||||
|
|
||||||
// Compute the position of each group on the pie:
|
// Compute the position of each group on the pie:
|
||||||
const pieData: D3Sections[] = Object.entries(sections)
|
const pieData: D3Sections[] = Object.entries(sections)
|
||||||
|
@ -46,7 +46,7 @@ export interface D3Sections {
|
|||||||
value: number;
|
value: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PieDb extends DiagramDB {
|
export interface PieDB extends DiagramDB {
|
||||||
// config
|
// config
|
||||||
reset: () => void;
|
reset: () => void;
|
||||||
setConfig: (config: PieDiagramConfig) => void;
|
setConfig: (config: PieDiagramConfig) => void;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user