@hisptz/react-ui
Version:
A collection of reusable complex DHIS2 react ui components.
135 lines (109 loc) • 5.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DHIS2MultiSeriesChart = void 0;
var _lodash = require("lodash");
var _chart = require("../utils/chart");
var _index = require("./index");
class DHIS2MultiSeriesChart extends _index.DHIS2Chart {
getHighchartsType() {
return "";
}
getPlotOptions() {
return {
column: {},
line: {}
};
}
getSeries() {
var _analytics$headers, _analytics$headers2, _head, _analytics$headers3, _seriesConfig$series;
const analytics = this.analytics;
const config = this.config;
const categoryDimension = (0, _lodash.head)(config.layout.category);
const seriesConfig = this.config.multiSeries;
const categoryIndex = (0, _chart.getDimensionHeaderIndex)((_analytics$headers = analytics.headers) !== null && _analytics$headers !== void 0 ? _analytics$headers : [], categoryDimension !== null && categoryDimension !== void 0 ? categoryDimension : "");
const seriesIndex = (0, _chart.getDimensionHeaderIndex)((_analytics$headers2 = analytics.headers) !== null && _analytics$headers2 !== void 0 ? _analytics$headers2 : [], (_head = (0, _lodash.head)(config.layout.series)) !== null && _head !== void 0 ? _head : "");
const valueIndex = (0, _chart.getDimensionHeaderIndex)((_analytics$headers3 = analytics.headers) !== null && _analytics$headers3 !== void 0 ? _analytics$headers3 : [], "value");
if (!categoryDimension) {
throw new Error("At least one category dimension is required");
}
if (!seriesConfig) {
throw new Error("MultiSeries config is required for chart type multi-series");
}
return (0, _lodash.compact)(seriesConfig === null || seriesConfig === void 0 ? void 0 : (_seriesConfig$series = seriesConfig.series) === null || _seriesConfig$series === void 0 ? void 0 : _seriesConfig$series.map(_ref => {
var _analytics$metaData, _analytics$metaData2;
let {
id,
as,
cumulative,
yAxis
} = _ref;
const dataItem = (_analytics$metaData = analytics.metaData) === null || _analytics$metaData === void 0 ? void 0 : _analytics$metaData.items[id];
const categoryItems = (_analytics$metaData2 = analytics.metaData) === null || _analytics$metaData2 === void 0 ? void 0 : _analytics$metaData2.dimensions[categoryDimension];
const data = categoryItems === null || categoryItems === void 0 ? void 0 : categoryItems.map(item => {
var _analytics$rows;
const row = (_analytics$rows = analytics.rows) === null || _analytics$rows === void 0 ? void 0 : _analytics$rows.find(row => row[categoryIndex] === item && row[seriesIndex] === id);
return row !== null && row !== void 0 && row[valueIndex] ? parseFloat(row === null || row === void 0 ? void 0 : row[valueIndex]) : 0;
});
let cumulativeData = [];
if (cumulative) {
var _data$reduce;
cumulativeData = (_data$reduce = data === null || data === void 0 ? void 0 : data.reduce((acc, curr, index) => {
if (index === 0) {
return [...acc, curr];
}
return [...acc, acc[index - 1] + curr];
}, [])) !== null && _data$reduce !== void 0 ? _data$reduce : [];
}
return {
name: dataItem === null || dataItem === void 0 ? void 0 : dataItem.name,
data: cumulative ? cumulativeData : data,
type: as,
yAxis: yAxis !== null && yAxis !== void 0 ? yAxis : 0
};
}));
}
getXAxis() {
return {
type: "category",
categories: (0, _chart.getAllCategories)(this.analytics, this.config),
crosshair: true,
labels: {
enabled: true
},
title: {
text: ""
}
};
}
getYAxis() {
var _this$config$multiSer, _this$config$multiSer3, _this$config$multiSer4, _this$config$multiSer5, _yAxes$0$plotLines, _styles$color, _styles$dashStyle, _styles$width, _styles$zIndex;
let yAxes = [];
if ((_this$config$multiSer = this.config.multiSeries) !== null && _this$config$multiSer !== void 0 && _this$config$multiSer.yAxes) {
var _this$config$multiSer2;
yAxes = (_this$config$multiSer2 = this.config.multiSeries) === null || _this$config$multiSer2 === void 0 ? void 0 : _this$config$multiSer2.yAxes;
} else {
yAxes = super.getYAxis();
}
if (!((_this$config$multiSer3 = this.config.multiSeries) !== null && _this$config$multiSer3 !== void 0 && _this$config$multiSer3.target)) {
return yAxes;
}
const {
value,
styles,
label
} = (_this$config$multiSer4 = (_this$config$multiSer5 = this.config.multiSeries) === null || _this$config$multiSer5 === void 0 ? void 0 : _this$config$multiSer5.target) !== null && _this$config$multiSer4 !== void 0 ? _this$config$multiSer4 : {};
return [{ ...yAxes[0],
plotLines: [...((_yAxes$0$plotLines = yAxes[0].plotLines) !== null && _yAxes$0$plotLines !== void 0 ? _yAxes$0$plotLines : []), {
color: (_styles$color = styles === null || styles === void 0 ? void 0 : styles.color) !== null && _styles$color !== void 0 ? _styles$color : "#00FF00",
dashStyle: (_styles$dashStyle = styles === null || styles === void 0 ? void 0 : styles.dashStyle) !== null && _styles$dashStyle !== void 0 ? _styles$dashStyle : "Solid",
value,
width: (_styles$width = styles === null || styles === void 0 ? void 0 : styles.width) !== null && _styles$width !== void 0 ? _styles$width : 2,
zIndex: (_styles$zIndex = styles === null || styles === void 0 ? void 0 : styles.zIndex) !== null && _styles$zIndex !== void 0 ? _styles$zIndex : 1000,
label: label
}]
}, ...yAxes.slice(1)];
}
}
exports.DHIS2MultiSeriesChart = DHIS2MultiSeriesChart;