UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

145 lines 6.75 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, Output, ViewEncapsulation, EventEmitter, ChangeDetectionStrategy, ContentChild } 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 = (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.explodeSlices = false; _this.doughnut = false; _this.arcWidth = 0.25; _this.activeEntries = []; _this.tooltipDisabled = false; _this.select = new EventEmitter(); _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); _this.margin = [20, 20, 20, 20]; return _this; } PieChartComponent.prototype.update = function () { var _this = this; _super.prototype.update.call(this); if (this.labels) { this.margin = [30, 80, 30, 80]; } this.dims = calculateViewDimensions({ width: this.width, height: this.height, margins: this.margin, showLegend: this.legend, }); var xOffset = this.margin[3] + this.dims.width / 2; var yOffset = this.margin[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 () { var items = []; this.results.map(function (d) { var label = d.name; if (label.constructor.name === 'Date') { label = label.toLocaleDateString(); } else { label = label.toLocaleString(); } if (items.indexOf(label) === -1) { items.push(label); } }); return items; }; 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 }; }; PieChartComponent.prototype.onActivate = function (item) { var idx = this.activeEntries.findIndex(function (d) { return d.name === item.name && d.value === item.value; }); if (idx > -1) { return; } this.activeEntries = [item].concat(this.activeEntries); this.activate.emit({ value: item, entries: this.activeEntries }); }; PieChartComponent.prototype.onDeactivate = function (item) { var idx = this.activeEntries.findIndex(function (d) { return d.name === item.name && d.value === item.value; }); this.activeEntries.splice(idx, 1); this.activeEntries = this.activeEntries.slice(); this.deactivate.emit({ value: item, entries: this.activeEntries }); }; return PieChartComponent; }(BaseChartComponent)); export { PieChartComponent }; PieChartComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-charts-pie-chart', template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\"\n (legendLabelClick)=\"onClick($event)\">\n <svg:g [attr.transform]=\"translation\" class=\"pie-chart chart\">\n <svg:g ngx-charts-pie-series\n [colors]=\"colors\"\n [showLabels]=\"labels\"\n [labelFormatting]=\"labelFormatting\"\n [series]=\"data\"\n [activeEntries]=\"activeEntries\"\n [innerRadius]=\"innerRadius\"\n [outerRadius]=\"outerRadius\"\n [explodeSlices]=\"explodeSlices\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [tooltipText]=\"tooltipText\"\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 },] }, ]; /** @nocollapse */ PieChartComponent.ctorParameters = function () { return []; }; PieChartComponent.propDecorators = { 'labels': [{ type: Input },], 'legend': [{ type: Input },], 'legendTitle': [{ type: Input },], 'explodeSlices': [{ type: Input },], 'doughnut': [{ type: Input },], 'arcWidth': [{ type: Input },], 'gradient': [{ type: Input },], 'activeEntries': [{ type: Input },], 'tooltipDisabled': [{ type: Input },], 'labelFormatting': [{ type: Input },], 'tooltipText': [{ type: Input },], 'select': [{ type: Output },], 'activate': [{ type: Output },], 'deactivate': [{ type: Output },], 'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },], }; //# sourceMappingURL=pie-chart.component.js.map