VIPRDataPointSummarizer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. 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; };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. /**
  5. *+------------------------------------------------------------------------+
  6. *| Licensed Materials - Property of IBM
  7. *| IBM Cognos Products: Dashboard
  8. *| (C) Copyright IBM Corp. 2020
  9. *|
  10. *| US Government Users Restricted Rights - Use, duplication or disclosure
  11. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  12. *+------------------------------------------------------------------------+
  13. */
  14. define(['underscore', '../../../apiHelpers/SlotAPIHelper'], function (_, SlotAPIHelper) {
  15. var VIPRDataPointSummarizer = function () {
  16. //Helper used to provide summaries for VIPR entities (like legends/axis titles) to support tooltips etc.
  17. function VIPRDataPointSummarizer(visualization) {
  18. _classCallCheck(this, VIPRDataPointSummarizer);
  19. this.slots = visualization && visualization.getSlots();
  20. }
  21. /**
  22. * Summarize the datapoint values by tuple item
  23. * This function will be called when a legend item or axis title is selected.
  24. *
  25. * @param viprDataPoints array of VIPRDataPoints to summarize
  26. * @param itemIndex dataitem index of the tuple class
  27. * @param itemSource tuple item source
  28. */
  29. VIPRDataPointSummarizer.prototype.summarizeDataPointValues = function summarizeDataPointValues(viprDataPoints, itemIndex, itemSource) {
  30. var _this = this;
  31. // build data for the header (ie: captions for the selection '2007 | Winter')
  32. var values = this._buildHeaderLine(itemSource, itemIndex);
  33. if (!viprDataPoints || !viprDataPoints.length || !this.slots) {
  34. //There are no points to summarize, just return the header.
  35. return values;
  36. }
  37. // Summarize the measure values for the points this tuple represents for this selection by measureId.
  38. // (eg: 10 points are involved when hovering on a legend. Sum the measures for those 10 points (if appliable))
  39. var datapointsMap = {};
  40. var dataset = itemSource.dataset;
  41. if (dataset) {
  42. (function () {
  43. var slotList = _this.slots.getMappedSlotList();
  44. var datasetSlotList = slotList.filter(function (slot) {
  45. return slot.getDefinition().getDatasetIdList().indexOf(dataset.id) !== -1;
  46. });
  47. var multiMeasureAssignment = _this._getMultiMeasureAssignment(datasetSlotList, itemSource);
  48. // Add values to corresponding measure in map
  49. var _loop = function _loop(itemIdx) {
  50. var viprDataItem = dataset.getDataItem(itemIdx);
  51. if (viprDataItem.getType() === 'cont') {
  52. // Don't assume that the itemIdx is the same as the slot index
  53. // there could be a slot that contains multiple dataitems
  54. // We get the slot index from viprDataItem.indexes.dataitem
  55. var slotIndex = viprDataItem.indexes && viprDataItem.indexes.dataitem;
  56. if (slotIndex == null) {
  57. // not sure if we would ever run into this.. but just in case we have scenarios where the slots index is not found
  58. slotIndex = itemIdx;
  59. }
  60. var slotAPI = datasetSlotList[slotIndex];
  61. var dataItemAPIs = slotAPI.getDataItemList();
  62. // Construct empty map with value slot data items
  63. slotAPI.getDataItemList().forEach(function (dataitemAPI) {
  64. var columnId = dataitemAPI.getColumnId();
  65. if (!datapointsMap[columnId]) {
  66. datapointsMap[columnId] = {
  67. measureValues: [],
  68. aggregationType: dataitemAPI.getAggregation() || 'sum'
  69. };
  70. }
  71. });
  72. //Run through the datapoints, adding values to the map for this measure.
  73. viprDataPoints.forEach(function (viprDataPoint) {
  74. var measureId = _this._getMeasureIdFromDataPoint(viprDataPoint, dataItemAPIs, multiMeasureAssignment, slotAPI, itemIdx - slotIndex);
  75. if (measureId) {
  76. var viprValue = viprDataPoint.getValue(itemIdx);
  77. datapointsMap[measureId].measureValues.push(viprValue.getValue());
  78. datapointsMap[measureId].indexInResult = slotIndex;
  79. }
  80. });
  81. }
  82. };
  83. for (var itemIdx = 0; itemIdx < dataset.getDataItemCount(); itemIdx++) {
  84. _loop(itemIdx);
  85. }
  86. // Apply summaries for measure based on rules for aggregating.
  87. values = _this._summarizeValuesInPointsMap(values, datapointsMap);
  88. })();
  89. }
  90. return values;
  91. };
  92. // Find where the _multiMeasuresSeries item is assigned and what tuples are to be displayed there
  93. //eg: {
  94. // slotIndex: 2, (the index of the Color slot in a bar chart for example)
  95. // indexInSlot: 0, (the _multiMeasuresSeries item is the first item in the color slot)
  96. // multiMeasureItemCount: 2,
  97. // tuplesInSlot: [VIPRTuple, VIPRTuple]
  98. // }
  99. VIPRDataPointSummarizer.prototype._getMultiMeasureAssignment = function _getMultiMeasureAssignment(slotList, itemSource) {
  100. var mmPlaceholderSlotIndex = void 0;
  101. var mmPlaceholderIndexInSlot = void 0;
  102. var numMMItems = void 0;
  103. var mmDataItem = _.find(slotList, function (slotAPI, slotIdx) {
  104. mmPlaceholderSlotIndex = slotIdx;
  105. var dataItems = slotAPI.getDataItemList() || [];
  106. if (SlotAPIHelper.isMultiMeasuresValueSlot(slotAPI)) {
  107. numMMItems = dataItems.length;
  108. }
  109. return _.find(dataItems, function (dataitemAPI, dataItemIdx) {
  110. mmPlaceholderIndexInSlot = dataItemIdx;
  111. return dataitemAPI.getColumnId() === '_multiMeasuresSeries';
  112. });
  113. });
  114. var multiMeasureTuples = mmDataItem && itemSource && itemSource.dataset && itemSource.dataset.dataItems[mmPlaceholderSlotIndex].tuples;
  115. if (mmDataItem) {
  116. return {
  117. slotIndex: mmPlaceholderSlotIndex, //What slot is the "measure group(n)" placeholder in?
  118. indexInSlot: mmPlaceholderIndexInSlot, //Might be after other categoricals in that slot.
  119. tuplesInSlot: multiMeasureTuples, //VIPRTuples this multimeasure represents
  120. multiMeasureItemCount: numMMItems //A count of the multimeasure items in the value slot.
  121. };
  122. }
  123. return null;
  124. };
  125. VIPRDataPointSummarizer.prototype._getMeasureIdFromDataPoint = function _getMeasureIdFromDataPoint(viprDataPoint, dataItemAPIs, multiMeasureAssignment, slotAPI, dataItemIndex) {
  126. var measureId = null;
  127. if (dataItemAPIs.length > 1 && multiMeasureAssignment && SlotAPIHelper.isMultiMeasuresValueSlot(slotAPI)) {
  128. //Find out what tuple in the placeholder slot we're processing, then pick out the measure columnId.
  129. //If Year and MeasureGroup(2) are assigned, mmTupleIdx might be
  130. //6 for tuple 2008-Revenue
  131. //7 for tuple 2008-Planned
  132. //multiMeasureAssignment.indexInSlot would be 1 (ie: the Measure item for that tuple).
  133. var mmTupleIdx = viprDataPoint.getTupleIndex(multiMeasureAssignment.slotIndex);
  134. var dataItemUId = multiMeasureAssignment.tuplesInSlot[mmTupleIdx].tuple[multiMeasureAssignment.indexInSlot].u;
  135. var dataItemAPI = _.find(dataItemAPIs, function (dataItemAPI) {
  136. return dataItemUId === dataItemAPI.getId();
  137. });
  138. measureId = dataItemAPI && dataItemAPI.getColumnId();
  139. } else if (!multiMeasureAssignment || multiMeasureAssignment && viprDataPoint.index % multiMeasureAssignment.multiMeasureItemCount === 0) {
  140. //Condition 1: There are no multi measures items
  141. //Condition 2: There are multi measure items, but this measure isn't part of the multimeasure.
  142. // Data rows have been interleaved 1 per measure but that means that OTHER measure values were
  143. // duplicated. We only want to add the other values once (so code only executes "% multiMeasureCount")
  144. measureId = dataItemAPIs[dataItemIndex].getColumnId();
  145. }
  146. return measureId;
  147. };
  148. VIPRDataPointSummarizer.prototype._summarizeValuesInPointsMap = function _summarizeValuesInPointsMap(values, datapointsMap) {
  149. //Apply rules to combine the values collected by measure in the datapoints map.
  150. for (var measureId in datapointsMap) {
  151. var measureValues = datapointsMap[measureId].measureValues;
  152. var _aggregationType = datapointsMap[measureId].aggregationType;
  153. var idxInResult = datapointsMap[measureId].indexInResult;
  154. values[idxInResult] = values[idxInResult] || [];
  155. var summarized = void 0;
  156. if (measureValues.length > 1 && (_aggregationType === 'avg' || _aggregationType === 'countdistinct' || _aggregationType === 'calculated')) {
  157. summarized = 'N/A'; //can't support multi-select for avg or countdistinct.
  158. } else {
  159. summarized = measureValues[0]; // Keep first value to preserve single null value.
  160. for (var valueIdx = 1; valueIdx < measureValues.length; valueIdx++) {
  161. summarized += measureValues[valueIdx] || 0;
  162. }
  163. }
  164. values[idxInResult].push(summarized);
  165. }
  166. return values;
  167. };
  168. // build data for the header (ie: captions for the selection '2007 | Winter')
  169. VIPRDataPointSummarizer.prototype._buildHeaderLine = function _buildHeaderLine(itemSource, itemIndex) {
  170. var values = [];
  171. if (typeof itemIndex !== 'undefined' && typeof itemSource !== 'undefined') {
  172. if (itemSource.tuple) {
  173. values[itemIndex] = itemSource.tuple;
  174. } else {
  175. var aValues = [];
  176. aValues.push({
  177. u: itemSource.getUniqueName(),
  178. d: itemSource.getCaption()
  179. });
  180. values[itemIndex] = aValues;
  181. }
  182. if (itemSource.getParent && itemSource.getParent()) {
  183. values[itemIndex][0].p = itemSource.getParent();
  184. }
  185. }
  186. return values;
  187. };
  188. /**
  189. * NOTE: This code is only called when the config sets "shouldMultiPointSelectionSummarize"
  190. * (which is only true for area charts)
  191. * Summarize the datapoint targets to a single target
  192. *
  193. * ie. With the given 4 datapoint targets:
  194. * Golf Equipment, Web, 100
  195. * Camping Equipment, Web, 120,
  196. * Outdoor Protection, Web, 90,
  197. * Personal Accessories, Web, 140
  198. *
  199. * Should be summarized to:
  200. * undefined, Web, 450
  201. *
  202. * @param prevTargets array of multiple datapoint targets to summarize
  203. */
  204. VIPRDataPointSummarizer.prototype.summarizeDataPointTargets = function summarizeDataPointTargets(prevTargets) {
  205. var summary;
  206. var sumOrdIndex = -1;
  207. _.each(prevTargets, function (target) {
  208. if (!summary) {
  209. // clone the first target. Values will be modified so deep clone values.
  210. summary = _extends({}, target);
  211. summary.values = JSON.parse(JSON.stringify(target.values));
  212. } else {
  213. // sum the remaining target to a single target
  214. if (sumOrdIndex < 0) {
  215. _.each(target.values, function (value, index) {
  216. // find the ordinal value index
  217. if (typeof value[0] === 'number') {
  218. sumOrdIndex = index;
  219. } else if (_.isEqual(_.pluck(summary.values[index], 'u'), _.pluck(value, 'u')) === false) {
  220. // Keep the repeating values but invalidate the detail values
  221. summary.values[index] = undefined;
  222. }
  223. });
  224. }
  225. // Add up the remaining datapoint ordinal values
  226. _.each(summary.values[sumOrdIndex], function (value, index) {
  227. if (target.values[sumOrdIndex] && target.values[sumOrdIndex][index]) {
  228. summary.values[sumOrdIndex][index] += target.values[sumOrdIndex][index];
  229. }
  230. });
  231. }
  232. });
  233. // return the summarized target as the sole target
  234. return [summary];
  235. };
  236. return VIPRDataPointSummarizer;
  237. }();
  238. return VIPRDataPointSummarizer;
  239. });
  240. //# sourceMappingURL=VIPRDataPointSummarizer.js.map