UNPKG

@gooddata/react-components

Version:

GoodData.UI - A powerful JavaScript library for building analytical applications

109 lines 5.08 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); // (C) 2020 GoodData Corporation var isEqual = require("lodash/isEqual"); var range = require("lodash/range"); var gooddata_js_1 = require("@gooddata/gooddata-js"); var colorStrategy_1 = require("../colorStrategy"); var executionResultHelper_1 = require("../../../../helpers/executionResultHelper"); var color_1 = require("../../utils/color"); var HeatmapColorStrategy = /** @class */ (function (_super) { __extends(HeatmapColorStrategy, _super); function HeatmapColorStrategy() { return _super !== null && _super.apply(this, arguments) || this; } HeatmapColorStrategy.prototype.getColorByIndex = function (index) { return this.palette[index % this.palette.length]; }; HeatmapColorStrategy.prototype.createColorAssignment = function (colorPalette, colorMapping, _viewByAttribute, _stackByAttribute, executionResponse, afm) { var mappedColor; var colorAssignment; var measureGroup = executionResultHelper_1.findMeasureGroupInDimensions(executionResponse.dimensions); var headerItem = measureGroup && measureGroup.items[0]; if (colorMapping) { mappedColor = color_1.getColorFromMapping(headerItem, colorMapping, executionResponse, afm); if (mappedColor) { colorAssignment = [ { headerItem: headerItem, color: mappedColor, }, ]; } } colorAssignment = colorAssignment || this.getDefaultColorAssignment(colorPalette, headerItem); return { fullColorAssignment: colorAssignment, outputColorAssignment: colorAssignment, }; }; HeatmapColorStrategy.prototype.createPalette = function (colorPalette, colorAssignment) { if (gooddata_js_1.TypeGuards.isRgbColorItem(colorAssignment[0].color)) { if (isEqual(colorAssignment[0].color.value, color_1.DEFAULT_HEATMAP_BLUE_COLOR)) { return color_1.HEATMAP_BLUE_COLOR_PALETTE; } } if (gooddata_js_1.TypeGuards.isGuidColorItem(colorAssignment[0].color)) { return this.getCustomHeatmapColorPalette(color_1.getColorByGuid(colorPalette, colorAssignment[0].color.value, 0)); } return this.getCustomHeatmapColorPalette(colorAssignment[0].color.value); }; HeatmapColorStrategy.prototype.getCustomHeatmapColorPalette = function (baseColor) { var r = baseColor.r, g = baseColor.g, b = baseColor.b; var colorItemsCount = 6; var channels = [r, g, b]; var steps = channels.map(function (channel) { return (255 - channel) / colorItemsCount; }); var generatedColors = this.getCalculatedColors(colorItemsCount, channels, steps); return ["rgb(255,255,255)"].concat(generatedColors.reverse(), [color_1.getRgbStringFromRGB(baseColor)]); }; HeatmapColorStrategy.prototype.getCalculatedColors = function (count, channels, steps) { var _this = this; return range(1, count).map(function (index) { return "rgb(" + _this.getCalculatedChannel(channels[0], index, steps[0]) + "," + (_this.getCalculatedChannel(channels[1], index, steps[1]) + ",") + (_this.getCalculatedChannel(channels[2], index, steps[2]) + ")"); }); }; HeatmapColorStrategy.prototype.getCalculatedChannel = function (channel, index, step) { return Math.trunc(channel + index * step); }; HeatmapColorStrategy.prototype.getDefaultColorAssignment = function (colorPalette, headerItem) { var hasCustomPaletteWithColors = colorPalette && color_1.isCustomPalette(colorPalette) && colorPalette[0]; if (hasCustomPaletteWithColors) { return [ { headerItem: headerItem, color: { type: "guid", value: colorPalette[0].guid, }, }, ]; } return [ { headerItem: headerItem, color: { type: "rgb", value: color_1.DEFAULT_HEATMAP_BLUE_COLOR, }, }, ]; }; return HeatmapColorStrategy; }(colorStrategy_1.default)); exports.default = HeatmapColorStrategy; //# sourceMappingURL=heatmap.js.map