@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular
472 lines • 23.1 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, HostListener, ChangeDetectionStrategy, ContentChild, TemplateRef } from '@angular/core';
import { trigger, style, animate, transition } from '@angular/animations';
import { scaleLinear, scaleTime, scalePoint } from 'd3-scale';
import { curveLinear } from 'd3-shape';
import { calculateViewDimensions } from '../common/view-dimensions.helper';
import { ColorHelper } from '../common/color.helper';
import { BaseChartComponent } from '../common/base-chart.component';
import { id } from '../utils/id';
import { getUniqueXDomainValues, getScaleType } from '../common/domain.helper';
var LineChartComponent = /** @class */ (function (_super) {
__extends(LineChartComponent, _super);
function LineChartComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.legendTitle = 'Legend';
_this.legendPosition = 'right';
_this.showGridLines = true;
_this.curve = curveLinear;
_this.activeEntries = [];
_this.trimXAxisTicks = true;
_this.trimYAxisTicks = true;
_this.rotateXAxisTicks = true;
_this.maxXAxisTickLength = 16;
_this.maxYAxisTickLength = 16;
_this.roundDomains = false;
_this.tooltipDisabled = false;
_this.showRefLines = false;
_this.showRefLabels = true;
_this.activate = new EventEmitter();
_this.deactivate = new EventEmitter();
_this.margin = [10, 20, 10, 20];
_this.xAxisHeight = 0;
_this.yAxisWidth = 0;
_this.timelineHeight = 50;
_this.timelinePadding = 10;
return _this;
}
LineChartComponent.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,
legendPosition: this.legendPosition
});
if (this.timeline) {
this.dims.height -= this.timelineHeight + this.margin[2] + this.timelinePadding;
}
this.xDomain = this.getXDomain();
if (this.filteredDomain) {
this.xDomain = this.filteredDomain;
}
this.yDomain = this.getYDomain();
this.seriesDomain = this.getSeriesDomain();
this.xScale = this.getXScale(this.xDomain, this.dims.width);
this.yScale = this.getYScale(this.yDomain, this.dims.height);
this.updateTimeline();
this.setColors();
this.legendOptions = this.getLegendOptions();
this.transform = "translate(" + this.dims.xOffset + " , " + this.margin[0] + ")";
this.clipPathId = 'clip' + id().toString();
this.clipPath = "url(#" + this.clipPathId + ")";
};
LineChartComponent.prototype.updateTimeline = function () {
if (this.timeline) {
this.timelineWidth = this.dims.width;
this.timelineXDomain = this.getXDomain();
this.timelineXScale = this.getXScale(this.timelineXDomain, this.timelineWidth);
this.timelineYScale = this.getYScale(this.yDomain, this.timelineHeight);
this.timelineTransform = "translate(" + this.dims.xOffset + ", " + -this.margin[2] + ")";
}
};
LineChartComponent.prototype.getXDomain = function () {
var values = getUniqueXDomainValues(this.results);
this.scaleType = getScaleType(values);
var domain = [];
if (this.scaleType === 'linear') {
values = values.map(function (v) { return Number(v); });
}
var min;
var max;
if (this.scaleType === 'time' || this.scaleType === 'linear') {
min = this.xScaleMin ? this.xScaleMin : Math.min.apply(Math, values);
max = this.xScaleMax ? this.xScaleMax : Math.max.apply(Math, values);
}
if (this.scaleType === 'time') {
domain = [new Date(min), new Date(max)];
this.xSet = values.slice().sort(function (a, b) {
var aDate = a.getTime();
var bDate = b.getTime();
if (aDate > bDate)
return 1;
if (bDate > aDate)
return -1;
return 0;
});
}
else if (this.scaleType === 'linear') {
domain = [min, max];
// Use compare function to sort numbers numerically
this.xSet = values.slice().sort(function (a, b) { return a - b; });
}
else {
domain = values;
this.xSet = values;
}
return domain;
};
LineChartComponent.prototype.getYDomain = function () {
var domain = [];
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 (domain.indexOf(d.value) < 0) {
domain.push(d.value);
}
if (d.min !== undefined) {
this.hasRange = true;
if (domain.indexOf(d.min) < 0) {
domain.push(d.min);
}
}
if (d.max !== undefined) {
this.hasRange = true;
if (domain.indexOf(d.max) < 0) {
domain.push(d.max);
}
}
}
}
var values = domain.slice();
if (!this.autoScale) {
values.push(0);
}
var min = this.yScaleMin ? this.yScaleMin : Math.min.apply(Math, values);
var max = this.yScaleMax ? this.yScaleMax : Math.max.apply(Math, values);
return [min, max];
};
LineChartComponent.prototype.getSeriesDomain = function () {
return this.results.map(function (d) { return d.name; });
};
LineChartComponent.prototype.getXScale = function (domain, width) {
var scale;
if (this.scaleType === 'time') {
scale = scaleTime()
.range([0, width])
.domain(domain);
}
else if (this.scaleType === 'linear') {
scale = scaleLinear()
.range([0, width])
.domain(domain);
if (this.roundDomains) {
scale = scale.nice();
}
}
else if (this.scaleType === 'ordinal') {
scale = scalePoint()
.range([0, width])
.padding(0.1)
.domain(domain);
}
return scale;
};
LineChartComponent.prototype.getYScale = function (domain, height) {
var scale = scaleLinear()
.range([height, 0])
.domain(domain);
return this.roundDomains ? scale.nice() : scale;
};
LineChartComponent.prototype.updateDomain = function (domain) {
this.filteredDomain = domain;
this.xDomain = this.filteredDomain;
this.xScale = this.getXScale(this.xDomain, this.dims.width);
};
LineChartComponent.prototype.updateHoveredVertical = function (item) {
this.hoveredVertical = item.value;
this.deactivateAll();
};
LineChartComponent.prototype.hideCircles = function () {
this.hoveredVertical = null;
this.deactivateAll();
};
LineChartComponent.prototype.onClick = function (data) {
this.select.emit(data);
};
LineChartComponent.prototype.trackBy = function (index, item) {
return item.name;
};
LineChartComponent.prototype.setColors = function () {
var domain;
if (this.schemeType === 'ordinal') {
domain = this.seriesDomain;
}
else {
domain = this.yDomain;
}
this.colors = new ColorHelper(this.scheme, this.schemeType, domain, this.customColors);
};
LineChartComponent.prototype.getLegendOptions = function () {
var opts = {
scaleType: this.schemeType,
colors: undefined,
domain: [],
title: undefined,
position: this.legendPosition
};
if (opts.scaleType === 'ordinal') {
opts.domain = this.seriesDomain;
opts.colors = this.colors;
opts.title = this.legendTitle;
}
else {
opts.domain = this.yDomain;
opts.colors = this.colors.scale;
}
return opts;
};
LineChartComponent.prototype.updateYAxisWidth = function (_a) {
var width = _a.width;
this.yAxisWidth = width;
this.update();
};
LineChartComponent.prototype.updateXAxisHeight = function (_a) {
var height = _a.height;
this.xAxisHeight = height;
this.update();
};
LineChartComponent.prototype.onActivate = function (item) {
this.deactivateAll();
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name && d.value === item.value;
});
if (idx > -1) {
return;
}
this.activeEntries = [item];
this.activate.emit({ value: item, entries: this.activeEntries });
};
LineChartComponent.prototype.onDeactivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name && d.value === item.value;
});
this.activeEntries.splice(idx, 1);
this.activeEntries = this.activeEntries.slice();
this.deactivate.emit({ value: item, entries: this.activeEntries });
};
LineChartComponent.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 = [];
};
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "legend", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], LineChartComponent.prototype, "legendTitle", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], LineChartComponent.prototype, "legendPosition", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "xAxis", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "yAxis", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "showXAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "showYAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "xAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "yAxisLabel", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "autoScale", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "timeline", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "gradient", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "showGridLines", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "curve", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], LineChartComponent.prototype, "activeEntries", void 0);
__decorate([
Input(),
__metadata("design:type", String)
], LineChartComponent.prototype, "schemeType", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], LineChartComponent.prototype, "rangeFillOpacity", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "trimXAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "trimYAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "rotateXAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], LineChartComponent.prototype, "maxXAxisTickLength", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], LineChartComponent.prototype, "maxYAxisTickLength", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "xAxisTickFormatting", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "yAxisTickFormatting", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], LineChartComponent.prototype, "xAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Array)
], LineChartComponent.prototype, "yAxisTicks", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "roundDomains", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "tooltipDisabled", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "showRefLines", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "referenceLines", void 0);
__decorate([
Input(),
__metadata("design:type", Boolean)
], LineChartComponent.prototype, "showRefLabels", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "xScaleMin", void 0);
__decorate([
Input(),
__metadata("design:type", Object)
], LineChartComponent.prototype, "xScaleMax", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], LineChartComponent.prototype, "yScaleMin", void 0);
__decorate([
Input(),
__metadata("design:type", Number)
], LineChartComponent.prototype, "yScaleMax", void 0);
__decorate([
Output(),
__metadata("design:type", EventEmitter)
], LineChartComponent.prototype, "activate", void 0);
__decorate([
Output(),
__metadata("design:type", EventEmitter)
], LineChartComponent.prototype, "deactivate", void 0);
__decorate([
ContentChild('tooltipTemplate', { static: false }),
__metadata("design:type", TemplateRef)
], LineChartComponent.prototype, "tooltipTemplate", void 0);
__decorate([
ContentChild('seriesTooltipTemplate', { static: false }),
__metadata("design:type", TemplateRef)
], LineChartComponent.prototype, "seriesTooltipTemplate", void 0);
__decorate([
HostListener('mouseleave'),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], LineChartComponent.prototype, "hideCircles", null);
LineChartComponent = __decorate([
Component({
selector: 'ngx-charts-line-chart',
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)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\"\n >\n <svg:defs>\n <svg:clipPath [attr.id]=\"clipPathId\">\n <svg:rect\n [attr.width]=\"dims.width + 10\"\n [attr.height]=\"dims.height + 10\"\n [attr.transform]=\"'translate(-5, -5)'\"\n />\n </svg:clipPath>\n </svg:defs>\n <svg:g [attr.transform]=\"transform\" class=\"line-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 [showGridLines]=\"showGridLines\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n [trimTicks]=\"trimYAxisTicks\"\n [maxTickLength]=\"maxYAxisTickLength\"\n [tickFormatting]=\"yAxisTickFormatting\"\n [ticks]=\"yAxisTicks\"\n [referenceLines]=\"referenceLines\"\n [showRefLines]=\"showRefLines\"\n [showRefLabels]=\"showRefLabels\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\"\n ></svg:g>\n <svg:g [attr.clip-path]=\"clipPath\">\n <svg:g *ngFor=\"let series of results; trackBy: trackBy\" [@animationState]=\"'active'\">\n <svg:g\n ngx-charts-line-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [activeEntries]=\"activeEntries\"\n [scaleType]=\"scaleType\"\n [curve]=\"curve\"\n [rangeFillOpacity]=\"rangeFillOpacity\"\n [hasRange]=\"hasRange\"\n [animations]=\"animations\"\n />\n </svg:g>\n\n <svg:g *ngIf=\"!tooltipDisabled\" (mouseleave)=\"hideCircles()\">\n <svg:g\n ngx-charts-tooltip-area\n [dims]=\"dims\"\n [xSet]=\"xSet\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [results]=\"results\"\n [colors]=\"colors\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"seriesTooltipTemplate\"\n (hover)=\"updateHoveredVertical($event)\"\n />\n\n <svg:g *ngFor=\"let series of results\">\n <svg:g\n ngx-charts-circle-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [visibleValue]=\"hoveredVertical\"\n [activeEntries]=\"activeEntries\"\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipTemplate]=\"tooltipTemplate\"\n (select)=\"onClick($event)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n />\n </svg:g>\n </svg:g>\n </svg:g>\n </svg:g>\n <svg:g\n ngx-charts-timeline\n *ngIf=\"timeline && scaleType != 'ordinal'\"\n [attr.transform]=\"timelineTransform\"\n [results]=\"results\"\n [view]=\"[timelineWidth, height]\"\n [height]=\"timelineHeight\"\n [scheme]=\"scheme\"\n [customColors]=\"customColors\"\n [scaleType]=\"scaleType\"\n [legend]=\"legend\"\n (onDomainChange)=\"updateDomain($event)\"\n >\n <svg:g *ngFor=\"let series of results; trackBy: trackBy\">\n <svg:g\n ngx-charts-line-series\n [xScale]=\"timelineXScale\"\n [yScale]=\"timelineYScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [curve]=\"curve\"\n [hasRange]=\"hasRange\"\n [animations]=\"animations\"\n />\n </svg:g>\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
}),
animate(500, style({
opacity: 0
}))
])
])
]
})
], LineChartComponent);
return LineChartComponent;
}(BaseChartComponent));
export { LineChartComponent };
//# sourceMappingURL=line-chart.component.js.map