@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
149 lines • 8.92 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, ContentChild, ElementRef, HostListener, Input, ViewChild, Output, ViewEncapsulation, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { forceCollide, forceLink, forceManyBody, forceSimulation, forceX, forceY } from 'd3-force';
import { ChartComponent } from '../common/charts/chart.component';
import { BaseChartComponent } from '../common/base-chart.component';
import { calculateViewDimensions } from '../common/view-dimensions.helper';
import { ColorHelper } from '../common/color.helper';
var ForceDirectedGraphComponent = (function (_super) {
__extends(ForceDirectedGraphComponent, _super);
function ForceDirectedGraphComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.force = forceSimulation()
.force('charge', forceManyBody())
.force('collide', forceCollide(5))
.force('x', forceX())
.force('y', forceY());
_this.forceLink = forceLink().id(function (node) { return node.value; });
_this.legendTitle = 'Legend';
_this.nodes = [];
_this.links = [];
_this.activeEntries = [];
_this.tooltipDisabled = false;
_this.activate = new EventEmitter();
_this.deactivate = new EventEmitter();
_this.margin = [0, 0, 0, 0];
_this.results = [];
_this.groupResultsBy = function (node) { return node.value; };
return _this;
}
ForceDirectedGraphComponent.prototype.update = function () {
_super.prototype.update.call(this);
// center graph
this.dims = calculateViewDimensions({
width: this.width,
height: this.height,
margins: this.margin,
showLegend: this.legend,
});
this.seriesDomain = this.getSeriesDomain();
this.setColors();
this.legendOptions = this.getLegendOptions();
this.transform = "\n translate(" + (this.dims.xOffset + this.dims.width / 2) + ", " + (this.margin[0] + this.dims.height / 2) + ")\n ";
if (this.force) {
this.force.nodes(this.nodes)
.force('link', this.forceLink.links(this.links))
.alpha(0.5).restart();
}
};
ForceDirectedGraphComponent.prototype.onClick = function (data) {
this.select.emit(data);
};
ForceDirectedGraphComponent.prototype.onActivate = function (event) {
if (this.activeEntries.indexOf(event) > -1)
return;
this.activeEntries = [event].concat(this.activeEntries);
this.activate.emit({ value: event, entries: this.activeEntries });
};
ForceDirectedGraphComponent.prototype.onDeactivate = function (event) {
var idx = this.activeEntries.indexOf(event);
this.activeEntries.splice(idx, 1);
this.activeEntries = this.activeEntries.slice();
this.deactivate.emit({ value: event, entries: this.activeEntries });
};
ForceDirectedGraphComponent.prototype.getSeriesDomain = function () {
var _this = this;
return this.nodes.map(function (d) { return _this.groupResultsBy(d); })
.reduce(function (nodes, node) { return nodes.includes(node) ? nodes : nodes.concat([node]); }, [])
.sort();
};
ForceDirectedGraphComponent.prototype.trackLinkBy = function (index, link) {
return link.index;
};
ForceDirectedGraphComponent.prototype.trackNodeBy = function (index, node) {
return node.value;
};
ForceDirectedGraphComponent.prototype.setColors = function () {
this.colors = new ColorHelper(this.scheme, 'ordinal', this.seriesDomain, this.customColors);
};
ForceDirectedGraphComponent.prototype.getLegendOptions = function () {
return {
scaleType: 'ordinal',
domain: this.seriesDomain,
colors: this.colors,
title: this.legendTitle
};
};
// Easier to use Angular2 event management than use d3.drag
ForceDirectedGraphComponent.prototype.onDragStart = function (node, $event) {
this.force.alphaTarget(0.3).restart();
this.draggingNode = node;
this.draggingStart = { x: $event.x - node.x, y: $event.y - node.y };
this.draggingNode.fx = $event.x - this.draggingStart.x;
this.draggingNode.fy = $event.y - this.draggingStart.y;
};
ForceDirectedGraphComponent.prototype.onDrag = function ($event) {
if (!this.draggingNode)
return;
this.draggingNode.fx = $event.x - this.draggingStart.x;
this.draggingNode.fy = $event.y - this.draggingStart.y;
};
ForceDirectedGraphComponent.prototype.onDragEnd = function ($event) {
if (!this.draggingNode)
return;
this.force.alphaTarget(0);
this.draggingNode.fx = undefined;
this.draggingNode.fy = undefined;
this.draggingNode = undefined;
};
return ForceDirectedGraphComponent;
}(BaseChartComponent));
export { ForceDirectedGraphComponent };
ForceDirectedGraphComponent.decorators = [
{ type: Component, args: [{
selector: 'ngx-charts-force-directed-graph',
template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\">\n <svg:g [attr.transform]=\"transform\" class=\"force-directed-graph chart\">\n <svg:g class=\"links\">\n <svg:g *ngFor=\"let link of links; trackBy:trackLinkBy\">\n <ng-template *ngIf=\"linkTemplate\"\n [ngTemplateOutlet]=\"linkTemplate\"\n [ngOutletContext]=\"{ $implicit: link }\">\n </ng-template>\n <svg:line *ngIf=\"!linkTemplate\"\n strokeWidth=\"1\" class=\"edge\"\n [attr.x1]=\"link.source.x\"\n [attr.y1]=\"link.source.y\"\n [attr.x2]=\"link.target.x\"\n [attr.y2]=\"link.target.y\"\n />\n </svg:g>\n </svg:g>\n <svg:g class=\"nodes\">\n <svg:g *ngFor=\"let node of nodes; trackBy:trackNodeBy\"\n [attr.transform]=\"'translate(' + node.x + ',' + node.y + ')'\"\n [attr.fill]=\"colors.getColor(groupResultsBy(node))\"\n [attr.stroke]=\"colors.getColor(groupResultsBy(node))\"\n (mousedown)=\"onDragStart(node, $event)\"\n (click)=\"onClick({name: node.value})\"\n ngx-tooltip\n [tooltipDisabled]=\"tooltipDisabled\"\n [tooltipPlacement]=\"'top'\"\n [tooltipType]=\"'tooltip'\"\n [tooltipTitle]=\"tooltipTemplate ? undefined : node.value\"\n [tooltipTemplate]=\"tooltipTemplate\"\n [tooltipContext]=\"node\">\n <ng-template *ngIf=\"nodeTemplate\"\n [ngTemplateOutlet]=\"nodeTemplate\"\n [ngOutletContext]=\"{ $implicit: node }\">\n </ng-template>\n <svg:circle *ngIf=\"!nodeTemplate\" r=\"5\" />\n </svg:g>\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ",
styleUrls: [
'../common/base-chart.component.css',
'./force-directed-graph.component.css'
],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
},] },
];
/** @nocollapse */
ForceDirectedGraphComponent.ctorParameters = function () { return []; };
ForceDirectedGraphComponent.propDecorators = {
'force': [{ type: Input },],
'forceLink': [{ type: Input },],
'legend': [{ type: Input },],
'legendTitle': [{ type: Input },],
'nodes': [{ type: Input },],
'links': [{ type: Input },],
'activeEntries': [{ type: Input },],
'tooltipDisabled': [{ type: Input },],
'activate': [{ type: Output },],
'deactivate': [{ type: Output },],
'linkTemplate': [{ type: ContentChild, args: ['linkTemplate',] },],
'nodeTemplate': [{ type: ContentChild, args: ['nodeTemplate',] },],
'tooltipTemplate': [{ type: ContentChild, args: ['tooltipTemplate',] },],
'chart': [{ type: ViewChild, args: [ChartComponent, { read: ElementRef },] },],
'groupResultsBy': [{ type: Input },],
'onDrag': [{ type: HostListener, args: ['document:mousemove', ['$event'],] },],
'onDragEnd': [{ type: HostListener, args: ['document:mouseup', ['$event'],] },],
};
//# sourceMappingURL=force-directed-graph.component.js.map