Sha*_*sai 3 d3.js typescript angular
我在D3.js V.4.12中使用了Angular Typescript,尤其是在使用Tidy Radial Tree表示产品时。
与一起ng-cli,我安装npm install --save d3并创建了一个组件来显示信息。
可视化效果如下所示:
各个组件如下:
treediag.component.ts
import { Component, OnInit, ViewEncapsulation} from '@angular/core';
import * as d3 from 'd3';
import { ont, ont_highchair } from '../fd-ontology/ontology';
import { recursionParse, ontNode } from './model/recursion';
export class leaf {
name: string;
url: string;
color: string;
children: leaf[] = [];
}
@Component({
selector: 'app-treediag',
templateUrl: './treediag.component.html',
styleUrls: ['./treediag.component.css'],
encapsulation: ViewEncapsulation.None
})
export class TreediagComponent implements OnInit {
prop = {name: 'test'};
constructor() {
}
ngOnInit() {
var i = 0,
duration = 750, root;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(" + (width / 2 + 40) + "," + (height / 2 + 90) + ")");
var tree = d3.tree()
.size([2 * Math.PI, 400])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 10) / a.depth; });
root = tree(d3.hierarchy(this.parse_node(ont_highchair.completeStructure)));
// root.children.forEach(collapse);
// update(root);
var link = g.selectAll(".link")
.data(root.links())
.enter().append("path")
.attr("class", "link")
.attr("d", d3.linkRadial()
.angle(function(d) { return d.x; })
.radius(function(d) { return d.y; }));
var node = g.selectAll(".node")
.data(root.descendants())
.enter().append("g")
.attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) { return "translate(" + radialPoint(d.x, d.y) + ")"; })
.on("click", (d) => click(d))
.on("dblclick", (d) => dblclick(d));
node.append("circle")
.attr("r", 5)
.style("fill", (d) => {
if (d.data.color === 'green') {
return '#0f0';
} else {
if (d.depth === 0) {
return '#999';
}
return '#f00';
}
});
node.append("text")
.attr("dy", "0.31em")
.attr("x", function(d) { return d.x < Math.PI === !d.children ? 6 : -6; })
.attr("text-anchor", function(d) { return d.x < Math.PI === !d.children ? "start" : "end"; })
.attr("transform", function(d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 180 / Math.PI + ")"; })
.text(function(d) { return d.data.name; });
function radialPoint(x, y) {
return [(y = +y) * Math.cos(x -= Math.PI / 2), y * Math.sin(x)];
}
/* PROBLEM HERE*/
function click(d) {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16);
}
/* PROBLEM HERE */
function dblclick(d) {
console.log(d.data);
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 6);
}
}
Run Code Online (Sandbox Code Playgroud)
this.parse_node()只是一个从服务器接收JSON响应并展平层次结构的函数。
我习惯于.transistion()在节点上单击,以使节点上的半径增大,双击将半径减小到标准尺寸。
我没有在控制台中检索任何错误,也没有通过console.log()两个函数中的调用正确获取节点的信息。
但是,我发现奇怪的是,浏览器检查器显示了g两次生产的相同组件。也许这可能是一个问题,但是我看不到点击发生任何过渡。
当您按如下方式设置点击处理程序时:
.on("click", (d) => click(d))
Run Code Online (Sandbox Code Playgroud)
粗箭头表示法保留了的上下文this,因此它表示您的类的实例。
但是,您的处理程序:
function click(d) {
d3.select(this).select("circle").transition()
.duration(750)
.attr("r", 16);
}
Run Code Online (Sandbox Code Playgroud)
期望this是g被点击的。
因此,像这样设置您的处理程序:
.on("click", click)
Run Code Online (Sandbox Code Playgroud)