UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular

345 lines 16.5 kB
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 __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { Component, Input, Output, ViewEncapsulation, EventEmitter, ChangeDetectionStrategy, ContentChild, TemplateRef } from '@angular/core'; import { trigger, style, animate, transition } from '@angular/animations'; import { scaleBand, scaleLinear } from 'd3-scale'; import { calculateViewDimensions } from '../common/view-dimensions.helper'; import { ColorHelper } from '../common/color.helper'; import { BaseChartComponent } from '../common/base-chart.component'; var BarHorizontalNormalizedComponent = /** @class */ (function (_super) { __extends(BarHorizontalNormalizedComponent, _super); function BarHorizontalNormalizedComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legend = false; _this.legendTitle = 'Legend'; _this.legendPosition = 'right'; _this.tooltipDisabled = false; _this.showGridLines = true; _this.activeEntries = []; _this.trimXAxisTicks = true; _this.trimYAxisTicks = true; _this.rotateXAxisTicks = true; _this.maxXAxisTickLength = 16; _this.maxYAxisTickLength = 16; _this.barPadding = 8; _this.roundDomains = false; _this.noBarWhenZero = true; _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); _this.margin = [10, 20, 10, 20]; _this.xAxisHeight = 0; _this.yAxisWidth = 0; return _this; } BarHorizontalNormalizedComponent.prototype.update = function () { _super.prototype.update.call(this); 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.schemeType, legendPosition: this.legendPosition }); this.formatDates(); this.groupDomain = this.getGroupDomain(); this.innerDomain = this.getInnerDomain(); this.valueDomain = this.getValueDomain(); this.xScale = this.getXScale(); this.yScale = this.getYScale(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")"; }; BarHorizontalNormalizedComponent.prototype.getGroupDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; if (!domain.includes(group.label)) { domain.push(group.label); } } return domain; }; BarHorizontalNormalizedComponent.prototype.getInnerDomain = 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.label)) { domain.push(d.label); } } } return domain; }; BarHorizontalNormalizedComponent.prototype.getValueDomain = function () { return [0, 100]; }; BarHorizontalNormalizedComponent.prototype.getYScale = function () { var spacing = this.groupDomain.length / (this.dims.height / this.barPadding + 1); return scaleBand() .rangeRound([0, this.dims.height]) .paddingInner(spacing) .domain(this.groupDomain); }; BarHorizontalNormalizedComponent.prototype.getXScale = function () { var scale = scaleLinear() .range([0, this.dims.width]) .domain(this.valueDomain); return this.roundDomains ? scale.nice() : scale; }; BarHorizontalNormalizedComponent.prototype.groupTransform = function (group) { return "translate(0, " + this.yScale(group.name) + ")"; }; BarHorizontalNormalizedComponent.prototype.onClick = function (data, group) { if (group) { data.series = group.name; } this.select.emit(data); }; BarHorizontalNormalizedComponent.prototype.trackBy = function (index, item) { return item.name; }; BarHorizontalNormalizedComponent.prototype.setColors = function () { var domain; if (this.schemeType === 'ordinal') { domain = this.innerDomain; } else { domain = this.valueDomain; } this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors); }; BarHorizontalNormalizedComponent.prototype.getLegendOptions = function () { var opts = { scaleType: this.schemeType, colors: undefined, domain: [], title: undefined, position: this.legendPosition }; if (opts.scaleType === 'ordinal') { opts.domain = this.innerDomain; opts.colors = this.colors; opts.title = this.legendTitle; } else { opts.domain = this.valueDomain; opts.colors = this.colors.scale; } return opts; }; BarHorizontalNormalizedComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; BarHorizontalNormalizedComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; BarHorizontalNormalizedComponent.prototype.onActivate = function (event, group, fromLegend) { if (fromLegend === void 0) { fromLegend = false; } var item = Object.assign({}, event); if (group) { item.series = group.name; } var items = this.results .map(function (g) { return g.series; }) .flat() .filter(function (i) { if (fromLegend) { return i.label === item.name; } else { return i.name === item.name && i.series === item.series; } }); this.activeEntries = items.slice(); this.activate.emit({ value: item, entries: this.activeEntries }); }; BarHorizontalNormalizedComponent.prototype.onDeactivate = function (event, group, fromLegend) { if (fromLegend === void 0) { fromLegend = false; } var item = Object.assign({}, event); if (group) { item.series = group.name; } this.activeEntries = this.activeEntries.filter(function (i) { if (fromLegend) { return i.label !== item.name; } else { return !(i.name === item.name && i.series === item.series); } }); this.deactivate.emit({ value: item, entries: this.activeEntries }); }; __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "legend", void 0); __decorate([ Input(), __metadata("design:type", String) ], BarHorizontalNormalizedComponent.prototype, "legendTitle", void 0); __decorate([ Input(), __metadata("design:type", String) ], BarHorizontalNormalizedComponent.prototype, "legendPosition", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "xAxis", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "yAxis", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "showXAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "showYAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "xAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "yAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "tooltipDisabled", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "gradient", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "showGridLines", void 0); __decorate([ Input(), __metadata("design:type", Array) ], BarHorizontalNormalizedComponent.prototype, "activeEntries", void 0); __decorate([ Input(), __metadata("design:type", String) ], BarHorizontalNormalizedComponent.prototype, "schemeType", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "trimXAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "trimYAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "rotateXAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Number) ], BarHorizontalNormalizedComponent.prototype, "maxXAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Number) ], BarHorizontalNormalizedComponent.prototype, "maxYAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "xAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "yAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Array) ], BarHorizontalNormalizedComponent.prototype, "xAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Array) ], BarHorizontalNormalizedComponent.prototype, "yAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontalNormalizedComponent.prototype, "barPadding", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "roundDomains", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontalNormalizedComponent.prototype, "noBarWhenZero", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], BarHorizontalNormalizedComponent.prototype, "activate", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], BarHorizontalNormalizedComponent.prototype, "deactivate", void 0); __decorate([ ContentChild('tooltipTemplate', { static: false }), __metadata("design:type", TemplateRef) ], BarHorizontalNormalizedComponent.prototype, "tooltipTemplate", void 0); BarHorizontalNormalizedComponent = __decorate([ Component({ selector: 'ngx-charts-bar-horizontal-normalized', template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n [animations]=\"animations\"\n (legendLabelActivate)=\"onActivate($event, undefined, true)\"\n (legendLabelDeactivate)=\"onDeactivate($event, undefined, true)\"\n (legendLabelClick)=\"onClick($event)\"\n >\n <svg:g [attr.transform]=\"transform\" class=\"bar-chart chart\">\n <svg:g\n ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n [trimTicks]=\"trimXAxisTicks\"\n [rotateTicks]=\"rotateXAxisTicks\"\n [maxTickLength]=\"maxXAxisTickLength\"\n [tickFormatting]=\"xAxisTickFormatting\"\n [ticks]=\"xAxisTicks\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\"\n ></svg:g>\n <svg:g\n ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [trimTicks]=\"trimYAxisTicks\"\n [maxTickLength]=\"maxYAxisTickLength\"\n [tickFormatting]=\"yAxisTickFormatting\"\n [ticks]=\"yAxisTicks\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"\n ></svg:g>\n <svg:g\n *ngFor=\"let group of results; trackBy: trackBy\"\n [@animationState]=\"'active'\"\n [attr.transform]=\"groupTransform(group)\"\n >\n <svg:g\n ngx-charts-series-horizontal\n type=\"normalized\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [activeEntries]=\"activeEntries\"\n [colors]=\"colors\"\n [series]=\"group.series\"\n [dims]=\"dims\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [seriesName]=\"group.name\"\n [animations]=\"animations\"\n (select)=\"onClick($event, group)\"\n (activate)=\"onActivate($event, group)\"\n (deactivate)=\"onDeactivate($event, group)\"\n [noBarWhenZero]=\"noBarWhenZero\"\n />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ", changeDetection: ChangeDetectionStrategy.OnPush, styleUrls: ['../common/base-chart.component.css'], encapsulation: ViewEncapsulation.None, animations: [ trigger('animationState', [ transition(':leave', [ style({ opacity: 1, transform: '*' }), animate(500, style({ opacity: 0, transform: 'scale(0)' })) ]) ]) ] }) ], BarHorizontalNormalizedComponent); return BarHorizontalNormalizedComponent; }(BaseChartComponent)); export { BarHorizontalNormalizedComponent }; //# sourceMappingURL=bar-horizontal-normalized.component.js.map