@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular
345 lines • 16.3 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Component, Input, Output, EventEmitter, ViewEncapsulation, ChangeDetectionStrategy, ContentChild, TemplateRef } from '@angular/core';
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 BarHorizontalComponent = /** @class */ (function (_super) {
__extends(BarHorizontalComponent, _super);
function BarHorizontalComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.legend = false;
_this.legendTitle = 'Legend';
_this.legendPosition = 'right';
_this.tooltipDisabled = false;
_this.showGridLines = true;
_this.activeEntries = [];
_this.trimXAxisTicks = true;
_this.trimYAxisTicks = true;
_this.rotateXAxisTicks = true;
_this.maxXAxisTickLength = 16;
_this.maxYAxisTickLength = 16;
_this.barPadding = 8;
_this.roundDomains = false;
_this.roundEdges = true;
_this.showDataLabel = false;
_this.noBarWhenZero = true;
_this.activate = new EventEmitter();
_this.deactivate = new EventEmitter();
_this.margin = [10, 20, 10, 20];
_this.xAxisHeight = 0;
_this.yAxisWidth = 0;
_this.dataLabelMaxWidth = { negative: 0, positive: 0 };
return _this;
}
BarHorizontalComponent.prototype.update = function () {
_super.prototype.update.call(this);
if (!this.showDataLabel) {
this.dataLabelMaxWidth = { negative: 0, positive: 0 };
}
this.margin = [10, 20 + this.dataLabelMaxWidth.positive, 10, 20 + this.dataLabelMaxWidth.negative];
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,
legendPosition: this.legendPosition
});
this.formatDates();
this.xScale = this.getXScale();
this.yScale = this.getYScale();
this.setColors();
this.legendOptions = this.getLegendOptions();
this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")";
};
BarHorizontalComponent.prototype.getXScale = function () {
this.xDomain = this.getXDomain();
var scale = scaleLinear()
.range([0, this.dims.width])
.domain(this.xDomain);
return this.roundDomains ? scale.nice() : scale;
};
BarHorizontalComponent.prototype.getYScale = function () {
this.yDomain = this.getYDomain();
var spacing = this.yDomain.length / (this.dims.height / this.barPadding + 1);
return scaleBand()
.rangeRound([0, this.dims.height])
.paddingInner(spacing)
.domain(this.yDomain);
};
BarHorizontalComponent.prototype.getXDomain = function () {
var values = this.results.map(function (d) { return d.value; });
var min = this.xScaleMin ? Math.min.apply(Math, [this.xScaleMin].concat(values)) : Math.min.apply(Math, [0].concat(values));
var max = this.xScaleMax ? Math.max.apply(Math, [this.xScaleMax].concat(values)) : Math.max.apply(Math, [0].concat(values));
return [min, max];
};
BarHorizontalComponent.prototype.getYDomain = function () {
return this.results.map(function (d) { return d.label; });
};
BarHorizontalComponent.prototype.onClick = function (data) {
this.select.emit(data);
};
BarHorizontalComponent.prototype.setColors = function () {
var domain;
if (this.schemeType === 'ordinal') {
domain = this.yDomain;
}
else {
domain = this.xDomain;
}
this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors);
};
BarHorizontalComponent.prototype.getLegendOptions = function () {
var opts = {
scaleType: this.schemeType,
colors: undefined,
domain: [],
title: undefined,
position: this.legendPosition
};
if (opts.scaleType === 'ordinal') {
opts.domain = this.yDomain;
opts.colors = this.colors;
opts.title = this.legendTitle;
}
else {
opts.domain = this.xDomain;
opts.colors = this.colors.scale;
}
return opts;
};
BarHorizontalComponent.prototype.updateYAxisWidth = function (_a) {
var width = _a.width;
this.yAxisWidth = width;
this.update();
};
BarHorizontalComponent.prototype.updateXAxisHeight = function (_a) {
var height = _a.height;
this.xAxisHeight = height;
this.update();
};
BarHorizontalComponent.prototype.onDataLabelMaxWidthChanged = function (event) {
var _this = this;
if (event.size.negative) {
this.dataLabelMaxWidth.negative = Math.max(this.dataLabelMaxWidth.negative, event.size.width);
}
else {
this.dataLabelMaxWidth.positive = Math.max(this.dataLabelMaxWidth.positive, event.size.width);
}
if (event.index === this.results.length - 1) {
setTimeout(function () { return _this.update(); });
}
};
BarHorizontalComponent.prototype.onActivate = function (item, fromLegend) {
if (fromLegend === void 0) { fromLegend = false; }
item = this.results.find(function (d) {
if (fromLegend) {
return d.label === item.name;
}
else {
return d.name === item.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 });
};
BarHorizontalComponent.prototype.onDeactivate = function (item, fromLegend) {
if (fromLegend === void 0) { fromLegend = false; }
item = this.results.find(function (d) {
if (fromLegend) {
return d.label === item.name;
}
else {
return d.name === item.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 });
};
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "legend", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], BarHorizontalComponent.prototype, "legendTitle", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], BarHorizontalComponent.prototype, "legendPosition", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "xAxis", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "yAxis", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "showXAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "showYAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "xAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "yAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "tooltipDisabled", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "gradient", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "showGridLines", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], BarHorizontalComponent.prototype, "activeEntries", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], BarHorizontalComponent.prototype, "schemeType", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "trimXAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "trimYAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "rotateXAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], BarHorizontalComponent.prototype, "maxXAxisTickLength", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], BarHorizontalComponent.prototype, "maxYAxisTickLength", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "xAxisTickFormatting", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "yAxisTickFormatting", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], BarHorizontalComponent.prototype, "xAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], BarHorizontalComponent.prototype, "yAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "barPadding", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "roundDomains", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "roundEdges", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], BarHorizontalComponent.prototype, "xScaleMax", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], BarHorizontalComponent.prototype, "xScaleMin", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "showDataLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], BarHorizontalComponent.prototype, "dataLabelFormatting", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], BarHorizontalComponent.prototype, "noBarWhenZero", void 0);
__decorate([
Output(),
__metadata("design:type", EventEmitter)
], BarHorizontalComponent.prototype, "activate", void 0);
__decorate([
Output(),
__metadata("design:type", EventEmitter)
], BarHorizontalComponent.prototype, "deactivate", void 0);
__decorate([
ContentChild('tooltipTemplate', { static: false }),
__metadata("design:type", TemplateRef)
], BarHorizontalComponent.prototype, "tooltipTemplate", void 0);
BarHorizontalComponent = __decorate([
Component({
selector: 'ngx-charts-bar-horizontal',
template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n [animations]=\"animations\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event, true)\"\n (legendLabelDeactivate)=\"onDeactivate($event, true)\"\n >\n <svg:g [attr.transform]=\"transform\" class=\"bar-chart chart\">\n <svg:g\n ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n [trimTicks]=\"trimXAxisTicks\"\n [rotateTicks]=\"rotateXAxisTicks\"\n [maxTickLength]=\"maxXAxisTickLength\"\n [tickFormatting]=\"xAxisTickFormatting\"\n [ticks]=\"xAxisTicks\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\"\n ></svg:g>\n <svg:g\n ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [trimTicks]=\"trimYAxisTicks\"\n [maxTickLength]=\"maxYAxisTickLength\"\n [tickFormatting]=\"yAxisTickFormatting\"\n [ticks]=\"yAxisTicks\"\n [yAxisOffset]=\"dataLabelMaxWidth.negative\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"\n ></svg:g>\n <svg:g\n ngx-charts-series-horizontal\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [series]=\"results\"\n [dims]=\"dims\"\n [gradient]=\"gradient\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [activeEntries]=\"activeEntries\"\n [roundEdges]=\"roundEdges\"\n [animations]=\"animations\"\n [showDataLabel]=\"showDataLabel\"\n [dataLabelFormatting]=\"dataLabelFormatting\"\n [noBarWhenZero]=\"noBarWhenZero\"\n (select)=\"onClick($event)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n (dataLabelWidthChanged)=\"onDataLabelMaxWidthChanged($event)\"\n ></svg:g>\n </svg:g>\n </ngx-charts-chart>\n ",
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['../common/base-chart.component.css'],
encapsulation: ViewEncapsulation.None
})
], BarHorizontalComponent);
return BarHorizontalComponent;
}(BaseChartComponent));
export { BarHorizontalComponent };
//# sourceMappingURL=bar-horizontal.component.js.map