UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular

467 lines 21.8 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 __assign = (this && this.__assign) || function () { __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; }; return __assign.apply(this, arguments); }; 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, EventEmitter, ViewEncapsulation, ChangeDetectionStrategy, ContentChild, TemplateRef } 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 { getScaleType } from '../common/domain.helper'; import { isDate } from '../utils/types'; var twoPI = 2 * Math.PI; var PolarChartComponent = /** @class */ (function (_super) { __extends(PolarChartComponent, _super); function PolarChartComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legendTitle = 'Legend'; _this.legendPosition = 'right'; _this.showGridLines = true; _this.curve = curveCardinalClosed; _this.activeEntries = []; _this.rangeFillOpacity = 0.15; _this.trimYAxisTicks = true; _this.maxYAxisTickLength = 16; _this.roundDomains = false; _this.tooltipDisabled = false; _this.showSeriesOnHover = true; _this.gradient = false; _this.yAxisMinScale = 0; _this.labelTrim = true; _this.labelTrimSize = 10; _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, legendPosition: this.legendPosition }); var halfWidth = Math.floor(this.dims.width / 2); var halfHeight = Math.floor(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 = 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(Math.floor(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, [this.yAxisMinScale].concat(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.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, position: this.legendPosition }; } return { scaleType: this.schemeType, colors: this.colors.scale, domain: this.yDomain, title: undefined, position: this.legendPosition }; }; 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; }; __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "legend", void 0); __decorate([ Input(), __metadata("design:type", String) ], PolarChartComponent.prototype, "legendTitle", void 0); __decorate([ Input(), __metadata("design:type", String) ], PolarChartComponent.prototype, "legendPosition", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "xAxis", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "yAxis", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "showXAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "showYAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", String) ], PolarChartComponent.prototype, "xAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", String) ], PolarChartComponent.prototype, "yAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "autoScale", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "showGridLines", void 0); __decorate([ Input(), __metadata("design:type", Object) ], PolarChartComponent.prototype, "curve", void 0); __decorate([ Input(), __metadata("design:type", Array) ], PolarChartComponent.prototype, "activeEntries", void 0); __decorate([ Input(), __metadata("design:type", String) ], PolarChartComponent.prototype, "schemeType", void 0); __decorate([ Input(), __metadata("design:type", Number) ], PolarChartComponent.prototype, "rangeFillOpacity", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "trimYAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Number) ], PolarChartComponent.prototype, "maxYAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Function) ], PolarChartComponent.prototype, "xAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Function) ], PolarChartComponent.prototype, "yAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "roundDomains", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "tooltipDisabled", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "showSeriesOnHover", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "gradient", void 0); __decorate([ Input(), __metadata("design:type", Number) ], PolarChartComponent.prototype, "yAxisMinScale", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], PolarChartComponent.prototype, "labelTrim", void 0); __decorate([ Input(), __metadata("design:type", Number) ], PolarChartComponent.prototype, "labelTrimSize", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], PolarChartComponent.prototype, "activate", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], PolarChartComponent.prototype, "deactivate", void 0); __decorate([ ContentChild('tooltipTemplate', { static: false }), __metadata("design:type", TemplateRef) ], PolarChartComponent.prototype, "tooltipTemplate", void 0); PolarChartComponent = __decorate([ Component({ selector: 'ngx-charts-polar-chart', template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n [animations]=\"animations\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\"\n >\n <svg:g class=\"polar-chart chart\" [attr.transform]=\"transform\">\n <svg:g [attr.transform]=\"transformPlot\">\n <svg:circle class=\"polar-chart-background\" cx=\"0\" cy=\"0\" [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\"\n cy=\"0\"\n [attr.r]=\"r\"\n />\n </svg:g>\n <svg:g *ngIf=\"xAxis\">\n <svg:g\n 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 [animations]=\"animations\"\n [labelTrim]=\"labelTrim\"\n [labelTrimSize]=\"labelTrimSize\"\n ></svg:g>\n </svg:g>\n </svg:g>\n <svg:g\n 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 [trimTicks]=\"trimYAxisTicks\"\n [maxTickLength]=\"maxYAxisTickLength\"\n [tickFormatting]=\"yAxisTickFormatting\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"\n ></svg:g>\n <svg:g\n 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\n 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 [animations]=\"animations\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n (select)=\"onClick($event)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\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 })) ]) ]) ] }) ], PolarChartComponent); return PolarChartComponent; }(BaseChartComponent)); export { PolarChartComponent }; //# sourceMappingURL=polar-chart.component.js.map