mermaid/packages/mermaid/src/diagrams/xychart/xychartDb.ts

230 lines
6.4 KiB
TypeScript
Raw Normal View History

2023-05-20 16:02:20 +02:00
import {
2023-09-07 09:15:22 +02:00
clear as commonClear,
getAccDescription,
2023-05-20 16:02:20 +02:00
getAccTitle,
getDiagramTitle,
setAccDescription,
2023-09-07 09:15:22 +02:00
setAccTitle,
setDiagramTitle,
2023-09-07 09:46:46 +02:00
} from '../common/commonDb.js';
2023-09-07 09:15:22 +02:00
import * as configApi from '../../config.js';
import defaultConfig from '../../defaultConfig.js';
import { getThemeVariables } from '../../themes/theme-default.js';
import { cleanAndMerge } from '../../utils.js';
import { sanitizeText } from '../common/common.js';
2023-06-08 18:30:02 +02:00
import { XYChartBuilder } from './chartBuilder/index.js';
2023-09-02 13:03:29 +02:00
import type {
DrawableElem,
SimplePlotDataType,
2023-09-07 09:15:22 +02:00
XYChartConfig,
XYChartData,
XYChartThemeConfig,
2023-09-07 09:15:22 +02:00
} from './chartBuilder/interfaces.js';
import { isBandAxisData, isLinearAxisData } from './chartBuilder/interfaces.js';
2023-09-29 15:29:05 +02:00
import type { Group } from '../../diagram-api/types.js';
2023-08-20 14:21:53 +02:00
let plotIndex = 0;
2023-09-29 15:29:05 +02:00
let tmpSVGGroup: Group;
2023-08-20 14:21:53 +02:00
let xyChartConfig: XYChartConfig = getChartDefaultConfig();
let xyChartThemeConfig: XYChartThemeConfig = getChartDefaultThemeConfig();
2023-09-19 17:39:31 +02:00
let xyChartData: XYChartData = getChartDefaultData();
2023-09-03 17:18:10 +02:00
let plotColorPalette = xyChartThemeConfig.plotColorPalette.split(',').map((color) => color.trim());
2023-08-20 14:21:53 +02:00
let hasSetXAxis = false;
let hasSetYAxis = false;
interface NormalTextType {
type: 'text';
text: string;
}
function getChartDefaultThemeConfig(): XYChartThemeConfig {
2023-09-02 17:45:21 +02:00
const defaultThemeVariables = getThemeVariables();
const config = configApi.getConfig();
2023-09-07 09:15:22 +02:00
return cleanAndMerge(defaultThemeVariables.xyChart, config.themeVariables.xyChart);
}
function getChartDefaultConfig(): XYChartConfig {
2023-09-02 17:45:21 +02:00
const config = configApi.getConfig();
2023-09-07 09:15:22 +02:00
return cleanAndMerge<XYChartConfig>(
defaultConfig.xyChart as XYChartConfig,
config.xyChart as XYChartConfig
);
}
2023-09-19 17:39:31 +02:00
function getChartDefaultData(): XYChartData {
return {
yAxis: {
type: 'linear',
title: '',
min: Infinity,
max: -Infinity,
},
xAxis: {
type: 'band',
title: '',
categories: [],
},
title: '',
plots: [],
};
}
2023-05-20 16:02:20 +02:00
function textSanitizer(text: string) {
2023-09-02 17:45:21 +02:00
const config = configApi.getConfig();
2023-05-20 16:02:20 +02:00
return sanitizeText(text.trim(), config);
}
2023-09-29 15:29:05 +02:00
function setTmpSVGG(SVGG: Group) {
tmpSVGGroup = SVGG;
2023-08-20 14:21:53 +02:00
}
function setOrientation(orientation: string) {
if (orientation === 'horizontal') {
xyChartConfig.chartOrientation = 'horizontal';
} else {
xyChartConfig.chartOrientation = 'vertical';
}
}
2023-08-06 12:15:01 +02:00
function setXAxisTitle(title: NormalTextType) {
xyChartData.xAxis.title = textSanitizer(title.text);
}
function setXAxisRangeData(min: number, max: number) {
xyChartData.xAxis = { type: 'linear', title: xyChartData.xAxis.title, min, max };
hasSetXAxis = true;
}
2023-08-06 12:15:01 +02:00
function setXAxisBand(categories: NormalTextType[]) {
xyChartData.xAxis = {
type: 'band',
title: xyChartData.xAxis.title,
2023-08-06 12:15:01 +02:00
categories: categories.map((c) => textSanitizer(c.text)),
};
hasSetXAxis = true;
}
2023-08-06 12:15:01 +02:00
function setYAxisTitle(title: NormalTextType) {
xyChartData.yAxis.title = textSanitizer(title.text);
}
function setYAxisRangeData(min: number, max: number) {
xyChartData.yAxis = { type: 'linear', title: xyChartData.yAxis.title, min, max };
hasSetYAxis = true;
}
// this function does not set `hasSetYAxis` as there can be multiple data so we should calculate the range accordingly
function setYAxisRangeFromPlotData(data: number[]) {
const minValue = Math.min(...data);
const maxValue = Math.max(...data);
const prevMinValue = isLinearAxisData(xyChartData.yAxis) ? xyChartData.yAxis.min : Infinity;
const prevMaxValue = isLinearAxisData(xyChartData.yAxis) ? xyChartData.yAxis.max : -Infinity;
xyChartData.yAxis = {
type: 'linear',
title: xyChartData.yAxis.title,
min: Math.min(prevMinValue, minValue),
max: Math.max(prevMaxValue, maxValue),
};
}
2023-08-20 14:21:53 +02:00
function transformDataWithoutCategory(data: number[]): SimplePlotDataType {
let retData: SimplePlotDataType = [];
if (data.length === 0) {
return retData;
}
if (!hasSetXAxis) {
const prevMinValue = isLinearAxisData(xyChartData.xAxis) ? xyChartData.xAxis.min : Infinity;
const prevMaxValue = isLinearAxisData(xyChartData.xAxis) ? xyChartData.xAxis.max : -Infinity;
setXAxisRangeData(Math.min(prevMinValue, 1), Math.max(prevMaxValue, data.length));
}
if (!hasSetYAxis) {
setYAxisRangeFromPlotData(data);
}
if (isBandAxisData(xyChartData.xAxis)) {
retData = xyChartData.xAxis.categories.map((c, i) => [c, data[i]]);
}
if (isLinearAxisData(xyChartData.xAxis)) {
const min = xyChartData.xAxis.min;
const max = xyChartData.xAxis.max;
const step = (max - min + 1) / data.length;
const categories: string[] = [];
for (let i = min; i <= max; i += step) {
categories.push(`${i}`);
}
retData = categories.map((c, i) => [c, data[i]]);
}
return retData;
}
2023-07-21 19:12:46 +02:00
function getPlotColorFromPalette(plotIndex: number): string {
2023-09-03 17:18:10 +02:00
return plotColorPalette[plotIndex === 0 ? 0 : plotIndex % plotColorPalette.length];
2023-07-21 19:12:46 +02:00
}
2023-08-06 12:15:01 +02:00
function setLineData(title: NormalTextType, data: number[]) {
2023-08-20 14:21:53 +02:00
const plotData = transformDataWithoutCategory(data);
xyChartData.plots.push({
type: 'line',
2023-07-21 19:12:46 +02:00
strokeFill: getPlotColorFromPalette(plotIndex),
strokeWidth: 2,
data: plotData,
});
2023-07-21 19:12:46 +02:00
plotIndex++;
}
2023-07-21 19:12:46 +02:00
2023-08-06 12:15:01 +02:00
function setBarData(title: NormalTextType, data: number[]) {
2023-08-20 14:21:53 +02:00
const plotData = transformDataWithoutCategory(data);
xyChartData.plots.push({
type: 'bar',
2023-07-21 19:12:46 +02:00
fill: getPlotColorFromPalette(plotIndex),
data: plotData,
});
2023-07-21 19:12:46 +02:00
plotIndex++;
}
2023-06-08 18:30:02 +02:00
function getDrawableElem(): DrawableElem[] {
if (xyChartData.plots.length === 0) {
throw Error('No Plot to render, please provide a plot with some data');
}
xyChartData.title = getDiagramTitle();
2023-09-29 15:29:05 +02:00
return XYChartBuilder.build(xyChartConfig, xyChartData, xyChartThemeConfig, tmpSVGGroup);
2023-06-08 18:30:02 +02:00
}
2023-09-02 09:33:30 +02:00
function getChartThemeConfig() {
return xyChartThemeConfig;
2023-06-08 18:30:02 +02:00
}
2023-09-02 09:33:30 +02:00
function getChartConfig() {
return xyChartConfig;
2023-06-08 18:30:02 +02:00
}
2023-05-20 16:02:20 +02:00
const clear = function () {
commonClear();
2023-07-21 19:12:46 +02:00
plotIndex = 0;
xyChartConfig = getChartDefaultConfig();
2023-09-19 17:39:31 +02:00
xyChartData = getChartDefaultData();
xyChartThemeConfig = getChartDefaultThemeConfig();
2023-09-03 17:18:10 +02:00
plotColorPalette = xyChartThemeConfig.plotColorPalette.split(',').map((color) => color.trim());
hasSetXAxis = false;
hasSetYAxis = false;
2023-05-20 16:02:20 +02:00
};
export default {
2023-06-08 18:30:02 +02:00
getDrawableElem,
2023-05-20 16:02:20 +02:00
clear,
setAccTitle,
getAccTitle,
setDiagramTitle,
getDiagramTitle,
getAccDescription,
setAccDescription,
setOrientation,
setXAxisTitle,
setXAxisRangeData,
setXAxisBand,
setYAxisTitle,
setYAxisRangeData,
setLineData,
setBarData,
2023-08-20 14:21:53 +02:00
setTmpSVGG,
2023-09-02 09:33:30 +02:00
getChartThemeConfig,
getChartConfig,
2023-05-20 16:02:20 +02:00
};