UNPKG

@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

44 lines (39 loc) 2.22 kB
import { isArray, isNumber } from "@visactor/vutils"; import { InsightType } from "../../type"; import { ChartType } from "../../../../types"; const getMajorityInGroup = (dataset, measureId, seriesId, dimensionId, threshold = .85) => { const result = [], sum = dataset.reduce(((prev, cur) => { const dataValue = parseFloat(cur.dataItem[measureId]); return prev + (isNumber(dataValue) ? Math.abs(dataValue) : 0); }), 0); return sum - Number.EPSILON < 0 ? [] : (dataset.forEach((d => { const seriesName = d.dataItem[seriesId], dimensionName = d.dataItem[dimensionId], dataValue = parseFloat(d.dataItem[measureId]), ratio = (isNumber(dataValue) ? Math.abs(dataValue) : 0) / sum; ratio > threshold && result.push({ type: InsightType.MajorityValue, data: [ d ], fieldId: measureId, value: d.dataItem[measureId], significant: ratio, seriesName: seriesName, info: { ratio: ratio, dimensionName: dimensionName } }); })), result); }, calcMajorityValue = (context, options) => { const result = [], {cell: cell, dimensionDataMap: dimensionDataMap, seriesDataMap: seriesDataMap} = context, {threshold: threshold} = options || {}, {y: celly, x: cellx, color: color} = cell, yField = isArray(celly) ? celly.flat() : [ celly ], xField = isArray(cellx) ? cellx[0] : cellx; let groupField; return groupField = isArray(cellx) && cellx.length > 1 ? cellx[1] : isArray(color) ? color[0] : color, groupField && xField ? Object.keys(seriesDataMap).length <= 1 ? [] : (Object.keys(dimensionDataMap).forEach((dimension => { const dimensionDataset = dimensionDataMap[dimension], dimensionInsights = getMajorityInGroup(dimensionDataset, yField[0], groupField, xField, threshold); result.push(...dimensionInsights); })), result) : []; }; export const LineChartMajorityValue = { name: "majorityValue", forceChartType: [ ChartType.BarChart, ChartType.AreaChart, ChartType.WaterFallChart ], insightType: InsightType.MajorityValue, algorithmFunction: calcMajorityValue }; //# sourceMappingURL=index.js.map