UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular

407 lines 19.2 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, ViewEncapsulation, Output, 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 BarHorizontal2DComponent = /** @class */ (function (_super) { __extends(BarHorizontal2DComponent, _super); function BarHorizontal2DComponent() { 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.groupPadding = 16; _this.barPadding = 8; _this.roundDomains = false; _this.roundEdges = true; _this.showDataLabel = false; _this.noBarWhenZero = true; _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); _this.margin = [10, 20, 10, 20]; _this.xAxisHeight = 0; _this.yAxisWidth = 0; _this.dataLabelMaxWidth = { negative: 0, positive: 0 }; return _this; } BarHorizontal2DComponent.prototype.update = function () { _super.prototype.update.call(this); if (!this.showDataLabel) { this.dataLabelMaxWidth = { negative: 0, positive: 0 }; } this.margin = [10, 20 + this.dataLabelMaxWidth.positive, 10, 20 + this.dataLabelMaxWidth.negative]; 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.valuesDomain = this.getValueDomain(); this.groupScale = this.getGroupScale(); this.innerScale = this.getInnerScale(); this.valueScale = this.getValueScale(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")"; }; BarHorizontal2DComponent.prototype.getGroupScale = function () { var spacing = this.groupDomain.length / (this.dims.height / this.groupPadding + 1); return scaleBand() .rangeRound([0, this.dims.height]) .paddingInner(spacing) .paddingOuter(spacing / 2) .domain(this.groupDomain); }; BarHorizontal2DComponent.prototype.getInnerScale = function () { var height = this.groupScale.bandwidth(); var spacing = this.innerDomain.length / (height / this.barPadding + 1); return scaleBand() .rangeRound([0, height]) .paddingInner(spacing) .domain(this.innerDomain); }; BarHorizontal2DComponent.prototype.getValueScale = function () { var scale = scaleLinear() .range([0, this.dims.width]) .domain(this.valuesDomain); return this.roundDomains ? scale.nice() : scale; }; BarHorizontal2DComponent.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; }; BarHorizontal2DComponent.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; }; BarHorizontal2DComponent.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); } } } var min = Math.min.apply(Math, [0].concat(domain)); var max = this.xScaleMax ? Math.max.apply(Math, [this.xScaleMax].concat(domain)) : Math.max.apply(Math, [0].concat(domain)); return [min, max]; }; BarHorizontal2DComponent.prototype.groupTransform = function (group) { return "translate(0, " + this.groupScale(group.label) + ")"; }; BarHorizontal2DComponent.prototype.onClick = function (data, group) { if (group) { data.series = group.name; } this.select.emit(data); }; BarHorizontal2DComponent.prototype.trackBy = function (index, item) { return item.name; }; BarHorizontal2DComponent.prototype.setColors = function () { var domain; if (this.schemeType === 'ordinal') { domain = this.innerDomain; } else { domain = this.valuesDomain; } this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors); }; BarHorizontal2DComponent.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.valuesDomain; opts.colors = this.colors.scale; } return opts; }; BarHorizontal2DComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; BarHorizontal2DComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; BarHorizontal2DComponent.prototype.onDataLabelMaxWidthChanged = function (event, groupIndex) { var _this = this; if (event.size.negative) { this.dataLabelMaxWidth.negative = Math.max(this.dataLabelMaxWidth.negative, event.size.width); } else { this.dataLabelMaxWidth.positive = Math.max(this.dataLabelMaxWidth.positive, event.size.width); } if (groupIndex === this.results.length - 1) { setTimeout(function () { return _this.update(); }); } }; BarHorizontal2DComponent.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 }); }; BarHorizontal2DComponent.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) ], BarHorizontal2DComponent.prototype, "legend", void 0); __decorate([ Input(), __metadata("design:type", String) ], BarHorizontal2DComponent.prototype, "legendTitle", void 0); __decorate([ Input(), __metadata("design:type", String) ], BarHorizontal2DComponent.prototype, "legendPosition", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "xAxis", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "yAxis", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "showXAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "showYAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "xAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "yAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "tooltipDisabled", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "gradient", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "showGridLines", void 0); __decorate([ Input(), __metadata("design:type", Array) ], BarHorizontal2DComponent.prototype, "activeEntries", void 0); __decorate([ Input(), __metadata("design:type", String) ], BarHorizontal2DComponent.prototype, "schemeType", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "trimXAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "trimYAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "rotateXAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Number) ], BarHorizontal2DComponent.prototype, "maxXAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Number) ], BarHorizontal2DComponent.prototype, "maxYAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "xAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "yAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Array) ], BarHorizontal2DComponent.prototype, "xAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Array) ], BarHorizontal2DComponent.prototype, "yAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "groupPadding", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "barPadding", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "roundDomains", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "roundEdges", void 0); __decorate([ Input(), __metadata("design:type", Number) ], BarHorizontal2DComponent.prototype, "xScaleMax", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "showDataLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], BarHorizontal2DComponent.prototype, "dataLabelFormatting", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], BarHorizontal2DComponent.prototype, "noBarWhenZero", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], BarHorizontal2DComponent.prototype, "activate", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], BarHorizontal2DComponent.prototype, "deactivate", void 0); __decorate([ ContentChild('tooltipTemplate', { static: false }), __metadata("design:type", TemplateRef) ], BarHorizontal2DComponent.prototype, "tooltipTemplate", void 0); BarHorizontal2DComponent = __decorate([ Component({ selector: 'ngx-charts-bar-horizontal-2d', 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-grid-panel-series\n [xScale]=\"valueScale\"\n [yScale]=\"groupScale\"\n [data]=\"results\"\n [dims]=\"dims\"\n orient=\"horizontal\"\n ></svg:g>\n <svg:g\n ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"valueScale\"\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]=\"groupScale\"\n [dims]=\"dims\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [trimTicks]=\"trimYAxisTicks\"\n [maxTickLength]=\"maxYAxisTickLength\"\n [tickFormatting]=\"yAxisTickFormatting\"\n [ticks]=\"yAxisTicks\"\n [yAxisOffset]=\"dataLabelMaxWidth.negative\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"\n ></svg:g>\n <svg:g\n *ngFor=\"let group of results; let index = index; trackBy: trackBy\"\n [@animationState]=\"'active'\"\n [attr.transform]=\"groupTransform(group)\"\n >\n <svg:g\n ngx-charts-series-horizontal\n [xScale]=\"valueScale\"\n [activeEntries]=\"activeEntries\"\n [yScale]=\"innerScale\"\n [colors]=\"colors\"\n [series]=\"group.series\"\n [dims]=\"dims\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [seriesName]=\"group.name\"\n [roundEdges]=\"roundEdges\"\n [animations]=\"animations\"\n [showDataLabel]=\"showDataLabel\"\n [dataLabelFormatting]=\"dataLabelFormatting\"\n [noBarWhenZero]=\"noBarWhenZero\"\n (select)=\"onClick($event, group)\"\n (activate)=\"onActivate($event, group)\"\n (deactivate)=\"onDeactivate($event, group)\"\n (dataLabelWidthChanged)=\"onDataLabelMaxWidthChanged($event, index)\"\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)' })) ]) ]) ] }) ], BarHorizontal2DComponent); return BarHorizontal2DComponent; }(BaseChartComponent)); export { BarHorizontal2DComponent }; //# sourceMappingURL=bar-horizontal-2d.component.js.map