@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
267 lines • 13.5 kB
JavaScript
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, HostListener, ViewEncapsulation, ChangeDetectionStrategy, ContentChild } from '@angular/core';
import { trigger, style, animate, transition } from '@angular/animations';
import { scaleLinear } from 'd3-scale';
import { BaseChartComponent } from '../common/base-chart.component';
import { calculateViewDimensions } from '../common/view-dimensions.helper';
import { ColorHelper } from '../common/color.helper';
import { getScaleType, getDomain, getScale } from './bubble-chart.utils';
var BubbleChartComponent = (function (_super) {
__extends(BubbleChartComponent, _super);
function BubbleChartComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.showGridLines = true;
_this.legend = false;
_this.legendTitle = 'Legend';
_this.xAxis = true;
_this.yAxis = true;
_this.roundDomains = false;
_this.maxRadius = 10;
_this.minRadius = 3;
_this.schemeType = 'ordinal';
_this.legendPosition = 'right';
_this.tooltipDisabled = false;
_this.activate = new EventEmitter();
_this.deactivate = new EventEmitter();
_this.scaleType = 'linear';
_this.margin = [10, 20, 10, 20];
_this.bubblePadding = [0, 0, 0, 0];
_this.xAxisHeight = 0;
_this.yAxisWidth = 0;
_this.activeEntries = [];
return _this;
}
BubbleChartComponent.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.seriesDomain = this.results.map(function (d) { return d.name; });
this.rDomain = this.getRDomain();
this.xDomain = this.getXDomain();
this.yDomain = this.getYDomain();
this.transform = "translate(" + this.dims.xOffset + "," + this.margin[0] + ")";
var colorDomain = this.schemeType === 'ordinal' ? this.seriesDomain : this.rDomain;
this.colors = new ColorHelper(this.scheme, this.schemeType, colorDomain, this.customColors);
this.data = this.results;
this.minRadius = Math.max(this.minRadius, 1);
this.maxRadius = Math.max(this.maxRadius, 1);
this.rScale = this.getRScale(this.rDomain, [this.minRadius, this.maxRadius]);
this.bubblePadding = [0, 0, 0, 0];
this.setScales();
this.bubblePadding = this.getBubblePadding();
this.setScales();
this.legendOptions = this.getLegendOptions();
};
BubbleChartComponent.prototype.hideCircles = function () {
this.deactivateAll();
};
BubbleChartComponent.prototype.onClick = function (data, series) {
if (series) {
data.series = series.name;
}
this.select.emit(data);
};
BubbleChartComponent.prototype.getBubblePadding = function () {
var yMin = 0;
var xMin = 0;
var yMax = this.dims.height;
var xMax = this.dims.width;
for (var _i = 0, _a = this.data; _i < _a.length; _i++) {
var s = _a[_i];
for (var _b = 0, _c = s.series; _b < _c.length; _b++) {
var d = _c[_b];
var r = this.rScale(d.r);
var cx = (this.xScaleType === 'linear') ? this.xScale(Number(d.x)) : this.xScale(d.x);
var cy = (this.yScaleType === 'linear') ? this.yScale(Number(d.y)) : this.yScale(d.y);
xMin = Math.max(r - cx, xMin);
yMin = Math.max(r - cy, yMin);
yMax = Math.max(cy + r, yMax);
xMax = Math.max(cx + r, xMax);
}
}
xMax = Math.max(xMax - this.dims.width, 0);
yMax = Math.max(yMax - this.dims.height, 0);
return [yMin, xMax, yMax, xMin];
};
BubbleChartComponent.prototype.setScales = function () {
this.xScale = this.getXScale(this.xDomain, this.dims.width - this.bubblePadding[1]);
this.yScale = this.getYScale(this.yDomain, this.dims.height - this.bubblePadding[2]);
};
BubbleChartComponent.prototype.getYScale = function (domain, height) {
return getScale(domain, [height, this.bubblePadding[0]], this.yScaleType, this.roundDomains);
};
BubbleChartComponent.prototype.getXScale = function (domain, width) {
return getScale(domain, [this.bubblePadding[3], width], this.xScaleType, this.roundDomains);
};
BubbleChartComponent.prototype.getRScale = function (domain, range) {
var scale = scaleLinear()
.range(range)
.domain(domain);
return this.roundDomains ? scale.nice() : scale;
};
BubbleChartComponent.prototype.getLegendOptions = function () {
var opts = {
scaleType: this.schemeType,
colors: undefined,
domain: [],
position: this.legendPosition,
title: undefined
};
if (opts.scaleType === 'ordinal') {
opts.domain = this.seriesDomain;
opts.colors = this.colors;
opts.title = this.legendTitle;
}
else {
opts.domain = this.rDomain;
opts.colors = this.colors.scale;
}
return opts;
};
BubbleChartComponent.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.x)) {
values.push(d.x);
}
}
}
this.xScaleType = getScaleType(values);
return getDomain(values, this.xScaleType, this.autoScale);
};
BubbleChartComponent.prototype.getYDomain = 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.y)) {
values.push(d.y);
}
}
}
this.yScaleType = getScaleType(values);
return getDomain(values, this.yScaleType, this.autoScale);
};
BubbleChartComponent.prototype.getRDomain = function () {
var min = Infinity;
var max = -Infinity;
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];
var value = Number(d.r) || 1;
min = Math.min(min, value);
max = Math.max(max, value);
}
}
return [min, max];
};
BubbleChartComponent.prototype.updateYAxisWidth = function (_a) {
var width = _a.width;
this.yAxisWidth = width;
this.update();
};
BubbleChartComponent.prototype.updateXAxisHeight = function (_a) {
var height = _a.height;
this.xAxisHeight = height;
this.update();
};
BubbleChartComponent.prototype.onActivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name;
});
if (idx > -1) {
return;
}
this.activeEntries = [item].concat(this.activeEntries);
this.activate.emit({ value: item, entries: this.activeEntries });
};
BubbleChartComponent.prototype.onDeactivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name;
});
this.activeEntries.splice(idx, 1);
this.activeEntries = this.activeEntries.slice();
this.deactivate.emit({ value: item, entries: this.activeEntries });
};
BubbleChartComponent.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 = [];
};
BubbleChartComponent.prototype.trackBy = function (index, item) {
return item.name;
};
return BubbleChartComponent;
}(BaseChartComponent));
export { BubbleChartComponent };
BubbleChartComponent.decorators = [
{ type: Component, args: [{
selector: 'ngx-charts-bubble-chart',
template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [activeEntries]=\"activeEntries\"\n [legendOptions]=\"legendOptions\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\">\n <svg:defs>\n <svg:clipPath>\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=\"bubble-chart chart\">\n <svg:g ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [showGridLines]=\"showGridLines\"\n [dims]=\"dims\"\n [xScale]=\"xScale\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n [tickFormatting]=\"xAxisTickFormatting\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\"/>\n <svg:g ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [showGridLines]=\"showGridLines\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [tickFormatting]=\"yAxisTickFormatting\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"/>\n <svg:rect\n class=\"bubble-chart-area\"\n x=\"0\"\n y=\"0\"\n [attr.width]=\"dims.width\"\n [attr.height]=\"dims.height\"\n style=\"fill: rgb(255, 0, 0); opacity: 0; cursor: 'auto';\"\n (mouseenter)=\"deactivateAll()\"\n />\n <svg:g *ngFor=\"let series of data; trackBy:trackBy\" [@animationState]=\"'active'\">\n <svg:g ngx-charts-bubble-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [rScale]=\"rScale\"\n [xScaleType]=\"xScaleType\"\n [yScaleType]=\"yScaleType\"\n [xAxisLabel]=\"xAxisLabel\"\n [yAxisLabel]=\"yAxisLabel\"\n [colors]=\"colors\"\n [data]=\"series\"\n [activeEntries]=\"activeEntries\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n (select)=\"onClick($event, series)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\" />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ",
styleUrls: ['../common/base-chart.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
animations: [
trigger('animationState', [
transition(':leave', [
style({
opacity: 1,
}),
animate(500, style({
opacity: 0
}))
])
])
]
},] },
];
/** @nocollapse */
BubbleChartComponent.ctorParameters = function () { return []; };
BubbleChartComponent.propDecorators = {
'showGridLines': [{ type: Input },],
'legend': [{ type: Input },],
'legendTitle': [{ type: Input },],
'xAxis': [{ type: Input },],
'yAxis': [{ type: Input },],
'showXAxisLabel': [{ type: Input },],
'showYAxisLabel': [{ type: Input },],
'xAxisLabel': [{ type: Input },],
'yAxisLabel': [{ type: Input },],
'xAxisTickFormatting': [{ type: Input },],
'yAxisTickFormatting': [{ type: Input },],
'roundDomains': [{ type: Input },],
'maxRadius': [{ type: Input },],
'minRadius': [{ type: Input },],
'autoScale': [{ type: Input },],
'schemeType': [{ type: Input },],
'legendPosition': [{ type: Input },],
'tooltipDisabled': [{ type: Input },],
'activate': [{ type: Output },],
'deactivate': [{ type: Output },],
'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },],
'hideCircles': [{ type: HostListener, args: ['mouseleave',] },],
};
//# sourceMappingURL=bubble-chart.component.js.map