UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

226 lines 10.2 kB
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; import { Component, Input, ViewEncapsulation, ChangeDetectionStrategy, ContentChild } from '@angular/core'; import { scaleBand } from 'd3-scale'; import { BaseChartComponent } from '../common/base-chart.component'; import { calculateViewDimensions } from '../common/view-dimensions.helper'; import { ColorHelper } from '../common/color.helper'; var HeatMapComponent = (function (_super) { __extends(HeatMapComponent, _super); function HeatMapComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legendTitle = 'Legend'; _this.innerPadding = 8; _this.tooltipDisabled = false; _this.margin = [10, 20, 10, 20]; _this.xAxisHeight = 0; _this.yAxisWidth = 0; _this.scaleType = 'linear'; return _this; } HeatMapComponent.prototype.update = function () { _super.prototype.update.call(this); this.formatDates(); this.xDomain = this.getXDomain(); this.yDomain = this.getYDomain(); this.valueDomain = this.getValueDomain(); this.scaleType = this.getScaleType(this.valueDomain); this.dims = calculateViewDimensions({ width: this.width, height: this.height, margins: this.margin, showXAxis: this.xAxis, showYAxis: this.yAxis, xAxisHeight: this.xAxisHeight, yAxisWidth: this.yAxisWidth, showXLabel: this.showXAxisLabel, showYLabel: this.showYAxisLabel, showLegend: this.legend, legendType: this.scaleType }); if (this.scaleType === 'linear') { var min = Math.min.apply(Math, [0].concat(this.valueDomain)); var max = Math.max.apply(Math, this.valueDomain); this.valueDomain = [min, max]; } this.xScale = this.getXScale(); this.yScale = this.getYScale(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")"; this.rects = this.getRects(); }; HeatMapComponent.prototype.getXDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; if (!domain.includes(group.name)) { domain.push(group.name); } } return domain; }; HeatMapComponent.prototype.getYDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; for (var _b = 0, _c = group.series; _b < _c.length; _b++) { var d = _c[_b]; if (!domain.includes(d.name)) { domain.push(d.name); } } } return domain; }; HeatMapComponent.prototype.getValueDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; for (var _b = 0, _c = group.series; _b < _c.length; _b++) { var d = _c[_b]; if (!domain.includes(d.value)) { domain.push(d.value); } } } return domain; }; /** * Converts the input to gap paddingInner in fraction * Supports the following inputs: * Numbers: 8 * Strings: "8", "8px", "8%" * Arrays: [8,2], "8,2", "[8,2]" * Mixed: [8,"2%"], ["8px","2%"], "8,2%", "[8,2%]" * * @param {(string | number | Array<string | number>)} value * @param {number} [index=0] * @param {number} N * @param {number} L * @returns {number} * * @memberOf HeatMapComponent */ HeatMapComponent.prototype.getDimension = function (value, index, N, L) { if (index === void 0) { index = 0; } if (typeof value === 'string') { value = value .replace('[', '') .replace(']', '') .replace('px', '') .replace('\'', ''); if (value.includes(',')) { value = value.split(','); } } if (Array.isArray(value) && typeof index === 'number') { return this.getDimension(value[index], null, N, L); } if (typeof value === 'string' && value.includes('%')) { return +value.replace('%', '') / 100; } return N / (L / +value + 1); }; HeatMapComponent.prototype.getXScale = function () { var f = this.getDimension(this.innerPadding, 0, this.xDomain.length, this.dims.width); return scaleBand() .rangeRound([0, this.dims.width]) .domain(this.xDomain) .paddingInner(f); }; HeatMapComponent.prototype.getYScale = function () { var f = this.getDimension(this.innerPadding, 1, this.yDomain.length, this.dims.height); return scaleBand() .rangeRound([this.dims.height, 0]) .domain(this.yDomain) .paddingInner(f); }; HeatMapComponent.prototype.getRects = function () { var _this = this; var rects = []; this.xDomain.map(function (xVal) { _this.yDomain.map(function (yVal) { rects.push({ x: _this.xScale(xVal), y: _this.yScale(yVal), rx: 3, width: _this.xScale.bandwidth(), height: _this.yScale.bandwidth(), fill: 'rgba(200,200,200,0.03)' }); }); }); return rects; }; HeatMapComponent.prototype.onClick = function (data) { this.select.emit(data); }; HeatMapComponent.prototype.getScaleType = function (values) { var num = true; for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { var value = values_1[_i]; if (typeof value !== 'number') { num = false; } } if (num) return 'linear'; return 'ordinal'; }; HeatMapComponent.prototype.setColors = function () { this.colors = new ColorHelper(this.scheme, this.scaleType, this.valueDomain); }; HeatMapComponent.prototype.getLegendOptions = function () { return { scaleType: this.scaleType, domain: this.valueDomain, colors: this.scaleType === 'ordinal' ? this.colors : this.colors.scale, title: this.scaleType === 'ordinal' ? this.legendTitle : undefined }; }; HeatMapComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; HeatMapComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; return HeatMapComponent; }(BaseChartComponent)); export { HeatMapComponent }; HeatMapComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-charts-heat-map', template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n (legendLabelClick)=\"onClick($event)\">\n <svg:g [attr.transform]=\"transform\" class=\"heat-map chart\">\n <svg:g ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n [tickFormatting]=\"xAxisTickFormatting\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\">\n </svg:g>\n <svg:g ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [tickFormatting]=\"yAxisTickFormatting\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\">\n </svg:g>\n <svg:rect *ngFor=\"let rect of rects\"\n [attr.x]=\"rect.x\"\n [attr.y]=\"rect.y\"\n [attr.rx]=\"rect.rx\"\n [attr.width]=\"rect.width\"\n [attr.height]=\"rect.height\"\n [attr.fill]=\"rect.fill\"\n />\n <svg:g ngx-charts-heat-map-cell-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"results\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [tooltipText]=\"tooltipText\"\n (select)=\"onClick($event)\"\n />\n </svg:g>\n </ngx-charts-chart>\n ", changeDetection: ChangeDetectionStrategy.OnPush, styleUrls: ['../common/base-chart.component.css'], encapsulation: ViewEncapsulation.None },] }, ]; /** @nocollapse */ HeatMapComponent.ctorParameters = function () { return []; }; HeatMapComponent.propDecorators = { 'legend': [{ type: Input },], 'legendTitle': [{ type: Input },], 'xAxis': [{ type: Input },], 'yAxis': [{ type: Input },], 'showXAxisLabel': [{ type: Input },], 'showYAxisLabel': [{ type: Input },], 'xAxisLabel': [{ type: Input },], 'yAxisLabel': [{ type: Input },], 'gradient': [{ type: Input },], 'innerPadding': [{ type: Input },], 'xAxisTickFormatting': [{ type: Input },], 'yAxisTickFormatting': [{ type: Input },], 'tooltipDisabled': [{ type: Input },], 'tooltipText': [{ type: Input },], 'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },], }; //# sourceMappingURL=heat-map.component.js.map