Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Angular Typescript with D3.js V.4.12 and I am particularly using the Tidy Radial Tree for representing a product.

Initial Steps

along with ng-cli, I installed npm install --save d3 and created a component to display the information.

The visualization is shown below:

d3 tidy tree for product

The respective 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 takes in a JSON Response from the server and flattens the heirarchical structure.

I am using .transistion() to on the nodes such that a single click on the node will increase the node radius and a double click will reduce the radius back to standard size.

I do not retrieve any errors in the console and obtain the information of the node correctly via the console.log() calls in both the functions.

However what I find strange is that the Browser Inspector shows the same g components being produced twice. Maybe this might be a problem but I don't see any transitions happening upon clicks.

browser inspector


1 Answers

When you set up your click handler like this:

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

The fat-arrow notation preserves the context of this, so it's referring to the instance of your class.

Your handler, though:

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

Is expecting this to be the g that was clicked.

So, set up your handler like:

.on("click", click)
like image 60
Mark Avatar answered May 16 '26 19:05

Mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!