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

77 lines (70 loc) 2.83 kB
import { chartAdvisor } from "@visactor/chart-advisor"; import { isValidDataTable } from "../../../utils/dataTable"; import { chartTypeMap, getCell, typeMap, VMindChartTypeMap } from "../utils"; import { ChartType as VMindChartType } from "../../../types"; import { uniqBy } from "../../../utils/common"; const getAdvisedChartList = (schema, dataset) => { const dimensionList = schema.fields.filter((d => "dimension" === d.role)).map((d => ({ uniqueId: d.id, type: typeMap(d.type) }))), measureList = schema.fields.filter((d => "measure" === d.role)).map((d => ({ uniqueId: d.id, type: typeMap(d.type) }))), aliasMap = Object.fromEntries(schema.fields.map((d => [ d.id, d.alias ]))); return chartAdvisor({ originDataset: dataset, dimensionList: dimensionList, measureList: measureList, aliasMap: aliasMap }); }, getAdvisedListTransformer = context => { const {vizSchema: vizSchema, dataTable: dataTable, chartTypeList: chartTypeList} = context; if (!isValidDataTable(dataTable)) return { advisedList: [], chartSource: "chartAdvisor", usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } }; const {scores: scores} = getAdvisedChartList(vizSchema, dataTable), availableChartTypeList = chartTypeList.reduce(((res, chartType) => { var _a; return [ ...res, ...null !== (_a = null == VMindChartTypeMap ? void 0 : VMindChartTypeMap[chartType]) && void 0 !== _a ? _a : [] ]; }), []); return { advisedList: uniqBy(scores.filter((d => availableChartTypeList.includes(d.chartType) && d.score - 0 >= 1e-8)).map((result => ({ chartType: chartTypeMap(result.chartType).toUpperCase(), cell: getCell(result.cell), dataset: result.dataset, score: result.score }))), "chartType"), chartSource: "chartAdvisor", usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } }; }; export const getCellContextByAdvisor = context => { const advisorResult = getAdvisedListTransformer(context), {advisedList: advisedList, chartSource: chartSource, usage: usage} = advisorResult; if (0 === advisedList.length) return { chartType: VMindChartType.BarChart.toUpperCase(), cell: {}, dataset: [], chartSource: chartSource, usage: usage, advisedList: advisedList }; const result = advisedList[0]; return { chartType: result.chartType, cell: getCell(result.cell), dataset: result.dataset, chartSource: chartSource, usage: usage, advisedList: advisedList }; }; //# sourceMappingURL=index.js.map