UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular

238 lines 10.9 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 { calculateViewDimensions } from '../common/view-dimensions.helper'; import { ColorHelper } from '../common/color.helper'; import { BaseChartComponent } from '../common/base-chart.component'; var PieChartComponent = /** @class */ (function (_super) { __extends(PieChartComponent, _super); function PieChartComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.labels = false; _this.legend = false; _this.legendTitle = 'Legend'; _this.legendPosition = 'right'; _this.explodeSlices = false; _this.doughnut = false; _this.arcWidth = 0.25; _this.activeEntries = []; _this.tooltipDisabled = false; _this.trimLabels = true; _this.maxLabelLength = 10; _this.dblclick = new EventEmitter(); _this.select = new EventEmitter(); _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); return _this; } PieChartComponent.prototype.update = function () { var _this = this; _super.prototype.update.call(this); if (this.labels && this.hasNoOptionalMarginsSet()) { this.margins = [30, 80, 30, 80]; } else if (!this.labels && this.hasNoOptionalMarginsSet()) { // default value for margins this.margins = [20, 20, 20, 20]; } this.dims = calculateViewDimensions({ width: this.width, height: this.height, margins: this.margins, showLegend: this.legend, legendPosition: this.legendPosition }); this.formatDates(); var xOffset = this.margins[3] + this.dims.width / 2; var yOffset = this.margins[0] + this.dims.height / 2; this.translation = "translate(" + xOffset + ", " + yOffset + ")"; this.outerRadius = Math.min(this.dims.width, this.dims.height); if (this.labels) { // make room for labels this.outerRadius /= 3; } else { this.outerRadius /= 2; } this.innerRadius = 0; if (this.doughnut) { this.innerRadius = this.outerRadius * (1 - this.arcWidth); } this.domain = this.getDomain(); // sort data according to domain this.data = this.results.sort(function (a, b) { return _this.domain.indexOf(a.name) - _this.domain.indexOf(b.name); }); this.setColors(); this.legendOptions = this.getLegendOptions(); }; PieChartComponent.prototype.getDomain = function () { return this.results.map(function (d) { return d.label; }); }; PieChartComponent.prototype.onClick = function (data) { this.select.emit(data); }; PieChartComponent.prototype.setColors = function () { this.colors = new ColorHelper(this.scheme, 'ordinal', this.domain, this.customColors); }; PieChartComponent.prototype.getLegendOptions = function () { return { scaleType: 'ordinal', domain: this.domain, colors: this.colors, title: this.legendTitle, position: this.legendPosition }; }; PieChartComponent.prototype.onActivate = function (item, fromLegend) { if (fromLegend === void 0) { fromLegend = false; } item = this.results.find(function (d) { if (fromLegend) { return d.label === item.name; } else { return d.name === item.name; } }); var idx = this.activeEntries.findIndex(function (d) { return d.name === item.name && d.value === item.value && d.series === item.series; }); if (idx > -1) { return; } this.activeEntries = [item].concat(this.activeEntries); this.activate.emit({ value: item, entries: this.activeEntries }); }; PieChartComponent.prototype.onDeactivate = function (item, fromLegend) { if (fromLegend === void 0) { fromLegend = false; } item = this.results.find(function (d) { if (fromLegend) { return d.label === item.name; } else { return d.name === item.name; } }); var idx = this.activeEntries.findIndex(function (d) { return d.name === item.name && d.value === item.value && d.series === item.series; }); this.activeEntries.splice(idx, 1); this.activeEntries = this.activeEntries.slice(); this.deactivate.emit({ value: item, entries: this.activeEntries }); }; PieChartComponent.prototype.hasNoOptionalMarginsSet = function () { return !this.margins || this.margins.length <= 0; }; __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "labels", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "legend", void 0); __decorate([ Input(), __metadata("design:type", String) ], PieChartComponent.prototype, "legendTitle", void 0); __decorate([ Input(), __metadata("design:type", String) ], PieChartComponent.prototype, "legendPosition", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "explodeSlices", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "doughnut", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "arcWidth", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PieChartComponent.prototype, "gradient", void 0); __decorate([ Input(), __metadata("design:type", Array) ], PieChartComponent.prototype, "activeEntries", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PieChartComponent.prototype, "tooltipDisabled", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "labelFormatting", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PieChartComponent.prototype, "trimLabels", void 0); __decorate([ Input(), __metadata("design:type", Number) ], PieChartComponent.prototype, "maxLabelLength", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PieChartComponent.prototype, "tooltipText", void 0); __decorate([ Output(), __metadata("design:type", Object) ], PieChartComponent.prototype, "dblclick", void 0); __decorate([ Input(), __metadata("design:type", Array) ], PieChartComponent.prototype, "margins", void 0); __decorate([ Output(), __metadata("design:type", Object) ], PieChartComponent.prototype, "select", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], PieChartComponent.prototype, "activate", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], PieChartComponent.prototype, "deactivate", void 0); __decorate([ ContentChild('tooltipTemplate', { static: false }), __metadata("design:type", TemplateRef) ], PieChartComponent.prototype, "tooltipTemplate", void 0); PieChartComponent = __decorate([ Component({ selector: 'ngx-charts-pie-chart', 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, true)\"\n (legendLabelDeactivate)=\"onDeactivate($event, true)\"\n (legendLabelClick)=\"onClick($event)\"\n >\n <svg:g [attr.transform]=\"translation\" class=\"pie-chart chart\">\n <svg:g\n ngx-charts-pie-series\n [colors]=\"colors\"\n [series]=\"data\"\n [showLabels]=\"labels\"\n [labelFormatting]=\"labelFormatting\"\n [trimLabels]=\"trimLabels\"\n [maxLabelLength]=\"maxLabelLength\"\n [activeEntries]=\"activeEntries\"\n [innerRadius]=\"innerRadius\"\n [outerRadius]=\"outerRadius\"\n [explodeSlices]=\"explodeSlices\"\n [gradient]=\"gradient\"\n [animations]=\"animations\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [tooltipText]=\"tooltipText\"\n (dblclick)=\"dblclick.emit($event)\"\n (select)=\"onClick($event)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n />\n </svg:g>\n </ngx-charts-chart>\n ", styleUrls: ['../common/base-chart.component.css', './pie-chart.component.css'], encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush }) ], PieChartComponent); return PieChartComponent; }(BaseChartComponent)); export { PieChartComponent }; //# sourceMappingURL=pie-chart.component.js.map