UNPKG

@swimlane/ngx-charts

Version:

Declarative Charting Framework for Angular2 and beyond!

226 lines 10.9 kB
"use strict"; 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 __()); }; var core_1 = require('@angular/core'); var view_dimensions_helper_1 = require('../common/view-dimensions.helper'); var color_helper_1 = require('../common/color.helper'); var base_chart_component_1 = require('../common/base-chart.component'); var d3_1 = require('../d3'); var BarVertical2DComponent = (function (_super) { __extends(BarVertical2DComponent, _super); function BarVertical2DComponent() { _super.apply(this, arguments); this.legend = false; this.scaleType = 'ordinal'; this.showGridLines = true; this.activeEntries = []; this.activate = new core_1.EventEmitter(); this.deactivate = new core_1.EventEmitter(); this.margin = [10, 20, 10, 20]; this.xAxisHeight = 0; this.yAxisWidth = 0; } BarVertical2DComponent.prototype.update = function () { var _this = this; _super.prototype.update.call(this); this.zone.run(function () { _this.dims = view_dimensions_helper_1.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 = 0.2; return d3_1.default.scaleBand() .rangeRound([0, this.dims.width]) .paddingInner(spacing) .paddingOuter(spacing / 2) .domain(this.groupDomain); }; BarVertical2DComponent.prototype.getInnerScale = function () { var spacing = 0.2; return d3_1.default.scaleBand() .rangeRound([0, this.groupScale.bandwidth()]) .paddingInner(spacing) .domain(this.innerDomain); }; BarVertical2DComponent.prototype.getValueScale = function () { return d3_1.default.scaleLinear() .range([this.dims.height, 0]) .domain(this.valuesDomain); }; 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 color_helper_1.ColorHelper(this.scheme, this.schemeType, domain, this.customColors); }; BarVertical2DComponent.prototype.getLegendOptions = function () { var opts = { scaleType: this.schemeType, colors: undefined, domain: [] }; if (opts.scaleType === 'ordinal') { opts.domain = this.innerDomain; opts.colors = this.colors; } 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: event, entries: this.activeEntries }); }; BarVertical2DComponent.decorators = [ { type: core_1.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 (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 (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 (select)=\"onClick($event, group)\"\n (activate)=\"onActivate($event, group)\"\n (deactivate)=\"onDeactivate($event, group)\"\n />\n </svg:g>\n </ngx-charts-chart>\n ", changeDetection: core_1.ChangeDetectionStrategy.OnPush, animations: [ core_1.trigger('animationState', [ core_1.transition('* => void', [ core_1.style({ opacity: 1, transform: '*', }), core_1.animate(500, core_1.style({ opacity: 0, transform: 'scale(0)' })) ]) ]) ] },] }, ]; /** @nocollapse */ BarVertical2DComponent.ctorParameters = function () { return []; }; BarVertical2DComponent.propDecorators = { 'legend': [{ type: core_1.Input },], 'xAxis': [{ type: core_1.Input },], 'yAxis': [{ type: core_1.Input },], 'showXAxisLabel': [{ type: core_1.Input },], 'showYAxisLabel': [{ type: core_1.Input },], 'xAxisLabel': [{ type: core_1.Input },], 'yAxisLabel': [{ type: core_1.Input },], 'scaleType': [{ type: core_1.Input },], 'gradient': [{ type: core_1.Input },], 'showGridLines': [{ type: core_1.Input },], 'activeEntries': [{ type: core_1.Input },], 'schemeType': [{ type: core_1.Input },], 'activate': [{ type: core_1.Output },], 'deactivate': [{ type: core_1.Output },], }; return BarVertical2DComponent; }(base_chart_component_1.BaseChartComponent)); exports.BarVertical2DComponent = BarVertical2DComponent; //# sourceMappingURL=bar-vertical-2d.component.js.map