@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
133 lines • 6.26 kB
JavaScript
"use strict";
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 __());
};
var core_1 = require('@angular/core');
var view_dimensions_helper_1 = require('../common/view-dimensions.helper');
var color_helper_1 = require('../common/color.helper');
var base_chart_component_1 = require('../common/base-chart.component');
var PieChartComponent = (function (_super) {
__extends(PieChartComponent, _super);
function PieChartComponent() {
_super.apply(this, arguments);
this.labels = false;
this.legend = false;
this.explodeSlices = false;
this.doughnut = false;
this.activeEntries = [];
this.select = new core_1.EventEmitter();
this.activate = new core_1.EventEmitter();
this.deactivate = new core_1.EventEmitter();
this.margin = [20, 20, 20, 20];
}
PieChartComponent.prototype.update = function () {
var _this = this;
_super.prototype.update.call(this);
this.zone.run(function () {
if (_this.labels) {
_this.margin = [30, 80, 30, 80];
}
_this.dims = view_dimensions_helper_1.calculateViewDimensions({
width: _this.width,
height: _this.height,
margins: _this.margin,
showLegend: _this.legend,
columns: 10
});
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 * 0.75;
}
_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 color_helper_1.ColorHelper(this.scheme, 'ordinal', this.domain, this.customColors);
};
PieChartComponent.prototype.getLegendOptions = function () {
return {
scaleType: 'ordinal',
domain: this.domain,
colors: this.colors
};
};
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: event, entries: this.activeEntries });
};
PieChartComponent.decorators = [
{ type: core_1.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 [series]=\"data\"\n [activeEntries]=\"activeEntries\"\n [innerRadius]=\"innerRadius\"\n [outerRadius]=\"outerRadius\"\n [explodeSlices]=\"explodeSlices\"\n [gradient]=\"gradient\"\n (select)=\"onClick($event)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n />\n </svg:g>\n </ngx-charts-chart>\n ",
changeDetection: core_1.ChangeDetectionStrategy.OnPush
},] },
];
/** @nocollapse */
PieChartComponent.ctorParameters = function () { return []; };
PieChartComponent.propDecorators = {
'labels': [{ type: core_1.Input },],
'legend': [{ type: core_1.Input },],
'explodeSlices': [{ type: core_1.Input },],
'doughnut': [{ type: core_1.Input },],
'gradient': [{ type: core_1.Input },],
'activeEntries': [{ type: core_1.Input },],
'select': [{ type: core_1.Output },],
'activate': [{ type: core_1.Output },],
'deactivate': [{ type: core_1.Output },],
};
return PieChartComponent;
}(base_chart_component_1.BaseChartComponent));
exports.PieChartComponent = PieChartComponent;
//# sourceMappingURL=pie-chart.component.js.map