UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

314 lines 16.1 kB
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 __()); }; import { Component, Input, Output, EventEmitter, ViewEncapsulation, HostListener, ChangeDetectionStrategy, ContentChild } from '@angular/core'; import { PathLocationStrategy } from '@angular/common'; 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'; var AreaChartComponent = (function (_super) { __extends(AreaChartComponent, _super); function AreaChartComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legendTitle = 'Legend'; _this.showGridLines = true; _this.curve = curveLinear; _this.activeEntries = []; _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; } AreaChartComponent.prototype.update = function () { _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 }); 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); this.updateTimeline(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + ", " + this.margin[0] + ")"; var pageUrl = this.location instanceof PathLocationStrategy ? this.location.path() : ''; this.clipPathId = 'clip' + id().toString(); this.clipPath = "url(" + pageUrl + "#" + this.clipPathId + ")"; }; AreaChartComponent.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] + ")"; } }; AreaChartComponent.prototype.getXDomain = 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); } } } this.scaleType = this.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]; this.xSet = values.slice().sort(); } else { domain = values; this.xSet = values; } return domain; }; AreaChartComponent.prototype.getYDomain = 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.includes(d.value)) { domain.push(d.value); } } } var min = Math.min.apply(Math, domain); var max = Math.max.apply(Math, domain); if (!this.autoScale) { min = Math.min(0, min); } return [min, max]; }; AreaChartComponent.prototype.getSeriesDomain = function () { return this.results.map(function (d) { return d.name; }); }; AreaChartComponent.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; }; AreaChartComponent.prototype.getYScale = function (domain, height) { var scale = scaleLinear() .range([height, 0]) .domain(domain); return this.roundDomains ? scale.nice() : scale; }; AreaChartComponent.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 (!this.isDate(value)) { date = false; } if (typeof value !== 'number') { num = false; } } if (date) { return 'time'; } if (num) { return 'linear'; } return 'ordinal'; }; AreaChartComponent.prototype.isDate = function (value) { if (value instanceof Date) { return true; } return false; }; AreaChartComponent.prototype.updateDomain = function (domain) { this.filteredDomain = domain; this.xDomain = this.filteredDomain; this.xScale = this.getXScale(this.xDomain, this.dims.width); }; AreaChartComponent.prototype.updateHoveredVertical = function (item) { this.hoveredVertical = item.value; this.deactivateAll(); }; AreaChartComponent.prototype.hideCircles = function () { this.hoveredVertical = null; this.deactivateAll(); }; AreaChartComponent.prototype.onClick = function (data, series) { if (series) { data.series = series.name; } this.select.emit(data); }; AreaChartComponent.prototype.trackBy = function (index, item) { return item.name; }; AreaChartComponent.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); }; AreaChartComponent.prototype.getLegendOptions = function () { var opts = { scaleType: this.schemeType, colors: undefined, domain: [], title: undefined }; 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; }; AreaChartComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; AreaChartComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; AreaChartComponent.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 }); }; AreaChartComponent.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 }); }; AreaChartComponent.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 = []; }; return AreaChartComponent; }(BaseChartComponent)); export { AreaChartComponent }; AreaChartComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-charts-area-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: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 </svg:clipPath>\n </svg:defs>\n <svg:g [attr.transform]=\"transform\" class=\"area-chart chart\">\n <svg:g ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n [tickFormatting]=\"xAxisTickFormatting\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\">\n </svg:g>\n <svg:g ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [tickFormatting]=\"yAxisTickFormatting\"\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 ngx-charts-area-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [activeEntries]=\"activeEntries\"\n [scaleType]=\"scaleType\"\n [gradient]=\"gradient\"\n [curve]=\"curve\"\n />\n </svg:g>\n\n <svg:g *ngIf=\"!tooltipDisabled\" (mouseleave)=\"hideCircles()\">\n <svg:g ngx-charts-tooltip-area\n [dims]=\"dims\"\n [xSet]=\"xSet\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [results]=\"results\"\n [colors]=\"colors\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"seriesTooltipTemplate\"\n (hover)=\"updateHoveredVertical($event)\"\n />\n\n <svg:g *ngFor=\"let series of results\">\n <svg:g ngx-charts-circle-series\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 ngx-charts-timeline\n *ngIf=\"timeline && scaleType === 'time'\"\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 <svg:g *ngFor=\"let series of results; trackBy:trackBy\">\n <svg:g ngx-charts-area-series\n [xScale]=\"timelineXScale\"\n [yScale]=\"timelineYScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [gradient]=\"gradient\"\n [curve]=\"curve\"\n />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ", changeDetection: ChangeDetectionStrategy.OnPush, styleUrls: ['../common/base-chart.component.css'], encapsulation: ViewEncapsulation.None },] }, ]; /** @nocollapse */ AreaChartComponent.ctorParameters = function () { return []; }; AreaChartComponent.propDecorators = { 'legend': [{ type: Input },], 'legendTitle': [{ type: Input },], 'state': [{ type: Input },], 'xAxis': [{ type: Input },], 'yAxis': [{ type: Input },], 'autoScale': [{ type: Input },], 'showXAxisLabel': [{ type: Input },], 'showYAxisLabel': [{ type: Input },], 'xAxisLabel': [{ type: Input },], 'yAxisLabel': [{ type: Input },], 'timeline': [{ type: Input },], 'gradient': [{ type: Input },], 'showGridLines': [{ type: Input },], 'curve': [{ type: Input },], 'activeEntries': [{ type: Input },], 'schemeType': [{ type: Input },], 'xAxisTickFormatting': [{ type: Input },], 'yAxisTickFormatting': [{ type: Input },], 'roundDomains': [{ type: Input },], 'tooltipDisabled': [{ type: Input },], 'activate': [{ type: Output },], 'deactivate': [{ type: Output },], 'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },], 'seriesTooltipTemplate': [{ type: ContentChild, args: ['seriesTooltipTemplate',] },], 'hideCircles': [{ type: HostListener, args: ['mouseleave',] },], }; //# sourceMappingURL=area-chart.component.js.map