UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

273 lines 12.7 kB
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, EventEmitter, ChangeDetectionStrategy, TemplateRef } from '@angular/core'; import { trigger, style, animate, transition } from '@angular/animations'; import { formatLabel } from '../common/label.helper'; import { D0Types } from './series-vertical.component'; var SeriesHorizontal = /** @class */ (function () { function SeriesHorizontal() { this.barsForDataLabels = []; this.type = 'standard'; this.tooltipDisabled = false; this.animations = true; this.showDataLabel = false; this.select = new EventEmitter(); this.activate = new EventEmitter(); this.deactivate = new EventEmitter(); this.dataLabelWidthChanged = new EventEmitter(); } SeriesHorizontal.prototype.ngOnChanges = function (changes) { this.update(); }; SeriesHorizontal.prototype.update = function () { var _this = this; var _a; this.updateTooltipSettings(); var d0 = (_a = {}, _a[D0Types.positive] = 0, _a[D0Types.negative] = 0, _a); var d0Type; d0Type = D0Types.positive; var total; if (this.type === 'normalized') { total = this.series.map(function (d) { return d.value; }).reduce(function (sum, d) { return sum + d; }, 0); } var xScaleMin = Math.max(this.xScale.domain()[0], 0); this.bars = this.series.map(function (d, index) { var value = d.value; var label = d.name; var formattedLabel = formatLabel(label); var roundEdges = _this.roundEdges; d0Type = value > 0 ? D0Types.positive : D0Types.negative; var bar = { value: value, label: label, roundEdges: roundEdges, data: d, formattedLabel: formattedLabel }; bar.height = _this.yScale.bandwidth(); if (_this.type === 'standard') { bar.width = Math.abs(_this.xScale(value) - _this.xScale(xScaleMin)); if (value < 0) { bar.x = _this.xScale(value); } else { bar.x = _this.xScale(xScaleMin); } bar.y = _this.yScale(label); } else if (_this.type === 'stacked') { var offset0 = d0[d0Type]; var offset1 = offset0 + value; d0[d0Type] += value; bar.width = _this.xScale(offset1) - _this.xScale(offset0); bar.x = _this.xScale(offset0); bar.y = 0; bar.offset0 = offset0; bar.offset1 = offset1; } else if (_this.type === 'normalized') { var offset0 = d0[d0Type]; var offset1 = offset0 + value; d0[d0Type] += value; if (total > 0) { offset0 = (offset0 * 100) / total; offset1 = (offset1 * 100) / total; } else { offset0 = 0; offset1 = 0; } bar.width = _this.xScale(offset1) - _this.xScale(offset0); bar.x = _this.xScale(offset0); bar.y = 0; bar.offset0 = offset0; bar.offset1 = offset1; value = (offset1 - offset0).toFixed(2) + '%'; } if (_this.colors.scaleType === 'ordinal') { bar.color = _this.colors.getColor(label); } else { if (_this.type === 'standard') { bar.color = _this.colors.getColor(value); bar.gradientStops = _this.colors.getLinearGradientStops(value); } else { bar.color = _this.colors.getColor(bar.offset1); bar.gradientStops = _this.colors.getLinearGradientStops(bar.offset1, bar.offset0); } } var tooltipLabel = formattedLabel; bar.ariaLabel = formattedLabel + ' ' + value.toLocaleString(); if (_this.seriesName) { tooltipLabel = _this.seriesName + " \u2022 " + formattedLabel; bar.data.series = _this.seriesName; bar.ariaLabel = _this.seriesName + ' ' + bar.ariaLabel; } bar.tooltipText = _this.tooltipDisabled ? undefined : "\n <span class=\"tooltip-label\">" + tooltipLabel + "</span>\n <span class=\"tooltip-val\">" + value.toLocaleString() + "</span>\n "; return bar; }); this.updateDataLabels(); }; SeriesHorizontal.prototype.updateDataLabels = function () { var _this = this; if (this.type === 'stacked') { this.barsForDataLabels = []; var section = {}; section.series = this.seriesName; var totalPositive = this.series.map(function (d) { return d.value; }).reduce(function (sum, d) { return d > 0 ? sum + d : sum; }, 0); var totalNegative = this.series.map(function (d) { return d.value; }).reduce(function (sum, d) { return d < 0 ? sum + d : sum; }, 0); section.total = totalPositive + totalNegative; section.x = 0; section.y = 0; // if total is positive then we show it on the right, otherwise on the left if (section.total > 0) { section.width = this.xScale(totalPositive); } else { section.width = this.xScale(totalNegative); } section.height = this.yScale.bandwidth(); this.barsForDataLabels.push(section); } else { this.barsForDataLabels = this.series.map(function (d) { var section = {}; section.series = _this.seriesName ? _this.seriesName : d.name; section.total = d.value; section.x = _this.xScale(0); section.y = _this.yScale(d.name); section.width = _this.xScale(section.total) - _this.xScale(0); section.height = _this.yScale.bandwidth(); return section; }); } }; SeriesHorizontal.prototype.updateTooltipSettings = function () { this.tooltipPlacement = this.tooltipDisabled ? undefined : 'top'; this.tooltipType = this.tooltipDisabled ? undefined : 'tooltip'; }; SeriesHorizontal.prototype.isActive = function (entry) { if (!this.activeEntries) return false; var item = this.activeEntries.find(function (d) { return entry.name === d.name && entry.series === d.series; }); return item !== undefined; }; SeriesHorizontal.prototype.trackBy = function (index, bar) { return bar.label; }; SeriesHorizontal.prototype.trackDataLabelBy = function (index, barLabel) { return index + '#' + barLabel.series + '#' + barLabel.total; }; SeriesHorizontal.prototype.click = function (data) { this.select.emit(data); }; __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "dims", void 0); __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "type", void 0); __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "series", void 0); __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "xScale", void 0); __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "yScale", void 0); __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "colors", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], SeriesHorizontal.prototype, "tooltipDisabled", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], SeriesHorizontal.prototype, "gradient", void 0); __decorate([ Input(), __metadata("design:type", Array) ], SeriesHorizontal.prototype, "activeEntries", void 0); __decorate([ Input(), __metadata("design:type", String) ], SeriesHorizontal.prototype, "seriesName", void 0); __decorate([ Input(), __metadata("design:type", TemplateRef) ], SeriesHorizontal.prototype, "tooltipTemplate", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], SeriesHorizontal.prototype, "roundEdges", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], SeriesHorizontal.prototype, "animations", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], SeriesHorizontal.prototype, "showDataLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "dataLabelFormatting", void 0); __decorate([ Output(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "select", void 0); __decorate([ Output(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "activate", void 0); __decorate([ Output(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "deactivate", void 0); __decorate([ Output(), __metadata("design:type", Object) ], SeriesHorizontal.prototype, "dataLabelWidthChanged", void 0); SeriesHorizontal = __decorate([ Component({ selector: 'g[ngx-charts-series-horizontal]', template: "\n <svg:g ngx-charts-bar\n *ngFor=\"let bar of bars; trackBy:trackBy\"\n [@animationState]=\"'active'\"\n [width]=\"bar.width\"\n [height]=\"bar.height\"\n [x]=\"bar.x\"\n [y]=\"bar.y\"\n [fill]=\"bar.color\"\n [stops]=\"bar.gradientStops\"\n [data]=\"bar.data\"\n [orientation]=\"'horizontal'\"\n [roundEdges]=\"bar.roundEdges\"\n (select)=\"click($event)\"\n [gradient]=\"gradient\"\n [isActive]=\"isActive(bar.data)\"\n [ariaLabel]=\"bar.ariaLabel\"\n [animations]=\"animations\"\n (activate)=\"activate.emit($event)\"\n (deactivate)=\"deactivate.emit($event)\"\n ngx-tooltip\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipPlacement]=\"tooltipPlacement\"\n [tooltipType]=\"tooltipType\"\n [tooltipTitle]=\"tooltipTemplate ? undefined : bar.tooltipText\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [tooltipContext]=\"bar.data\">\n </svg:g>\n <svg:g *ngIf=\"showDataLabel\">\n <svg:g ngx-charts-bar-label *ngFor=\"let b of barsForDataLabels; let i = index; trackBy:trackDataLabelBy\" \n [barX]=\"b.x\"\n [barY]=\"b.y\"\n [barWidth]=\"b.width\"\n [barHeight]=\"b.height\"\n [value]=\"b.total\"\n [valueFormatting]=\"dataLabelFormatting\"\n [orientation]=\"'horizontal'\"\n (dimensionsChanged)=\"dataLabelWidthChanged.emit({size:$event, index:i})\" \n />\n </svg:g> \n ", changeDetection: ChangeDetectionStrategy.OnPush, animations: [ trigger('animationState', [ transition(':leave', [ style({ opacity: 1 }), animate(500, style({ opacity: 0 })) ]) ]) ] }) ], SeriesHorizontal); return SeriesHorizontal; }()); export { SeriesHorizontal }; //# sourceMappingURL=series-horizontal.component.js.map