'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| IBM Cognos Products: Dashboard *| (C) Copyright IBM Corp. 2020 *| *| US Government Users Restricted Rights - Use, duplication or disclosure *| restricted by GSA ADP Schedule Contract with IBM Corp. *+------------------------------------------------------------------------+ */ define(['underscore', '../../../apiHelpers/SlotAPIHelper'], function (_, SlotAPIHelper) { var VIPRDataPointSummarizer = function () { //Helper used to provide summaries for VIPR entities (like legends/axis titles) to support tooltips etc. function VIPRDataPointSummarizer(visualization) { _classCallCheck(this, VIPRDataPointSummarizer); this.slots = visualization && visualization.getSlots(); } /** * Summarize the datapoint values by tuple item * This function will be called when a legend item or axis title is selected. * * @param viprDataPoints array of VIPRDataPoints to summarize * @param itemIndex dataitem index of the tuple class * @param itemSource tuple item source */ VIPRDataPointSummarizer.prototype.summarizeDataPointValues = function summarizeDataPointValues(viprDataPoints, itemIndex, itemSource) { var _this = this; // build data for the header (ie: captions for the selection '2007 | Winter') var values = this._buildHeaderLine(itemSource, itemIndex); if (!viprDataPoints || !viprDataPoints.length || !this.slots) { //There are no points to summarize, just return the header. return values; } // Summarize the measure values for the points this tuple represents for this selection by measureId. // (eg: 10 points are involved when hovering on a legend. Sum the measures for those 10 points (if appliable)) var datapointsMap = {}; var dataset = itemSource.dataset; if (dataset) { (function () { var slotList = _this.slots.getMappedSlotList(); var datasetSlotList = slotList.filter(function (slot) { return slot.getDefinition().getDatasetIdList().indexOf(dataset.id) !== -1; }); var multiMeasureAssignment = _this._getMultiMeasureAssignment(datasetSlotList, itemSource); // Add values to corresponding measure in map var _loop = function _loop(itemIdx) { var viprDataItem = dataset.getDataItem(itemIdx); if (viprDataItem.getType() === 'cont') { // Don't assume that the itemIdx is the same as the slot index // there could be a slot that contains multiple dataitems // We get the slot index from viprDataItem.indexes.dataitem var slotIndex = viprDataItem.indexes && viprDataItem.indexes.dataitem; if (slotIndex == null) { // not sure if we would ever run into this.. but just in case we have scenarios where the slots index is not found slotIndex = itemIdx; } var slotAPI = datasetSlotList[slotIndex]; var dataItemAPIs = slotAPI.getDataItemList(); // Construct empty map with value slot data items slotAPI.getDataItemList().forEach(function (dataitemAPI) { var columnId = dataitemAPI.getColumnId(); if (!datapointsMap[columnId]) { datapointsMap[columnId] = { measureValues: [], aggregationType: dataitemAPI.getAggregation() || 'sum' }; } }); //Run through the datapoints, adding values to the map for this measure. viprDataPoints.forEach(function (viprDataPoint) { var measureId = _this._getMeasureIdFromDataPoint(viprDataPoint, dataItemAPIs, multiMeasureAssignment, slotAPI, itemIdx - slotIndex); if (measureId) { var viprValue = viprDataPoint.getValue(itemIdx); datapointsMap[measureId].measureValues.push(viprValue.getValue()); datapointsMap[measureId].indexInResult = slotIndex; } }); } }; for (var itemIdx = 0; itemIdx < dataset.getDataItemCount(); itemIdx++) { _loop(itemIdx); } // Apply summaries for measure based on rules for aggregating. values = _this._summarizeValuesInPointsMap(values, datapointsMap); })(); } return values; }; // Find where the _multiMeasuresSeries item is assigned and what tuples are to be displayed there //eg: { // slotIndex: 2, (the index of the Color slot in a bar chart for example) // indexInSlot: 0, (the _multiMeasuresSeries item is the first item in the color slot) // multiMeasureItemCount: 2, // tuplesInSlot: [VIPRTuple, VIPRTuple] // } VIPRDataPointSummarizer.prototype._getMultiMeasureAssignment = function _getMultiMeasureAssignment(slotList, itemSource) { var mmPlaceholderSlotIndex = void 0; var mmPlaceholderIndexInSlot = void 0; var numMMItems = void 0; var mmDataItem = _.find(slotList, function (slotAPI, slotIdx) { mmPlaceholderSlotIndex = slotIdx; var dataItems = slotAPI.getDataItemList() || []; if (SlotAPIHelper.isMultiMeasuresValueSlot(slotAPI)) { numMMItems = dataItems.length; } return _.find(dataItems, function (dataitemAPI, dataItemIdx) { mmPlaceholderIndexInSlot = dataItemIdx; return dataitemAPI.getColumnId() === '_multiMeasuresSeries'; }); }); var multiMeasureTuples = mmDataItem && itemSource && itemSource.dataset && itemSource.dataset.dataItems[mmPlaceholderSlotIndex].tuples; if (mmDataItem) { return { slotIndex: mmPlaceholderSlotIndex, //What slot is the "measure group(n)" placeholder in? indexInSlot: mmPlaceholderIndexInSlot, //Might be after other categoricals in that slot. tuplesInSlot: multiMeasureTuples, //VIPRTuples this multimeasure represents multiMeasureItemCount: numMMItems //A count of the multimeasure items in the value slot. }; } return null; }; VIPRDataPointSummarizer.prototype._getMeasureIdFromDataPoint = function _getMeasureIdFromDataPoint(viprDataPoint, dataItemAPIs, multiMeasureAssignment, slotAPI, dataItemIndex) { var measureId = null; if (dataItemAPIs.length > 1 && multiMeasureAssignment && SlotAPIHelper.isMultiMeasuresValueSlot(slotAPI)) { //Find out what tuple in the placeholder slot we're processing, then pick out the measure columnId. //If Year and MeasureGroup(2) are assigned, mmTupleIdx might be //6 for tuple 2008-Revenue //7 for tuple 2008-Planned //multiMeasureAssignment.indexInSlot would be 1 (ie: the Measure item for that tuple). var mmTupleIdx = viprDataPoint.getTupleIndex(multiMeasureAssignment.slotIndex); var dataItemUId = multiMeasureAssignment.tuplesInSlot[mmTupleIdx].tuple[multiMeasureAssignment.indexInSlot].u; var dataItemAPI = _.find(dataItemAPIs, function (dataItemAPI) { return dataItemUId === dataItemAPI.getId(); }); measureId = dataItemAPI && dataItemAPI.getColumnId(); } else if (!multiMeasureAssignment || multiMeasureAssignment && viprDataPoint.index % multiMeasureAssignment.multiMeasureItemCount === 0) { //Condition 1: There are no multi measures items //Condition 2: There are multi measure items, but this measure isn't part of the multimeasure. // Data rows have been interleaved 1 per measure but that means that OTHER measure values were // duplicated. We only want to add the other values once (so code only executes "% multiMeasureCount") measureId = dataItemAPIs[dataItemIndex].getColumnId(); } return measureId; }; VIPRDataPointSummarizer.prototype._summarizeValuesInPointsMap = function _summarizeValuesInPointsMap(values, datapointsMap) { //Apply rules to combine the values collected by measure in the datapoints map. for (var measureId in datapointsMap) { var measureValues = datapointsMap[measureId].measureValues; var _aggregationType = datapointsMap[measureId].aggregationType; var idxInResult = datapointsMap[measureId].indexInResult; values[idxInResult] = values[idxInResult] || []; var summarized = void 0; if (measureValues.length > 1 && (_aggregationType === 'avg' || _aggregationType === 'countdistinct' || _aggregationType === 'calculated')) { summarized = 'N/A'; //can't support multi-select for avg or countdistinct. } else { summarized = measureValues[0]; // Keep first value to preserve single null value. for (var valueIdx = 1; valueIdx < measureValues.length; valueIdx++) { summarized += measureValues[valueIdx] || 0; } } values[idxInResult].push(summarized); } return values; }; // build data for the header (ie: captions for the selection '2007 | Winter') VIPRDataPointSummarizer.prototype._buildHeaderLine = function _buildHeaderLine(itemSource, itemIndex) { var values = []; if (typeof itemIndex !== 'undefined' && typeof itemSource !== 'undefined') { if (itemSource.tuple) { values[itemIndex] = itemSource.tuple; } else { var aValues = []; aValues.push({ u: itemSource.getUniqueName(), d: itemSource.getCaption() }); values[itemIndex] = aValues; } if (itemSource.getParent && itemSource.getParent()) { values[itemIndex][0].p = itemSource.getParent(); } } return values; }; /** * NOTE: This code is only called when the config sets "shouldMultiPointSelectionSummarize" * (which is only true for area charts) * Summarize the datapoint targets to a single target * * ie. With the given 4 datapoint targets: * Golf Equipment, Web, 100 * Camping Equipment, Web, 120, * Outdoor Protection, Web, 90, * Personal Accessories, Web, 140 * * Should be summarized to: * undefined, Web, 450 * * @param prevTargets array of multiple datapoint targets to summarize */ VIPRDataPointSummarizer.prototype.summarizeDataPointTargets = function summarizeDataPointTargets(prevTargets) { var summary; var sumOrdIndex = -1; _.each(prevTargets, function (target) { if (!summary) { // clone the first target. Values will be modified so deep clone values. summary = _extends({}, target); summary.values = JSON.parse(JSON.stringify(target.values)); } else { // sum the remaining target to a single target if (sumOrdIndex < 0) { _.each(target.values, function (value, index) { // find the ordinal value index if (typeof value[0] === 'number') { sumOrdIndex = index; } else if (_.isEqual(_.pluck(summary.values[index], 'u'), _.pluck(value, 'u')) === false) { // Keep the repeating values but invalidate the detail values summary.values[index] = undefined; } }); } // Add up the remaining datapoint ordinal values _.each(summary.values[sumOrdIndex], function (value, index) { if (target.values[sumOrdIndex] && target.values[sumOrdIndex][index]) { summary.values[sumOrdIndex][index] += target.values[sumOrdIndex][index]; } }); } }); // return the summarized target as the sole target return [summary]; }; return VIPRDataPointSummarizer; }(); return VIPRDataPointSummarizer; }); //# sourceMappingURL=VIPRDataPointSummarizer.js.map