UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

245 lines 11.8 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, ViewEncapsulation, EventEmitter, 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 BarVertical2DComponent = (function (_super) { __extends(BarVertical2DComponent, _super); function BarVertical2DComponent() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.legend = false; _this.legendTitle = 'Legend'; _this.tooltipDisabled = false; _this.scaleType = 'ordinal'; _this.showGridLines = true; _this.activeEntries = []; _this.groupPadding = 16; _this.barPadding = 8; _this.roundDomains = false; _this.roundEdges = true; _this.activate = new EventEmitter(); _this.deactivate = new EventEmitter(); _this.margin = [10, 20, 10, 20]; _this.xAxisHeight = 0; _this.yAxisWidth = 0; return _this; } BarVertical2DComponent.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.valuesDomain = this.getValueDomain(); this.groupScale = this.getGroupScale(); this.innerScale = this.getInnerScale(); this.valueScale = this.getValueScale(); this.setColors(); this.legendOptions = this.getLegendOptions(); this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")"; }; BarVertical2DComponent.prototype.getGroupScale = function () { var spacing = this.groupDomain.length / (this.dims.height / this.groupPadding + 1); return scaleBand() .rangeRound([0, this.dims.width]) .paddingInner(spacing) .paddingOuter(spacing / 2) .domain(this.groupDomain); }; BarVertical2DComponent.prototype.getInnerScale = function () { var width = this.groupScale.bandwidth(); var spacing = this.innerDomain.length / (width / this.barPadding + 1); return scaleBand() .rangeRound([0, width]) .paddingInner(spacing) .domain(this.innerDomain); }; BarVertical2DComponent.prototype.getValueScale = function () { var scale = scaleLinear() .range([this.dims.height, 0]) .domain(this.valuesDomain); return this.roundDomains ? scale.nice() : scale; }; BarVertical2DComponent.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; }; BarVertical2DComponent.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; }; BarVertical2DComponent.prototype.getValueDomain = 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.value)) { domain.push(d.value); } } } var min = Math.min.apply(Math, [0].concat(domain)); var max = Math.max.apply(Math, domain); return [min, max]; }; BarVertical2DComponent.prototype.groupTransform = function (group) { return "translate(" + this.groupScale(group.name) + ", 0)"; }; BarVertical2DComponent.prototype.onClick = function (data, group) { if (group) { data.series = group.name; } this.select.emit(data); }; BarVertical2DComponent.prototype.trackBy = function (index, item) { return item.name; }; BarVertical2DComponent.prototype.setColors = function () { var domain; if (this.schemeType === 'ordinal') { domain = this.innerDomain; } else { domain = this.valuesDomain; } this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors); }; BarVertical2DComponent.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.valuesDomain; opts.colors = this.colors.scale; } return opts; }; BarVertical2DComponent.prototype.updateYAxisWidth = function (_a) { var width = _a.width; this.yAxisWidth = width; this.update(); }; BarVertical2DComponent.prototype.updateXAxisHeight = function (_a) { var height = _a.height; this.xAxisHeight = height; this.update(); }; BarVertical2DComponent.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 }); }; BarVertical2DComponent.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 BarVertical2DComponent; }(BaseChartComponent)); export { BarVertical2DComponent }; BarVertical2DComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-charts-bar-vertical-2d', 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-grid-panel-series\n [xScale]=\"groupScale\"\n [yScale]=\"valueScale\"\n [data]=\"results\"\n [dims]=\"dims\"\n orient=\"vertical\">\n </svg:g>\n <svg:g ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"groupScale\"\n [dims]=\"dims\"\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]=\"valueScale\"\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 ngx-charts-series-vertical\n *ngFor=\"let group of results; trackBy:trackBy\"\n [@animationState]=\"'active'\"\n [attr.transform]=\"groupTransform(group)\"\n [activeEntries]=\"activeEntries\"\n [xScale]=\"innerScale\"\n [yScale]=\"valueScale\"\n [colors]=\"colors\"\n [series]=\"group.series\"\n [dims]=\"dims\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [seriesName]=\"group.name\"\n [roundEdges]=\"roundEdges\"\n (select)=\"onClick($event, group)\"\n (activate)=\"onActivate($event, group)\"\n (deactivate)=\"onDeactivate($event, group)\"\n />\n </svg:g>\n </ngx-charts-chart>\n ", styleUrls: ['../common/base-chart.component.css'], encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, animations: [ trigger('animationState', [ transition(':leave', [ style({ opacity: 1, transform: '*', }), animate(500, style({ opacity: 0, transform: 'scale(0)' })) ]) ]) ] },] }, ]; /** @nocollapse */ BarVertical2DComponent.ctorParameters = function () { return []; }; BarVertical2DComponent.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 },], 'scaleType': [{ type: Input },], 'gradient': [{ type: Input },], 'showGridLines': [{ type: Input },], 'activeEntries': [{ type: Input },], 'schemeType': [{ type: Input },], 'xAxisTickFormatting': [{ type: Input },], 'yAxisTickFormatting': [{ type: Input },], 'groupPadding': [{ type: Input },], 'barPadding': [{ type: Input },], 'roundDomains': [{ type: Input },], 'roundEdges': [{ type: Input },], 'activate': [{ type: Output },], 'deactivate': [{ type: Output },], 'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },], }; //# sourceMappingURL=bar-vertical-2d.component.js.map