@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
370 lines • 17.3 kB
JavaScript
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 __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
import { Component, Input, Output, EventEmitter, ViewEncapsulation, ChangeDetectionStrategy, ContentChild } from '@angular/core';
import { trigger, style, animate, transition } from '@angular/animations';
import { scaleLinear, scaleTime, scalePoint } from 'd3-scale';
import { curveCardinalClosed } from 'd3-shape';
import { calculateViewDimensions } from '../common/view-dimensions.helper';
import { ColorHelper } from '../common/color.helper';
import { BaseChartComponent } from '../common/base-chart.component';
import { isDate, isNumber } from '../utils/types';
var twoPI = 2 * Math.PI;
var PolarChartComponent = (function (_super) {
__extends(PolarChartComponent, _super);
function PolarChartComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.legendTitle = 'Legend';
_this.showGridLines = true;
_this.curve = curveCardinalClosed;
_this.activeEntries = [];
_this.rangeFillOpacity = 0.15;
_this.roundDomains = false;
_this.tooltipDisabled = false;
_this.showSeriesOnHover = true;
_this.gradient = false;
_this.activate = new EventEmitter();
_this.deactivate = new EventEmitter();
_this.margin = [10, 20, 10, 20];
_this.xAxisHeight = 0;
_this.yAxisWidth = 0;
return _this;
}
PolarChartComponent.prototype.update = function () {
_super.prototype.update.call(this);
this.setDims();
this.setScales();
this.setColors();
this.legendOptions = this.getLegendOptions();
this.setTicks();
};
PolarChartComponent.prototype.setDims = function () {
this.dims = calculateViewDimensions({
width: this.width,
height: this.height,
margins: this.margin,
showXAxis: this.xAxis,
showYAxis: this.yAxis,
xAxisHeight: this.xAxisHeight,
yAxisWidth: this.yAxisWidth,
showXLabel: this.showXAxisLabel,
showYLabel: this.showYAxisLabel,
showLegend: this.legend,
legendType: this.schemeType
});
var halfWidth = ~~(this.dims.width / 2);
var halfHeight = ~~(this.dims.height / 2);
var outerRadius = this.outerRadius = Math.min(halfHeight / 1.5, halfWidth / 1.5);
var yOffset = Math.max(0, halfHeight - outerRadius);
this.yAxisDims = __assign({}, this.dims, { width: halfWidth });
this.transform = "translate(" + this.dims.xOffset + ", " + this.margin[0] + ")";
this.transformYAxis = "translate(0, " + yOffset + ")";
this.labelOffset = this.dims.height + 40;
this.transformPlot = "translate(" + halfWidth + ", " + halfHeight + ")";
};
PolarChartComponent.prototype.setScales = function () {
var xValues = this.getXValues();
this.scaleType = this.getScaleType(xValues);
this.xDomain = this.filteredDomain || this.getXDomain(xValues);
this.yDomain = this.getYDomain();
this.seriesDomain = this.getSeriesDomain();
this.xScale = this.getXScale(this.xDomain, twoPI);
this.yScale = this.getYScale(this.yDomain, this.outerRadius);
this.yAxisScale = this.getYScale(this.yDomain.reverse(), this.outerRadius);
};
PolarChartComponent.prototype.setTicks = function () {
var _this = this;
var tickFormat;
if (this.xAxisTickFormatting) {
tickFormat = this.xAxisTickFormatting;
}
else if (this.xScale.tickFormat) {
tickFormat = this.xScale.tickFormat.apply(this.xScale, [5]);
}
else {
tickFormat = function (d) {
if (isDate(d)) {
return d.toLocaleDateString();
}
return d.toLocaleString();
};
}
var outerRadius = this.outerRadius;
var s = 1.1;
this.thetaTicks = this.xDomain.map(function (d) {
var startAngle = _this.xScale(d);
var dd = s * outerRadius * (startAngle > Math.PI ? -1 : 1);
var label = tickFormat(d);
var startPos = [outerRadius * Math.sin(startAngle), -outerRadius * Math.cos(startAngle)];
var pos = [dd, s * startPos[1]];
return {
innerRadius: 0,
outerRadius: outerRadius,
startAngle: startAngle,
endAngle: startAngle,
value: outerRadius,
label: label,
startPos: startPos,
pos: pos
};
});
var minDistance = 10;
/* from pie chart, abstract out -*/
for (var i = 0; i < this.thetaTicks.length - 1; i++) {
var a = this.thetaTicks[i];
for (var j = i + 1; j < this.thetaTicks.length; j++) {
var b = this.thetaTicks[j];
// if they're on the same side
if (b.pos[0] * a.pos[0] > 0) {
// if they're overlapping
var o = minDistance - Math.abs(b.pos[1] - a.pos[1]);
if (o > 0) {
// push the second up or down
b.pos[1] += Math.sign(b.pos[0]) * o;
}
}
}
}
this.radiusTicks = this.yAxisScale
.ticks(~~(this.dims.height / 50))
.map(function (d) { return _this.yScale(d); });
};
PolarChartComponent.prototype.getXValues = function () {
var values = [];
for (var _i = 0, _a = this.results; _i < _a.length; _i++) {
var results = _a[_i];
for (var _b = 0, _c = results.series; _b < _c.length; _b++) {
var d = _c[_b];
if (!values.includes(d.name)) {
values.push(d.name);
}
}
}
return values;
};
PolarChartComponent.prototype.getXDomain = function (values) {
if (values === void 0) { values = this.getXValues(); }
if (this.scaleType === 'time') {
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
return [min, max];
}
else if (this.scaleType === 'linear') {
values = values.map(function (v) { return Number(v); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
return [min, max];
}
return values;
};
PolarChartComponent.prototype.getYValues = function () {
var domain = [];
for (var _i = 0, _a = this.results; _i < _a.length; _i++) {
var results = _a[_i];
for (var _b = 0, _c = results.series; _b < _c.length; _b++) {
var d = _c[_b];
if (domain.indexOf(d.value) < 0) {
domain.push(d.value);
}
if (d.min !== undefined) {
if (domain.indexOf(d.min) < 0) {
domain.push(d.min);
}
}
if (d.max !== undefined) {
if (domain.indexOf(d.max) < 0) {
domain.push(d.max);
}
}
}
}
return domain;
};
PolarChartComponent.prototype.getYDomain = function (domain) {
if (domain === void 0) { domain = this.getYValues(); }
var min = Math.min.apply(Math, domain);
var max = Math.max.apply(Math, domain);
min = Math.max(0, min);
if (!this.autoScale) {
min = Math.min(0, min);
}
return [min, max];
};
PolarChartComponent.prototype.getSeriesDomain = function () {
return this.results.map(function (d) { return d.name; });
};
PolarChartComponent.prototype.getXScale = function (domain, width) {
switch (this.scaleType) {
case 'time':
return scaleTime()
.range([0, width])
.domain(domain);
case 'linear':
var scale = scaleLinear()
.range([0, width])
.domain(domain);
return this.roundDomains ? scale.nice() : scale;
default:
return scalePoint()
.range([0, width - twoPI / domain.length])
.padding(0)
.domain(domain);
}
};
PolarChartComponent.prototype.getYScale = function (domain, height) {
var scale = scaleLinear()
.range([0, height])
.domain(domain);
return this.roundDomains ? scale.nice() : scale;
};
PolarChartComponent.prototype.getScaleType = function (values) {
var date = true;
var num = true;
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
var value = values_1[_i];
if (!isDate(value)) {
date = false;
}
if (!isNumber(value)) {
num = false;
}
}
if (date)
return 'time';
if (num)
return 'linear';
return 'ordinal';
};
PolarChartComponent.prototype.onClick = function (data, series) {
if (series) {
data.series = series.name;
}
this.select.emit(data);
};
PolarChartComponent.prototype.setColors = function () {
var domain = (this.schemeType === 'ordinal') ?
this.seriesDomain :
this.yDomain.reverse();
this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors);
};
PolarChartComponent.prototype.getLegendOptions = function () {
if (this.schemeType === 'ordinal') {
return {
scaleType: this.schemeType,
colors: this.colors,
domain: this.seriesDomain,
title: this.legendTitle
};
}
return {
scaleType: this.schemeType,
colors: this.colors.scale,
domain: this.yDomain,
title: undefined
};
};
PolarChartComponent.prototype.updateYAxisWidth = function (_a) {
var width = _a.width;
this.yAxisWidth = width;
this.update();
};
PolarChartComponent.prototype.updateXAxisHeight = function (_a) {
var height = _a.height;
this.xAxisHeight = height;
this.update();
};
PolarChartComponent.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 = this.showSeriesOnHover ? [item].concat(this.activeEntries) : this.activeEntries;
this.activate.emit({ value: item, entries: this.activeEntries });
};
PolarChartComponent.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 });
};
PolarChartComponent.prototype.deactivateAll = function () {
this.activeEntries = this.activeEntries.slice();
for (var _i = 0, _a = this.activeEntries; _i < _a.length; _i++) {
var entry = _a[_i];
this.deactivate.emit({ value: entry, entries: [] });
}
this.activeEntries = [];
};
PolarChartComponent.prototype.trackBy = function (index, item) {
return item.name;
};
return PolarChartComponent;
}(BaseChartComponent));
export { PolarChartComponent };
PolarChartComponent.decorators = [
{ type: Component, args: [{
selector: 'ngx-charts-polar-chart',
template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\">\n <svg:g class=\"polar-chart chart\" [attr.transform]=\"transform\">\n <svg:g [attr.transform]=\"transformPlot\">\n <svg:circle\n class=\"polar-chart-background\"\n cx=\"0\" cy=\"0\"\n [attr.r]=\"this.outerRadius\" />\n <svg:g *ngIf=\"showGridLines\">\n <svg:circle\n *ngFor=\"let r of radiusTicks\"\n class=\"gridline-path radial-gridline-path\"\n cx=\"0\" cy=\"0\"\n [attr.r]=\"r\" />\n </svg:g>\n <svg:g *ngIf=\"xAxis\">\n <svg:g ngx-charts-pie-label\n *ngFor=\"let tick of thetaTicks\"\n [data]=\"tick\"\n [radius]=\"outerRadius\"\n [label]=\"tick.label\"\n [max]=\"outerRadius\"\n [value]=\"showGridLines ? 1 : outerRadius\"\n [explodeSlices]=\"true\">\n </svg:g>\n </svg:g>\n </svg:g>\n <svg:g ngx-charts-y-axis\n [attr.transform]=\"transformYAxis\"\n *ngIf=\"yAxis\"\n [yScale]=\"yAxisScale\"\n [dims]=\"yAxisDims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [tickFormatting]=\"yAxisTickFormatting\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\">\n </svg:g>\n <svg:g ngx-charts-axis-label\n *ngIf=\"xAxis && showXAxisLabel\"\n [label]=\"xAxisLabel\"\n [offset]=\"labelOffset\"\n [orient]=\"'bottom'\"\n [height]=\"dims.height\"\n [width]=\"dims.width\">\n </svg:g>\n <svg:g [attr.transform]=\"transformPlot\">\n <svg:g *ngFor=\"let series of results; trackBy:trackBy\" [@animationState]=\"'active'\">\n <svg:g ngx-charts-polar-series\n [gradient]=\"gradient\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [activeEntries]=\"activeEntries\"\n [scaleType]=\"scaleType\"\n [curve]=\"curve\"\n [rangeFillOpacity]=\"rangeFillOpacity\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n />\n </svg:g>\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ",
styleUrls: [
'../common/base-chart.component.css',
'../pie-chart/pie-chart.component.css',
'./polar-chart.component.css'
],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
animations: [
trigger('animationState', [
transition(':leave', [
style({
opacity: 1,
}),
animate(500, style({
opacity: 0
}))
])
])
]
},] },
];
/** @nocollapse */
PolarChartComponent.ctorParameters = function () { return []; };
PolarChartComponent.propDecorators = {
'legend': [{ type: Input },],
'legendTitle': [{ type: Input },],
'xAxis': [{ type: Input },],
'yAxis': [{ type: Input },],
'showXAxisLabel': [{ type: Input },],
'showYAxisLabel': [{ type: Input },],
'xAxisLabel': [{ type: Input },],
'yAxisLabel': [{ type: Input },],
'autoScale': [{ type: Input },],
'showGridLines': [{ type: Input },],
'curve': [{ type: Input },],
'activeEntries': [{ type: Input },],
'schemeType': [{ type: Input },],
'rangeFillOpacity': [{ type: Input },],
'xAxisTickFormatting': [{ type: Input },],
'yAxisTickFormatting': [{ type: Input },],
'roundDomains': [{ type: Input },],
'tooltipDisabled': [{ type: Input },],
'showSeriesOnHover': [{ type: Input },],
'gradient': [{ type: Input },],
'activate': [{ type: Output },],
'deactivate': [{ type: Output },],
'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },],
};
//# sourceMappingURL=polar-chart.component.js.map