@visactor/vmind
Version:
<div align="center"> <a href="https://github.com/VisActor#gh-light-mode-only" target="_blank"> <img alt="VisActor Logo" width="200" src="https://github.com/VisActor/.github/blob/main/profile/logo_500_200_light.svg"/> </a> <a href="https://githu
320 lines (299 loc) • 18.3 kB
JavaScript
import { ChartType } from "../../../types";
import { InsightType } from "../type";
import { DEFAULT_SERIES_NAME } from "../const";
import { TrendType } from "./statistics";
import { getFieldIdInCell } from "../../../utils/field";
const getFieldInfoById = (fieldInfo, fieldId) => fieldInfo.find((info => info.fieldName === fieldId));
export const isEmptySeries = seriesName => !seriesName || seriesName === DEFAULT_SERIES_NAME;
const getMinMaxTemplate = (insight, ctx, language) => {
var _a, _b, _c, _d, _e;
const {value: value, info: info, type: type} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, xFieldId = getFieldIdInCell(cell.x), yFieldId = getFieldIdInCell(cell.y), isChinese = "chinese" === language, placeholderValue = type === InsightType.Min ? isChinese ? "最小值" : "minimum" : isChinese ? "最大值" : "maximum";
return {
content: (null == info ? void 0 : info.isAxesArea) ? isChinese ? `\${a}的${placeholderValue}位于\${b},值为\${c}` : `The ${placeholderValue} value of \${a} at \${b}, with a value of \${c}` : isChinese ? `${placeholderValue}位于\${b},值为\${c}` : `The ${placeholderValue} value at \${b} with a value of \${c}`,
variables: Object.assign(Object.assign({}, (null == info ? void 0 : info.isAxesArea) ? {
a: {
value: (null == info ? void 0 : info.titleName) || (null === (_a = getFieldInfoById(fieldInfo, yFieldId)) || void 0 === _a ? void 0 : _a.alias) || yFieldId,
fieldName: null !== (_c = null === (_b = getFieldInfoById(fieldInfo, yFieldId)) || void 0 === _b ? void 0 : _b.alias) && void 0 !== _c ? _c : yFieldId
}
} : {}), {
b: {
isDimValue: !0,
value: info.dimValue,
fieldName: null !== (_e = null === (_d = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _d ? void 0 : _d.alias) && void 0 !== _e ? _e : xFieldId
},
c: {
value: value,
isMeasure: !0,
fieldName: null
}
})
};
}, getAvgTemplate = (insight, ctx, language) => {
var _a, _b, _c;
const {value: value, info: info} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, yFieldId = getFieldIdInCell(cell.y), isChinese = "chinese" === language;
return {
content: (null == info ? void 0 : info.isAxesArea) ? isChinese ? "${a}的平均值为${b}" : "The average value of ${a} is ${b}" : isChinese ? "平均值为${b}" : "The average value is ${b}",
variables: Object.assign(Object.assign({}, (null == info ? void 0 : info.isAxesArea) ? {
a: {
value: (null == info ? void 0 : info.titleName) || (null === (_a = getFieldInfoById(fieldInfo, yFieldId)) || void 0 === _a ? void 0 : _a.alias) || yFieldId,
fieldName: null !== (_c = null === (_b = getFieldInfoById(fieldInfo, yFieldId)) || void 0 === _b ? void 0 : _b.alias) && void 0 !== _c ? _c : yFieldId
}
} : {}), {
b: {
value: value,
isMeasure: !0,
fieldName: null
}
})
};
}, getOutlierTemplate = (insight, ctx, language) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
const {seriesName: seriesName, data: data, value: value, fieldId: fieldId} = insight, {fieldInfo: fieldInfo, cell: cell, chartType: chartType} = ctx, xFieldId = getFieldIdInCell(cell.x), seriesField = getFieldIdInCell(null == cell ? void 0 : cell.color), isChinese = "chinese" === language;
return [ ChartType.ScatterPlot ].includes(chartType) ? {
content: isEmptySeries(seriesName) ? isChinese ? "(${b}, ${c})上显著异常" : "Significant anomaly at (${b}, ${c})" : isChinese ? "${a}在(${b}, ${c})上显著异常" : "${a} shows a significant anomaly at (${b}, ${c})",
variables: Object.assign(Object.assign({}, isEmptySeries(seriesName) ? {} : {
a: {
value: seriesName,
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : seriesField
}
}), {
b: {
isDimValue: !0,
value: null === (_d = null === (_c = null == data ? void 0 : data[0]) || void 0 === _c ? void 0 : _c.dataItem) || void 0 === _d ? void 0 : _d[fieldId[0]],
fieldName: null !== (_f = null === (_e = getFieldInfoById(fieldInfo, fieldId[0])) || void 0 === _e ? void 0 : _e.alias) && void 0 !== _f ? _f : fieldId[0]
},
c: {
isDimValue: !0,
value: null === (_h = null === (_g = null == data ? void 0 : data[0]) || void 0 === _g ? void 0 : _g.dataItem) || void 0 === _h ? void 0 : _h[fieldId[1]],
fieldName: null !== (_k = null === (_j = getFieldInfoById(fieldInfo, fieldId[1])) || void 0 === _j ? void 0 : _j.alias) && void 0 !== _k ? _k : fieldId[1]
}
})
} : {
content: isEmptySeries(seriesName) ? isChinese ? "${b}上显著异常,值为${c}" : "Significant anomaly at ${b}, with a value of ${c}" : isChinese ? "${a}在${b}上显著异常,值为${c}" : "${a} shows a significant anomaly at ${b}, with a value of ${c}",
variables: Object.assign(Object.assign({}, isEmptySeries(seriesName) ? {} : {
a: {
value: seriesName,
fieldName: null !== (_m = null === (_l = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _l ? void 0 : _l.alias) && void 0 !== _m ? _m : seriesField
}
}), {
b: {
isDimValue: !0,
value: null === (_p = null === (_o = null == data ? void 0 : data[0]) || void 0 === _o ? void 0 : _o.dataItem) || void 0 === _p ? void 0 : _p[xFieldId],
fieldName: null !== (_r = null === (_q = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _q ? void 0 : _q.alias) && void 0 !== _r ? _r : xFieldId
},
c: {
value: value,
isMeasure: !0,
fieldName: null !== (_t = null === (_s = getFieldInfoById(fieldInfo, fieldId)) || void 0 === _s ? void 0 : _s.alias) && void 0 !== _t ? _t : fieldId
}
})
};
}, getTurnPointTemplate = (insight, ctx, language) => {
const res = getOutlierTemplate(insight, ctx, language);
return {
content: "chinese" === language ? res.content.replaceAll("上显著异常", "是个拐点") : res.content.replaceAll("Significant anomaly", "Turning point").replaceAll("significant anomaly", "turning point"),
variables: res.variables
};
}, getExtremeTemplate = (insight, ctx, language) => {
const res = getOutlierTemplate(insight, ctx, language);
return {
content: "chinese" === language ? res.content.replaceAll("上显著异常", "是极值") : res.content.replaceAll("Significant anomaly", "Extreme value").replaceAll("significant anomaly", "extreme value"),
variables: res.variables
};
}, getMajorityTemplate = (insight, ctx, language) => {
var _a, _b, _c, _d, _e, _f;
const {seriesName: seriesName, fieldId: fieldId, info: info} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, {ratio: ratio, dimensionName: dimensionName} = info, xFieldId = getFieldIdInCell(cell.x), seriesField = getFieldIdInCell(null == cell ? void 0 : cell.color);
return {
content: "chinese" === language ? "${a}在${b}的占比贡献度显著,占比高达${c}" : "${a} significantly contributes to ${b}, at ${c}",
variables: {
a: {
value: seriesName,
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : seriesField
},
b: {
value: dimensionName,
isDimValue: !0,
fieldName: null !== (_d = null === (_c = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _c ? void 0 : _c.alias) && void 0 !== _d ? _d : xFieldId
},
c: {
value: ratio,
formatValue: `${(100 * ratio).toFixed(1)}%`,
fieldName: null !== (_f = null === (_e = getFieldInfoById(fieldInfo, fieldId)) || void 0 === _e ? void 0 : _e.alias) && void 0 !== _f ? _f : fieldId,
icon: "ratio"
}
}
};
}, getAbnormalBandTemplate = (insight, ctx, language) => {
var _a, _b, _c, _d, _e, _f;
const {seriesName: seriesName, data: data} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, xFieldId = getFieldIdInCell(cell.x), seriesField = getFieldIdInCell(null == cell ? void 0 : cell.color), isChinese = "chinese" === language;
return {
content: isEmptySeries(seriesName) ? isChinese ? "${b}至${c}之间存在异常区间" : "There is an anomalous interval between ${b} and ${c}" : isChinese ? "${a}在${b}至${c}之间存在异常区间" : "${a} has an anomalous interval between ${b} and ${c}",
variables: Object.assign(Object.assign({}, isEmptySeries(seriesName) ? {} : {
a: {
value: seriesName,
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : seriesField
}
}), {
b: {
isDimValue: !0,
value: data[0].dataItem[xFieldId],
fieldName: null !== (_d = null === (_c = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _c ? void 0 : _c.alias) && void 0 !== _d ? _d : xFieldId
},
c: {
isDimValue: !0,
value: data[data.length - 1].dataItem[xFieldId],
fieldName: null !== (_f = null === (_e = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _e ? void 0 : _e.alias) && void 0 !== _f ? _f : xFieldId
}
})
};
}, getOverallTrendTemplate = (insight, ctx, language) => {
var _a, _b, _c, _d;
const {value: value, info: info} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, {startDimValue: startDimValue, endDimValue: endDimValue, change: change, overall: overall} = info, xFieldId = getFieldIdInCell(cell.x), isChinese = "chinese" === language;
return {
content: isChinese ? `数据整体呈\${a}趋势,整体${value === TrendType.INCREASING ? "增长了" : "下降了"}\${d}。其中在\${b}至\${c}间连续\${a}。` : `The overall data shows a \${a} trend, with an overall ${value === TrendType.INCREASING ? "increase " : "decrease "}of \${d}. Notably, from \${b} to \${c}, there was a continuous \${a} trend.`,
variables: {
a: {
value: isChinese ? value === TrendType.INCREASING ? "上升" : "下降" : value === TrendType.INCREASING ? "increasing" : "decreasing",
fieldName: null,
icon: value === TrendType.INCREASING ? "ascendTrend" : "descendTrend"
},
b: {
isDimValue: !0,
value: startDimValue,
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : xFieldId
},
c: {
isDimValue: !0,
value: endDimValue,
fieldName: null !== (_d = null === (_c = getFieldInfoById(fieldInfo, xFieldId)) || void 0 === _c ? void 0 : _c.alias) && void 0 !== _d ? _d : xFieldId
},
d: {
formatValue: (100 * Math.abs(overall.change)).toFixed(1) + "%",
value: overall.change,
valueType: value === TrendType.INCREASING ? "ascendTrend" : "descendTrend",
fieldName: null
}
}
};
}, getAbnormalTrendTemplate = (insight, ctx, language) => {
var _a, _b;
const {seriesName: seriesName, value: value, info: info} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, seriesField = getFieldIdInCell(null == cell ? void 0 : cell.color), isChinese = "chinese" === language;
return {
content: isChinese ? "${a}趋势异常,呈${b}趋势,整体${b}了${c}" : "The ${a} trend is abnormal, showing a ${b} trend, with an overall ${b} of ${c}.",
variables: {
a: {
value: seriesName,
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : seriesField
},
b: {
value: isChinese ? value === TrendType.INCREASING ? "上升" : "下降" : value === TrendType.INCREASING ? "increase" : "decrease",
fieldName: null,
icon: value === TrendType.INCREASING ? "ascendTrend" : "descendTrend"
},
c: {
value: (100 * Math.abs(info.change)).toFixed(1) + "%",
valueType: value === TrendType.INCREASING ? "ascendTrend" : "descendTrend",
fieldName: null
}
}
};
}, getCorrelationTemplate = (insight, ctx, language) => {
var _a, _b, _c, _d, _e, _f;
const {seriesName: seriesName, value: value, info: info, name: name} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, {correlationType: correlationType} = info || {}, seriesField = getFieldIdInCell(null == cell ? void 0 : cell.color), isChinese = "chinese" === language;
return "spearman" === name ? {
content: isChinese ? "${a}和${b}呈${c}相关" : "${a} and ${b} show a ${c} correlation",
variables: {
a: {
value: seriesName[0],
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : seriesField
},
b: {
value: seriesName[1],
fieldName: null !== (_d = null === (_c = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _c ? void 0 : _c.alias) && void 0 !== _d ? _d : seriesField
},
c: {
value: isChinese ? "positive" === correlationType ? "正" : "负" : "positive" === correlationType ? "positive" : "negative",
fieldName: null
}
}
} : {
content: isEmptySeries(seriesName) ? isChinese ? "图表在xy上呈线性相关" : "The chart shows a linear correlation on the xy plane" : isChinese ? "${a}在xy上呈线性相关" : "${a} shows a linear correlation on the xy plane",
variables: Object.assign({}, isEmptySeries(seriesName) ? {
a: {
value: seriesName,
fieldName: null !== (_f = null === (_e = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _e ? void 0 : _e.alias) && void 0 !== _f ? _f : seriesField
}
} : {})
};
}, getVolatilityTemplate = (insight, ctx, language) => {
var _a, _b;
const {seriesName: seriesName} = insight, {fieldInfo: fieldInfo, cell: cell} = ctx, seriesField = getFieldIdInCell(null == cell ? void 0 : cell.color);
return {
content: isEmptySeries(seriesName) ? "chinese" === language ? "数据呈周期性波动" : "The data shows cyclical fluctuations." : "chinese" === language ? "${a}呈周期性波动" : "${a} shows cyclical fluctuations",
variables: isEmptySeries(seriesName) ? {} : {
a: {
value: seriesName,
fieldName: null !== (_b = null === (_a = getFieldInfoById(fieldInfo, seriesField)) || void 0 === _a ? void 0 : _a.alias) && void 0 !== _b ? _b : seriesField
}
}
};
};
export const addPlainText = textContent => {
const {content: content, variables: variables = {}} = textContent;
let plainText = `${content}`;
return Object.keys(variables).forEach((key => {
const value = variables[key];
plainText = plainText.replaceAll(`\${${key}}`, `${value.formatValue || value.value}`);
})), Object.assign(Object.assign({}, textContent), {
plainText: plainText
});
};
export const generateInsightTemplate = (insights, ctx, language) => {
for (let i = 0; i < insights.length; i++) {
const {type: type} = insights[i];
let textContent = null;
switch (type) {
case InsightType.Outlier:
textContent = getOutlierTemplate(insights[i], ctx, language);
break;
case InsightType.TurningPoint:
textContent = getTurnPointTemplate(insights[i], ctx, language);
break;
case InsightType.MajorityValue:
textContent = getMajorityTemplate(insights[i], ctx, language);
break;
case InsightType.AbnormalBand:
textContent = getAbnormalBandTemplate(insights[i], ctx, language);
break;
case InsightType.OverallTrend:
textContent = getOverallTrendTemplate(insights[i], ctx, language);
break;
case InsightType.AbnormalTrend:
textContent = getAbnormalTrendTemplate(insights[i], ctx, language);
break;
case InsightType.Correlation:
textContent = getCorrelationTemplate(insights[i], ctx, language);
break;
case InsightType.Volatility:
textContent = getVolatilityTemplate(insights[i], ctx, language);
break;
case InsightType.ExtremeValue:
textContent = getExtremeTemplate(insights[i], ctx, language);
break;
case InsightType.Min:
case InsightType.Max:
textContent = getMinMaxTemplate(insights[i], ctx, language);
break;
case InsightType.Avg:
textContent = getAvgTemplate(insights[i], ctx, language);
break;
default:
textContent = {
content: "chinese" === language ? `数据含有${insights[i].type}的见解` : `Data has ${insights[i].type} insight`
};
}
insights[i].textContent = addPlainText(textContent);
}
return insights;
};
//# sourceMappingURL=template.js.map