UNPKG

@hisptz/react-ui

Version:

A collection of reusable complex DHIS2 react ui components.

145 lines (121 loc) 6.36 kB
import { compact, find, findIndex, head, isEmpty, set } from "lodash"; import { DHIS2ColumnChart, DHIS2StackedColumnChart } from "../models/column"; import { DHIS2LineChart } from "../models/line"; import { DHIS2MultiSeriesChart } from "../models/multi-series"; import { DHIS2PieChart } from "../models/pie"; export function getDimensionHeaderIndex(headers, name) { return findIndex(headers, { name }); } export function getPointSeries(analytics, config, highchartsType) { const series = config.layout.series; return series.map(seriesName => { var _analytics$headers; const header = analytics === null || analytics === void 0 ? void 0 : (_analytics$headers = analytics.headers) === null || _analytics$headers === void 0 ? void 0 : _analytics$headers.find(header => header.name === seriesName); if (!header) { return undefined; } if (analytics !== null && analytics !== void 0 && analytics.metaData) { return getColumnSeries(analytics, header, config, highchartsType); } })[0]; } export function getColumnSeries(analytics, header, config, highchartsType) { var _analytics$headers2, _analytics$headers3, _config$colors, _analytics$metaData, _dimensions; const headerIndex = analytics === null || analytics === void 0 ? void 0 : (_analytics$headers2 = analytics.headers) === null || _analytics$headers2 === void 0 ? void 0 : _analytics$headers2.findIndex(h => header.name === h.name); const valueIndex = analytics === null || analytics === void 0 ? void 0 : (_analytics$headers3 = analytics.headers) === null || _analytics$headers3 === void 0 ? void 0 : _analytics$headers3.findIndex(h => h.name === "value"); const colors = (_config$colors = config.colors) !== null && _config$colors !== void 0 ? _config$colors : []; const { items, dimensions } = (_analytics$metaData = analytics === null || analytics === void 0 ? void 0 : analytics.metaData) !== null && _analytics$metaData !== void 0 ? _analytics$metaData : {}; const categoriesDimension = config.layout.category; const seriesDimensionValues = (_dimensions = dimensions === null || dimensions === void 0 ? void 0 : dimensions[header.name]) !== null && _dimensions !== void 0 ? _dimensions : []; return head(categoriesDimension === null || categoriesDimension === void 0 ? void 0 : categoriesDimension.map(categoryDimension => { var _analytics$headers4; const categories = dimensions === null || dimensions === void 0 ? void 0 : dimensions[categoryDimension]; const categoryDimensionIndex = analytics === null || analytics === void 0 ? void 0 : (_analytics$headers4 = analytics.headers) === null || _analytics$headers4 === void 0 ? void 0 : _analytics$headers4.findIndex(h => h.name === categoryDimension); return seriesDimensionValues === null || seriesDimensionValues === void 0 ? void 0 : seriesDimensionValues.map((seriesDimensionValue, index) => { var _items; const data = categories === null || categories === void 0 ? void 0 : categories.map(category => { const row = find(analytics === null || analytics === void 0 ? void 0 : analytics.rows, row => row[headerIndex !== null && headerIndex !== void 0 ? headerIndex : -1] === seriesDimensionValue && row[categoryDimensionIndex !== null && categoryDimensionIndex !== void 0 ? categoryDimensionIndex : -1] === category); return row !== null && row !== void 0 && row[valueIndex !== null && valueIndex !== void 0 ? valueIndex : -1] ? parseFloat(row === null || row === void 0 ? void 0 : row[valueIndex !== null && valueIndex !== void 0 ? valueIndex : -1]) : 0; }); return { name: items === null || items === void 0 ? void 0 : (_items = items[seriesDimensionValue]) === null || _items === void 0 ? void 0 : _items.name, data, type: highchartsType, color: colors[index % colors.length] }; }); })); } function getCategories(_ref, _ref2) { let { name } = _ref; let { items, dimensions } = _ref2; const categories = dimensions === null || dimensions === void 0 ? void 0 : dimensions[name]; return categories === null || categories === void 0 ? void 0 : categories.map(category => { var _items$name, _items2; return (_items$name = (_items2 = items[category]) === null || _items2 === void 0 ? void 0 : _items2.name) !== null && _items$name !== void 0 ? _items$name : ""; }); } export function getAllCategories(analytics, config) { const categories = config.layout.category; return compact(categories === null || categories === void 0 ? void 0 : categories.map(category => { var _analytics$headers5; const header = analytics === null || analytics === void 0 ? void 0 : (_analytics$headers5 = analytics.headers) === null || _analytics$headers5 === void 0 ? void 0 : _analytics$headers5.find(header => header.name === category); if (!header) { return undefined; } if (analytics !== null && analytics !== void 0 && analytics.metaData) { return getCategories(header, analytics === null || analytics === void 0 ? void 0 : analytics.metaData); } }))[0]; } export function updateLayout(config, _ref3) { let { type } = _ref3; if (type === config.type) { return config.layout; } const updatedLayout = { ...config.layout }; switch (type) { case "pie": set(updatedLayout, "category", []); if (isEmpty(updatedLayout.series)) { if (!isEmpty(config.layout.category)) { set(updatedLayout, "series", [head(config.layout.category)]); } else { throw new Error("Invalid layout for pie chart"); } } if (updatedLayout.series.length > 1) { set(updatedLayout, "series", [head(updatedLayout.series)]); } } return updatedLayout; } export function getChartInstance(id, analytics, config) { switch (config.type) { case "column": return new DHIS2ColumnChart(id, analytics, config); case "stacked-column": return new DHIS2StackedColumnChart(id, analytics, config); case "pie": return new DHIS2PieChart(id, analytics, config); case "line": return new DHIS2LineChart(id, analytics, config); case "multi-series": return new DHIS2MultiSeriesChart(id, analytics, config); default: throw new Error("Unsupported chart type: ".concat(config.type)); } }