@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
225 lines • 10.2 kB
JavaScript
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();
_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,
legendPosition: this.legendPosition
});
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,
position: this.legendPosition
};
};
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 });
};
__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([
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'),
__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)\"\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 [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