Node Transition does not happen with D3.js V4 and Angular

I use Angular Typescript with D3.js V.4.12 , and I especially use the Tidy Radial Tree to represent the product.

Initial steps

Together with ng-cli, I installed npm install --save d3and created a component for displaying information.

The visualization is shown below:

d3 tidy tree for product

The corresponding component is as follows:

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);
    }
}

this.parse_node()is just a function that receives a JSON response from the server and aligns the hierarchical structure.

I use .transistion()for such nodes that a single click on a node will increase the radius of the node, and a double click will reduce the radius to a standard size.

node console.log() .

, , , Browser Inspector g , . , , , .

browser inspector

+1
1

:

.on("click", (d) => click(d))

this, .

:

function click(d) {
    d3.select(this).select("circle").transition()
        .duration(750)
        .attr("r", 16);
}

, this g.

, :

.on("click", click)
+4

Source: https://habr.com/ru/post/1654279/


All Articles