@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
166 lines (131 loc) • 6.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAllCategories = getAllCategories;
exports.getChartInstance = getChartInstance;
exports.getColumnSeries = getColumnSeries;
exports.getDimensionHeaderIndex = getDimensionHeaderIndex;
exports.getPointSeries = getPointSeries;
exports.updateLayout = updateLayout;
var _lodash = require("lodash");
var _column = require("../models/column");
var _line = require("../models/line");
var _multiSeries = require("../models/multi-series");
var _pie = require("../models/pie");
function getDimensionHeaderIndex(headers, name) {
return (0, _lodash.findIndex)(headers, {
name
});
}
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];
}
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 (0, _lodash.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 = (0, _lodash.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 : "";
});
}
function getAllCategories(analytics, config) {
const categories = config.layout.category;
return (0, _lodash.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];
}
function updateLayout(config, _ref3) {
let {
type
} = _ref3;
if (type === config.type) {
return config.layout;
}
const updatedLayout = { ...config.layout
};
switch (type) {
case "pie":
(0, _lodash.set)(updatedLayout, "category", []);
if ((0, _lodash.isEmpty)(updatedLayout.series)) {
if (!(0, _lodash.isEmpty)(config.layout.category)) {
(0, _lodash.set)(updatedLayout, "series", [(0, _lodash.head)(config.layout.category)]);
} else {
throw new Error("Invalid layout for pie chart");
}
}
if (updatedLayout.series.length > 1) {
(0, _lodash.set)(updatedLayout, "series", [(0, _lodash.head)(updatedLayout.series)]);
}
}
return updatedLayout;
}
function getChartInstance(id, analytics, config) {
switch (config.type) {
case "column":
return new _column.DHIS2ColumnChart(id, analytics, config);
case "stacked-column":
return new _column.DHIS2StackedColumnChart(id, analytics, config);
case "pie":
return new _pie.DHIS2PieChart(id, analytics, config);
case "line":
return new _line.DHIS2LineChart(id, analytics, config);
case "multi-series":
return new _multiSeries.DHIS2MultiSeriesChart(id, analytics, config);
default:
throw new Error("Unsupported chart type: ".concat(config.type));
}
}