UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

229 lines 11.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, ChangeDetectionStrategy, ContentChild } from '@angular/core'; import { trigger, style, animate, transition } from '@angular/animations'; import { scaleBand, scaleLinear } from 'd3-scale'; import { calculateViewDimensions } from '../common/view-dimensions.helper'; import { ColorHelper } from '../common/color.helper'; import { BaseChartComponent } from '../common/base-chart.component'; var BarHorizontalStackedComponent = (function (_super) { __extends(BarHorizontalStackedComponent, _super); function BarHorizontalStackedComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legend = false; _this.legendTitle = 'Legend'; _this.tooltipDisabled = false; _this.showGridLines = true; _this.activeEntries = []; _this.barPadding = 8; _this.roundDomains = false; _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); _this.margin = [10, 20, 10, 20]; _this.xAxisHeight = 0; _this.yAxisWidth = 0; return _this; } BarHorizontalStackedComponent.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 }); this.formatDates(); this.groupDomain = this.getGroupDomain(); this.innerDomain = this.getInnerDomain(); this.valueDomain = this.getValueDomain(); this.xScale = this.getXScale(); this.yScale = this.getYScale(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")"; }; BarHorizontalStackedComponent.prototype.getGroupDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; if (!domain.includes(group.name)) { domain.push(group.name); } } return domain; }; BarHorizontalStackedComponent.prototype.getInnerDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; for (var _b = 0, _c = group.series; _b < _c.length; _b++) { var d = _c[_b]; if (!domain.includes(d.name)) { domain.push(d.name); } } } return domain; }; BarHorizontalStackedComponent.prototype.getValueDomain = function () { var domain = []; for (var _i = 0, _a = this.results; _i < _a.length; _i++) { var group = _a[_i]; var sum = 0; for (var _b = 0, _c = group.series; _b < _c.length; _b++) { var d = _c[_b]; sum += d.value; } domain.push(sum); } var min = Math.min.apply(Math, [0].concat(domain)); var max = Math.max.apply(Math, domain); return [min, max]; }; BarHorizontalStackedComponent.prototype.getYScale = function () { var spacing = this.groupDomain.length / (this.dims.height / this.barPadding + 1); return scaleBand() .rangeRound([0, this.dims.height]) .paddingInner(spacing) .domain(this.groupDomain); }; BarHorizontalStackedComponent.prototype.getXScale = function () { var scale = scaleLinear() .range([0, this.dims.width]) .domain(this.valueDomain); return this.roundDomains ? scale.nice() : scale; }; BarHorizontalStackedComponent.prototype.groupTransform = function (group) { return "translate(0, " + this.yScale(group.name) + ")"; }; BarHorizontalStackedComponent.prototype.onClick = function (data, group) { if (group) { data.series = group.name; } this.select.emit(data); }; BarHorizontalStackedComponent.prototype.trackBy = function (index, item) { return item.name; }; BarHorizontalStackedComponent.prototype.setColors = function () { var domain; if (this.schemeType === 'ordinal') { domain = this.innerDomain; } else { domain = this.valueDomain; } this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors); }; BarHorizontalStackedComponent.prototype.getLegendOptions = function () { var opts = { scaleType: this.schemeType, colors: undefined, domain: [], title: undefined }; if (opts.scaleType === 'ordinal') { opts.domain = this.innerDomain; opts.colors = this.colors; opts.title = this.legendTitle; } else { opts.domain = this.valueDomain; opts.colors = this.colors.scale; } return opts; }; BarHorizontalStackedComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; BarHorizontalStackedComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; BarHorizontalStackedComponent.prototype.onActivate = function (event, group) { var item = Object.assign({}, event); if (group) { item.series = group.name; } var idx = this.activeEntries.findIndex(function (d) { return d.name === item.name && d.value === item.value && d.series === item.series; }); if (idx > -1) { return; } this.activeEntries = [item].concat(this.activeEntries); this.activate.emit({ value: item, entries: this.activeEntries }); }; BarHorizontalStackedComponent.prototype.onDeactivate = function (event, group) { var item = Object.assign({}, event); if (group) { item.series = group.name; } var idx = this.activeEntries.findIndex(function (d) { return d.name === item.name && d.value === item.value && d.series === item.series; }); this.activeEntries.splice(idx, 1); this.activeEntries = this.activeEntries.slice(); this.deactivate.emit({ value: item, entries: this.activeEntries }); }; return BarHorizontalStackedComponent; }(BaseChartComponent)); export { BarHorizontalStackedComponent }; BarHorizontalStackedComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-charts-bar-horizontal-stacked', 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]=\"transform\" class=\"bar-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 [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [tickFormatting]=\"yAxisTickFormatting\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\">\n </svg:g>\n <svg:g\n *ngFor=\"let group of results; trackBy:trackBy\"\n [@animationState]=\"'active'\"\n [attr.transform]=\"groupTransform(group)\">\n <svg:g ngx-charts-series-horizontal\n type=\"stacked\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [series]=\"group.series\"\n [activeEntries]=\"activeEntries\"\n [dims]=\"dims\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [seriesName]=\"group.name\"\n (select)=\"onClick($event, group)\"\n (activate)=\"onActivate($event, group)\"\n (deactivate)=\"onDeactivate($event, group)\"\n />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ", changeDetection: ChangeDetectionStrategy.OnPush, styleUrls: ['../common/base-chart.component.css'], encapsulation: ViewEncapsulation.None, animations: [ trigger('animationState', [ transition(':leave', [ style({ opacity: 1, transform: '*', }), animate(500, style({ opacity: 0, transform: 'scale(0)' })) ]) ]) ] },] }, ]; /** @nocollapse */ BarHorizontalStackedComponent.ctorParameters = function () { return []; }; BarHorizontalStackedComponent.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 },], 'tooltipDisabled': [{ type: Input },], 'gradient': [{ type: Input },], 'showGridLines': [{ type: Input },], 'activeEntries': [{ type: Input },], 'schemeType': [{ type: Input },], 'xAxisTickFormatting': [{ type: Input },], 'yAxisTickFormatting': [{ type: Input },], 'barPadding': [{ type: Input },], 'roundDomains': [{ type: Input },], 'activate': [{ type: Output },], 'deactivate': [{ type: Output },], 'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },], }; //# sourceMappingURL=bar-horizontal-stacked.component.js.map