UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular

442 lines 22.5 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 __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, HostListener, ViewEncapsulation, ChangeDetectionStrategy, ContentChild, TemplateRef } from '@angular/core'; import { scaleLinear, scalePoint, scaleTime } from 'd3-scale'; import { curveLinear } from 'd3-shape'; import { calculateViewDimensions } from '../common/view-dimensions.helper'; import { ColorHelper } from '../common/color.helper'; import { BaseChartComponent } from '../common/base-chart.component'; import { id } from '../utils/id'; import { getUniqueXDomainValues, getScaleType } from '../common/domain.helper'; var AreaChartNormalizedComponent = /** @class */ (function (_super) { __extends(AreaChartNormalizedComponent, _super); function AreaChartNormalizedComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legend = false; _this.legendTitle = 'Legend'; _this.legendPosition = 'right'; _this.showGridLines = true; _this.curve = curveLinear; _this.activeEntries = []; _this.trimXAxisTicks = true; _this.trimYAxisTicks = true; _this.rotateXAxisTicks = true; _this.maxXAxisTickLength = 16; _this.maxYAxisTickLength = 16; _this.roundDomains = false; _this.tooltipDisabled = false; _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); _this.margin = [10, 20, 10, 20]; _this.xAxisHeight = 0; _this.yAxisWidth = 0; _this.timelineHeight = 50; _this.timelinePadding = 10; return _this; } AreaChartNormalizedComponent.prototype.update = function () { var _this = this; _super.prototype.update.call(this); 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 }); if (this.timeline) { this.dims.height -= this.timelineHeight + this.margin[2] + this.timelinePadding; } this.xDomain = this.getXDomain(); if (this.filteredDomain) { this.xDomain = this.filteredDomain; } this.yDomain = this.getYDomain(); this.seriesDomain = this.getSeriesDomain(); this.xScale = this.getXScale(this.xDomain, this.dims.width); this.yScale = this.getYScale(this.yDomain, this.dims.height); var _loop_1 = function (i) { var val = this_1.xSet[i]; var d0 = 0; var total = 0; for (var _i = 0, _a = this_1.results; _i < _a.length; _i++) { var group = _a[_i]; var d = group.series.find(function (item) { var a = item.name; var b = val; if (_this.scaleType === 'time') { a = a.valueOf(); b = b.valueOf(); } return a === b; }); if (d) { total += d.value; } } for (var _b = 0, _c = this_1.results; _b < _c.length; _b++) { var group = _c[_b]; var d = group.series.find(function (item) { var a = item.name; var b = val; if (_this.scaleType === 'time') { a = a.valueOf(); b = b.valueOf(); } return a === b; }); if (d) { d.d0 = d0; d.d1 = d0 + d.value; d0 += d.value; } else { d = { name: val, value: 0, d0: d0, d1: d0 }; group.series.push(d); } if (total > 0) { d.d0 = (d.d0 * 100) / total; d.d1 = (d.d1 * 100) / total; } else { d.d0 = 0; d.d1 = 0; } } }; var this_1 = this; for (var i = 0; i < this.xSet.length; i++) { _loop_1(i); } this.updateTimeline(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")"; this.clipPathId = 'clip' + id().toString(); this.clipPath = "url(#" + this.clipPathId + ")"; }; AreaChartNormalizedComponent.prototype.updateTimeline = function () { if (this.timeline) { this.timelineWidth = this.dims.width; this.timelineXDomain = this.getXDomain(); this.timelineXScale = this.getXScale(this.timelineXDomain, this.timelineWidth); this.timelineYScale = this.getYScale(this.yDomain, this.timelineHeight); this.timelineTransform = "translate(" + this.dims.xOffset + ", " + -this.margin[2] + ")"; } }; AreaChartNormalizedComponent.prototype.getXDomain = function () { var values = getUniqueXDomainValues(this.results); this.scaleType = getScaleType(values); var domain = []; if (this.scaleType === 'time') { var min = Math.min.apply(Math, values); var max = Math.max.apply(Math, values); domain = [new Date(min), new Date(max)]; this.xSet = values.slice().sort(function (a, b) { var aDate = a.getTime(); var bDate = b.getTime(); if (aDate > bDate) return 1; if (bDate > aDate) return -1; return 0; }); } 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); domain = [min, max]; // Use compare function to sort numbers numerically this.xSet = values.slice().sort(function (a, b) { return a - b; }); } else { domain = values; this.xSet = values; } return domain; }; AreaChartNormalizedComponent.prototype.getYDomain = function () { return [0, 100]; }; AreaChartNormalizedComponent.prototype.getSeriesDomain = function () { return this.results.map(function (d) { return d.name; }); }; AreaChartNormalizedComponent.prototype.getXScale = function (domain, width) { var scale; if (this.scaleType === 'time') { scale = scaleTime(); } else if (this.scaleType === 'linear') { scale = scaleLinear(); } else if (this.scaleType === 'ordinal') { scale = scalePoint().padding(0.1); } scale.range([0, width]).domain(domain); return this.roundDomains ? scale.nice() : scale; }; AreaChartNormalizedComponent.prototype.getYScale = function (domain, height) { var scale = scaleLinear() .range([height, 0]) .domain(domain); return this.roundDomains ? scale.nice() : scale; }; AreaChartNormalizedComponent.prototype.updateDomain = function (domain) { this.filteredDomain = domain; this.xDomain = this.filteredDomain; this.xScale = this.getXScale(this.xDomain, this.dims.width); }; AreaChartNormalizedComponent.prototype.updateHoveredVertical = function (item) { this.hoveredVertical = item.value; this.deactivateAll(); }; AreaChartNormalizedComponent.prototype.hideCircles = function () { this.hoveredVertical = null; this.deactivateAll(); }; AreaChartNormalizedComponent.prototype.onClick = function (data, series) { if (series) { data.series = series.name; } this.select.emit(data); }; AreaChartNormalizedComponent.prototype.trackBy = function (index, item) { return item.name; }; AreaChartNormalizedComponent.prototype.setColors = function () { var domain; if (this.schemeType === 'ordinal') { domain = this.seriesDomain; } else { domain = this.yDomain; } this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors); }; AreaChartNormalizedComponent.prototype.getLegendOptions = function () { var opts = { scaleType: this.schemeType, colors: undefined, domain: [], title: undefined, position: this.legendPosition }; if (opts.scaleType === 'ordinal') { opts.domain = this.seriesDomain; opts.colors = this.colors; opts.title = this.legendTitle; } else { opts.domain = this.yDomain; opts.colors = this.colors.scale; } return opts; }; AreaChartNormalizedComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; AreaChartNormalizedComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; AreaChartNormalizedComponent.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 }); }; AreaChartNormalizedComponent.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 }); }; AreaChartNormalizedComponent.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 = []; }; __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "legend", void 0); __decorate([ Input(), __metadata("design:type", String) ], AreaChartNormalizedComponent.prototype, "legendTitle", void 0); __decorate([ Input(), __metadata("design:type", String) ], AreaChartNormalizedComponent.prototype, "legendPosition", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "xAxis", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "yAxis", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "showXAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "showYAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "xAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "yAxisLabel", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "timeline", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "gradient", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], AreaChartNormalizedComponent.prototype, "showGridLines", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "curve", void 0); __decorate([ Input(), __metadata("design:type", Array) ], AreaChartNormalizedComponent.prototype, "activeEntries", void 0); __decorate([ Input(), __metadata("design:type", String) ], AreaChartNormalizedComponent.prototype, "schemeType", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], AreaChartNormalizedComponent.prototype, "trimXAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], AreaChartNormalizedComponent.prototype, "trimYAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], AreaChartNormalizedComponent.prototype, "rotateXAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Number) ], AreaChartNormalizedComponent.prototype, "maxXAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Number) ], AreaChartNormalizedComponent.prototype, "maxYAxisTickLength", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "xAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Object) ], AreaChartNormalizedComponent.prototype, "yAxisTickFormatting", void 0); __decorate([ Input(), __metadata("design:type", Array) ], AreaChartNormalizedComponent.prototype, "xAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Array) ], AreaChartNormalizedComponent.prototype, "yAxisTicks", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], AreaChartNormalizedComponent.prototype, "roundDomains", void 0); __decorate([ Input(), __metadata("design:type", Boolean) ], AreaChartNormalizedComponent.prototype, "tooltipDisabled", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], AreaChartNormalizedComponent.prototype, "activate", void 0); __decorate([ Output(), __metadata("design:type", EventEmitter) ], AreaChartNormalizedComponent.prototype, "deactivate", void 0); __decorate([ ContentChild('tooltipTemplate', { static: false }), __metadata("design:type", TemplateRef) ], AreaChartNormalizedComponent.prototype, "tooltipTemplate", void 0); __decorate([ ContentChild('seriesTooltipTemplate', { static: false }), __metadata("design:type", TemplateRef) ], AreaChartNormalizedComponent.prototype, "seriesTooltipTemplate", void 0); __decorate([ HostListener('mouseleave'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], AreaChartNormalizedComponent.prototype, "hideCircles", null); AreaChartNormalizedComponent = __decorate([ Component({ selector: 'ngx-charts-area-chart-normalized', 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:defs>\n <svg:clipPath [attr.id]=\"clipPathId\">\n <svg:rect\n [attr.width]=\"dims.width + 10\"\n [attr.height]=\"dims.height + 10\"\n [attr.transform]=\"'translate(-5, -5)'\"\n />\n </svg:clipPath>\n </svg:defs>\n <svg:g [attr.transform]=\"transform\" class=\"area-chart chart\">\n <svg:g\n ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n [trimTicks]=\"trimXAxisTicks\"\n [rotateTicks]=\"rotateXAxisTicks\"\n [maxTickLength]=\"maxXAxisTickLength\"\n [tickFormatting]=\"xAxisTickFormatting\"\n [ticks]=\"xAxisTicks\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\"\n ></svg:g>\n <svg:g\n ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [trimTicks]=\"trimYAxisTicks\"\n [maxTickLength]=\"maxYAxisTickLength\"\n [tickFormatting]=\"yAxisTickFormatting\"\n [ticks]=\"yAxisTicks\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"\n ></svg:g>\n <svg:g [attr.clip-path]=\"clipPath\">\n <svg:g *ngFor=\"let series of results; trackBy: trackBy\">\n <svg:g\n ngx-charts-area-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [activeEntries]=\"activeEntries\"\n [gradient]=\"gradient\"\n normalized=\"true\"\n [curve]=\"curve\"\n [animations]=\"animations\"\n />\n </svg:g>\n\n <svg:g *ngIf=\"!tooltipDisabled\" (mouseleave)=\"hideCircles()\">\n <svg:g\n ngx-charts-tooltip-area\n [dims]=\"dims\"\n [xSet]=\"xSet\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [results]=\"results\"\n [colors]=\"colors\"\n [showPercentage]=\"true\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"seriesTooltipTemplate\"\n (hover)=\"updateHoveredVertical($event)\"\n />\n\n <svg:g *ngFor=\"let series of results\">\n <svg:g\n ngx-charts-circle-series\n type=\"stacked\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [activeEntries]=\"activeEntries\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [visibleValue]=\"hoveredVertical\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n (select)=\"onClick($event, series)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n />\n </svg:g>\n </svg:g>\n </svg:g>\n </svg:g>\n <svg:g\n ngx-charts-timeline\n *ngIf=\"timeline && scaleType != 'ordinal'\"\n [attr.transform]=\"timelineTransform\"\n [results]=\"results\"\n [view]=\"[timelineWidth, height]\"\n [height]=\"timelineHeight\"\n [scheme]=\"scheme\"\n [customColors]=\"customColors\"\n [legend]=\"legend\"\n [scaleType]=\"scaleType\"\n (onDomainChange)=\"updateDomain($event)\"\n >\n <svg:g *ngFor=\"let series of results; trackBy: trackBy\">\n <svg:g\n ngx-charts-area-series\n [xScale]=\"timelineXScale\"\n [yScale]=\"timelineYScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [gradient]=\"gradient\"\n normalized=\"true\"\n [curve]=\"curve\"\n [animations]=\"animations\"\n />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ", changeDetection: ChangeDetectionStrategy.OnPush, styleUrls: ['../common/base-chart.component.css'], encapsulation: ViewEncapsulation.None }) ], AreaChartNormalizedComponent); return AreaChartNormalizedComponent; }(BaseChartComponent)); export { AreaChartNormalizedComponent }; //# sourceMappingURL=area-chart-normalized.component.js.map