@amcharts/amcharts5
Version:
amCharts 5
137 lines • 4.87 kB
JavaScript
import { __awaiter } from "tslib";
import { LinkedHierarchy } from "./LinkedHierarchy";
import * as $array from "../../core/util/Array";
import * as $type from "../../core/util/Type";
import * as d3hierarchy from "d3-hierarchy";
;
/**
* Displays a tree diagram.
*
* @see {@link https://www.amcharts.com/docs/v5/charts/hierarchy/tree/} for more info
* @important
*/
export class Tree extends LinkedHierarchy {
constructor() {
super(...arguments);
this._tag = "tree";
this._hierarchyLayout = d3hierarchy.tree();
}
_prepareChildren() {
if (this.isDirty("fitNodes")) {
this._valuesDirty = true;
}
super._prepareChildren();
if (this.isDirty("clustered")) {
this._hierarchyLayout = this.get("clustered") ? d3hierarchy.cluster() : d3hierarchy.tree();
this._updateVisuals();
}
if (this.isDirty("orientation") || this.isDirty("inversed") || this.isDirty("nodeSeparation")) {
this._updateVisuals();
}
}
_makeHierarchyData(data, dataItem) {
if (!this.get("fitNodes")) {
super._makeHierarchyData(data, dataItem);
return;
}
data.dataItem = dataItem;
const children = dataItem.get("children");
if (children) {
const childrenDataArray = [];
data.children = childrenDataArray;
$array.each(children, (childDataItem) => {
if (childDataItem.isHidden()) {
return;
}
const node = childDataItem.get("node");
if (node && node.isHidden()) {
return;
}
const childData = {};
childrenDataArray.push(childData);
this._makeHierarchyData(childData, childDataItem);
});
}
const value = dataItem.get("valueWorking");
if ($type.isNumber(value)) {
data.value = value;
}
}
hideDataItem(dataItem, duration) {
const _super = Object.create(null, {
hideDataItem: { get: () => super.hideDataItem }
});
return __awaiter(this, void 0, void 0, function* () {
const result = _super.hideDataItem.call(this, dataItem, duration);
if (this.get("fitNodes")) {
this.markDirtyValues();
}
return result;
});
}
showDataItem(dataItem, duration) {
const _super = Object.create(null, {
showDataItem: { get: () => super.showDataItem }
});
return __awaiter(this, void 0, void 0, function* () {
const result = _super.showDataItem.call(this, dataItem, duration);
if (this.get("fitNodes")) {
this.markDirtyValues();
}
return result;
});
}
disableDataItem(dataItem, duration) {
super.disableDataItem(dataItem, duration);
if (this.get("fitNodes")) {
this.markDirtyValues();
}
}
enableDataItem(dataItem, maxDepth, depth, duration) {
super.enableDataItem(dataItem, maxDepth, depth, duration);
if (this.get("fitNodes")) {
this.markDirtyValues();
}
}
_updateVisuals() {
if (this._rootNode) {
const layout = this._hierarchyLayout;
if (this.get("orientation") == "vertical") {
layout.size([this.innerWidth(), this.innerHeight()]);
}
else {
layout.size([this.innerHeight(), this.innerWidth()]);
}
const nodeSeparation = this.get("nodeSeparation");
if (nodeSeparation) {
layout.separation((a, b) => {
return nodeSeparation(a.data.dataItem, b.data.dataItem);
});
}
layout(this._rootNode);
}
super._updateVisuals();
}
_getPoint(hierarchyNode) {
const inversed = this.get("inversed");
if (this.get("orientation") == "vertical") {
if (inversed) {
return { x: hierarchyNode.x, y: this.innerHeight() - hierarchyNode.y };
}
else {
return { x: hierarchyNode.x, y: hierarchyNode.y };
}
}
else {
if (inversed) {
return { x: this.innerWidth() - hierarchyNode.y, y: hierarchyNode.x };
}
else {
return { x: hierarchyNode.y, y: hierarchyNode.x };
}
}
}
}
Tree.className = "Tree";
Tree.classNames = LinkedHierarchy.classNames.concat([Tree.className]);
//# sourceMappingURL=Tree.js.map