2023-05-20 16:02:20 +02:00
|
|
|
import { log } from '../../logger.js';
|
|
|
|
import mermaidAPI from '../../mermaidAPI.js';
|
|
|
|
import * as configApi from '../../config.js';
|
|
|
|
import { sanitizeText } from '../common/common.js';
|
|
|
|
import {
|
|
|
|
setAccTitle,
|
|
|
|
getAccTitle,
|
|
|
|
setDiagramTitle,
|
|
|
|
getDiagramTitle,
|
|
|
|
getAccDescription,
|
|
|
|
setAccDescription,
|
|
|
|
clear as commonClear,
|
|
|
|
} from '../../commonDb.js';
|
2023-06-08 18:30:02 +02:00
|
|
|
import { XYChartBuilder } from './chartBuilder/index.js';
|
2023-07-05 08:05:57 +02:00
|
|
|
import {
|
|
|
|
DrawableElem,
|
|
|
|
SimplePlotDataType,
|
|
|
|
XYChartData,
|
2023-07-19 19:11:41 +02:00
|
|
|
XYChartThemeConfig,
|
2023-07-05 08:05:57 +02:00
|
|
|
isBandAxisData,
|
|
|
|
isLinearAxisData,
|
|
|
|
} from './chartBuilder/Interfaces.js';
|
2023-07-02 13:44:12 +02:00
|
|
|
import { XYChartConfig } from '../../config.type.js';
|
2023-07-19 19:11:41 +02:00
|
|
|
import { getThemeVariables } from '../../themes/theme-default.js';
|
|
|
|
|
|
|
|
const defaultThemeVariables = getThemeVariables();
|
2023-05-20 16:02:20 +02:00
|
|
|
|
|
|
|
const config = configApi.getConfig();
|
2023-07-19 19:11:41 +02:00
|
|
|
|
|
|
|
function getChartDefaultThemeConfig(): XYChartThemeConfig {
|
|
|
|
return {
|
|
|
|
xychartTitleColor:
|
|
|
|
config.themeVariables?.xychartTitleColor || defaultThemeVariables.xychartTitleColor,
|
|
|
|
xychartAxisLineColor:
|
|
|
|
config.themeVariables?.xychartAxisLineColor || defaultThemeVariables.xychartAxisLineColor,
|
|
|
|
xychartXAxisLableColor:
|
|
|
|
config.themeVariables?.xychartXAxisLableColor || defaultThemeVariables.xychartXAxisLableColor,
|
|
|
|
xychartXAxisTitleColor:
|
|
|
|
config.themeVariables?.xychartXAxisTitleColor || defaultThemeVariables.xychartXAxisTitleColor,
|
|
|
|
xychartXAxisTickColor:
|
|
|
|
config.themeVariables?.xychartXAxisTickColor || defaultThemeVariables.xychartXAxisTickColor,
|
|
|
|
xychartYAxisLableColor:
|
|
|
|
config.themeVariables?.xychartYAxisLableColor || defaultThemeVariables.xychartYAxisLableColor,
|
|
|
|
xychartYAxisTitleColor:
|
|
|
|
config.themeVariables?.xychartYAxisTitleColor || defaultThemeVariables.xychartYAxisTitleColor,
|
|
|
|
xychartYAxisTickColor:
|
|
|
|
config.themeVariables?.xychartYAxisTickColor || defaultThemeVariables.xychartYAxisTickColor,
|
|
|
|
xychartBarPlotPalette:
|
|
|
|
config.themeVariables?.xychartBarPlotPalette || defaultThemeVariables.xychartBarPlotPalette,
|
|
|
|
xychartLinePlotPalette:
|
|
|
|
config.themeVariables?.xychartLinePlotPalette || defaultThemeVariables.xychartLinePlotPalette,
|
|
|
|
};
|
|
|
|
}
|
2023-07-02 13:44:12 +02:00
|
|
|
function getChartDefaultConfig(): XYChartConfig {
|
|
|
|
return config.xyChart
|
|
|
|
? { ...config.xyChart, yAxis: { ...config.xyChart.yAxis }, xAxis: { ...config.xyChart.xAxis } }
|
|
|
|
: {
|
|
|
|
width: 700,
|
|
|
|
height: 500,
|
|
|
|
fontFamily: config.fontFamily || 'Sans',
|
|
|
|
titleFontSize: 16,
|
|
|
|
titlePadding: 5,
|
|
|
|
showtitle: true,
|
|
|
|
plotBorderWidth: 2,
|
|
|
|
yAxis: {
|
|
|
|
showLabel: true,
|
|
|
|
labelFontSize: 14,
|
|
|
|
lablePadding: 5,
|
|
|
|
showTitle: true,
|
|
|
|
titleFontSize: 16,
|
|
|
|
titlePadding: 5,
|
|
|
|
showTick: true,
|
|
|
|
tickLength: 5,
|
|
|
|
tickWidth: 2,
|
|
|
|
},
|
|
|
|
xAxis: {
|
|
|
|
showLabel: true,
|
|
|
|
labelFontSize: 14,
|
|
|
|
lablePadding: 5,
|
|
|
|
showTitle: true,
|
|
|
|
titleFontSize: 16,
|
|
|
|
titlePadding: 5,
|
|
|
|
showTick: true,
|
|
|
|
tickLength: 5,
|
|
|
|
tickWidth: 2,
|
|
|
|
},
|
|
|
|
chartOrientation: 'vertical',
|
|
|
|
plotReservedSpacePercent: 50,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChartDefalutData(): XYChartData {
|
|
|
|
return {
|
|
|
|
yAxis: {
|
2023-07-05 08:05:57 +02:00
|
|
|
type: 'linear',
|
|
|
|
title: '',
|
|
|
|
min: Infinity,
|
|
|
|
max: -Infinity,
|
2023-07-02 13:44:12 +02:00
|
|
|
},
|
|
|
|
xAxis: {
|
2023-07-05 08:05:57 +02:00
|
|
|
type: 'band',
|
|
|
|
title: '',
|
2023-07-02 13:44:12 +02:00
|
|
|
categories: [],
|
|
|
|
},
|
|
|
|
title: '',
|
2023-07-03 18:14:56 +02:00
|
|
|
plots: [],
|
2023-07-02 13:44:12 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let xyChartConfig: XYChartConfig = getChartDefaultConfig();
|
2023-07-19 19:11:41 +02:00
|
|
|
let xyChartThemeConfig: XYChartThemeConfig = getChartDefaultThemeConfig();
|
2023-07-02 13:44:12 +02:00
|
|
|
let xyChartData: XYChartData = getChartDefalutData();
|
2023-07-05 08:05:57 +02:00
|
|
|
let hasSetXAxis = false;
|
|
|
|
let hasSetYAxis = false;
|
2023-05-20 16:02:20 +02:00
|
|
|
|
|
|
|
function textSanitizer(text: string) {
|
|
|
|
return sanitizeText(text.trim(), config);
|
|
|
|
}
|
|
|
|
|
2023-06-08 18:30:02 +02:00
|
|
|
function parseDirective(statement: string, context: string, type: string) {
|
2023-05-20 16:02:20 +02:00
|
|
|
// @ts-ignore: TODO Fix ts errors
|
|
|
|
mermaidAPI.parseDirective(this, statement, context, type);
|
2023-07-02 13:44:12 +02:00
|
|
|
}
|
2023-05-20 16:02:20 +02:00
|
|
|
|
2023-07-02 13:44:12 +02:00
|
|
|
function setOrientation(oriantation: string) {
|
|
|
|
if (oriantation === 'horizontal') {
|
|
|
|
xyChartConfig.chartOrientation = 'horizontal';
|
|
|
|
} else {
|
|
|
|
xyChartConfig.chartOrientation = 'vertical';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function setXAxisTitle(title: string) {
|
|
|
|
xyChartData.xAxis.title = textSanitizer(title);
|
|
|
|
}
|
|
|
|
function setXAxisRangeData(min: number, max: number) {
|
2023-07-05 08:05:57 +02:00
|
|
|
xyChartData.xAxis = { type: 'linear', title: xyChartData.xAxis.title, min, max };
|
|
|
|
hasSetXAxis = true;
|
2023-07-02 13:44:12 +02:00
|
|
|
}
|
|
|
|
function setXAxisBand(categories: string[]) {
|
2023-07-03 18:14:56 +02:00
|
|
|
xyChartData.xAxis = {
|
2023-07-05 08:05:57 +02:00
|
|
|
type: 'band',
|
2023-07-03 18:14:56 +02:00
|
|
|
title: xyChartData.xAxis.title,
|
|
|
|
categories: categories.map((c) => textSanitizer(c)),
|
|
|
|
};
|
2023-07-05 08:05:57 +02:00
|
|
|
hasSetXAxis = true;
|
2023-07-02 13:44:12 +02:00
|
|
|
}
|
|
|
|
function setYAxisTitle(title: string) {
|
|
|
|
xyChartData.yAxis.title = textSanitizer(title);
|
|
|
|
}
|
|
|
|
function setYAxisRangeData(min: number, max: number) {
|
2023-07-05 08:05:57 +02:00
|
|
|
xyChartData.yAxis = { type: 'linear', title: xyChartData.yAxis.title, min, max };
|
|
|
|
hasSetYAxis = true;
|
2023-07-03 18:14:56 +02:00
|
|
|
}
|
2023-07-05 08:05:57 +02:00
|
|
|
|
|
|
|
// 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),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-03 18:14:56 +02:00
|
|
|
if (isBandAxisData(xyChartData.xAxis)) {
|
2023-07-05 08:05:57 +02:00
|
|
|
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]]);
|
2023-07-03 18:14:56 +02:00
|
|
|
}
|
2023-07-05 08:05:57 +02:00
|
|
|
|
|
|
|
return retData;
|
|
|
|
}
|
|
|
|
function setLineData(title: string, data: number[]) {
|
|
|
|
const plotData = transformDataWithOutCategory(data);
|
|
|
|
xyChartData.plots.push({
|
|
|
|
type: 'line',
|
2023-07-19 19:11:41 +02:00
|
|
|
strokeFill:
|
|
|
|
xyChartThemeConfig.xychartLinePlotPalette[
|
|
|
|
Math.floor(Math.random() * (xyChartThemeConfig.xychartLinePlotPalette.length - 1))
|
|
|
|
],
|
2023-07-05 08:05:57 +02:00
|
|
|
strokeWidth: 2,
|
|
|
|
data: plotData,
|
|
|
|
});
|
2023-07-03 18:14:56 +02:00
|
|
|
}
|
|
|
|
function setBarData(title: string, data: number[]) {
|
2023-07-05 08:05:57 +02:00
|
|
|
const plotData = transformDataWithOutCategory(data);
|
|
|
|
xyChartData.plots.push({
|
|
|
|
type: 'bar',
|
2023-07-19 19:11:41 +02:00
|
|
|
fill: xyChartThemeConfig.xychartBarPlotPalette[
|
|
|
|
Math.floor(Math.random() * (xyChartThemeConfig.xychartBarPlotPalette.length - 1))
|
|
|
|
],
|
2023-07-05 08:05:57 +02:00
|
|
|
data: plotData,
|
|
|
|
});
|
2023-07-02 13:44:12 +02:00
|
|
|
}
|
2023-06-08 18:30:02 +02:00
|
|
|
|
|
|
|
function getDrawableElem(): DrawableElem[] {
|
2023-07-05 08:05:57 +02:00
|
|
|
if (xyChartData.plots.length === 0) {
|
|
|
|
throw Error('No Plot to render, please provide a plot with some data');
|
|
|
|
}
|
2023-07-02 13:44:12 +02:00
|
|
|
xyChartData.title = getDiagramTitle();
|
2023-07-19 19:11:41 +02:00
|
|
|
return XYChartBuilder.build(xyChartConfig, xyChartData, xyChartThemeConfig);
|
2023-06-08 18:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setHeight(height: number) {
|
2023-07-02 13:44:12 +02:00
|
|
|
xyChartConfig.height = height;
|
2023-06-08 18:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setWidth(width: number) {
|
2023-07-02 13:44:12 +02:00
|
|
|
xyChartConfig.width = width;
|
2023-06-08 18:30:02 +02:00
|
|
|
}
|
|
|
|
|
2023-05-20 16:02:20 +02:00
|
|
|
const clear = function () {
|
|
|
|
commonClear();
|
2023-07-02 13:44:12 +02:00
|
|
|
xyChartConfig = getChartDefaultConfig();
|
|
|
|
xyChartData = getChartDefalutData();
|
2023-07-19 19:11:41 +02:00
|
|
|
xyChartThemeConfig = getChartDefaultThemeConfig();
|
2023-07-05 08:05:57 +02:00
|
|
|
hasSetXAxis = false;
|
|
|
|
hasSetYAxis = false;
|
2023-05-20 16:02:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
2023-06-08 18:30:02 +02:00
|
|
|
setWidth,
|
|
|
|
setHeight,
|
|
|
|
getDrawableElem,
|
2023-05-20 16:02:20 +02:00
|
|
|
parseDirective,
|
|
|
|
clear,
|
|
|
|
setAccTitle,
|
|
|
|
getAccTitle,
|
|
|
|
setDiagramTitle,
|
|
|
|
getDiagramTitle,
|
|
|
|
getAccDescription,
|
|
|
|
setAccDescription,
|
2023-07-02 13:44:12 +02:00
|
|
|
setOrientation,
|
|
|
|
setXAxisTitle,
|
|
|
|
setXAxisRangeData,
|
|
|
|
setXAxisBand,
|
|
|
|
setYAxisTitle,
|
|
|
|
setYAxisRangeData,
|
|
|
|
setLineData,
|
|
|
|
setBarData,
|
2023-05-20 16:02:20 +02:00
|
|
|
};
|