D3JS的阳光与扭曲> v4

发布于 2025-01-31 18:49:30 字数 1940 浏览 6 评论 0 原文

我尝试几天将这个漂亮的阳台D3 V3示例转换为D3 V4的新版本:

,但由于DX和D.DX不再存在于边缘层次结构上,因此我无法使其起作用。这里有什么样的开发人员可以将我指向解决方案?如何更改事物以使该示例在V4及以上工作?

我这么远(我认为X1/X0无法正确计算 +之间无法以某种方式工作):

    // Recursively reposition the node at position x with scale k.
const reposition = (node:any, x: any, k:any) => {
  node.x0 = x;
  if (node.children && (n = node.children.length)) {
    var i = -1, n;
    while (++i < n) x += reposition(node.children[i], x, k);
  }
  return node.x1 = node.value * k;
}

// Stash the old values for transition.
const stash = (d: any) => {
  d.x00 = d.x0;
  d.dx10 = d.x1;
  return d;
}

// Interpolate the arcs in data space.
const arcTween = (a: any):any => {
  var i = d3.interpolate({x0: a.x00, x1: a.dx10}, a);
  return (t:any):any => {
    var b = i(t);
    a.x00 = b.x0;
    a.dx10 = b.x1;
    return arc(b);
  };
}


const magnify = (node:any) => {
  if (node.parent) {
    var parent = node.parent,
        x = parent.x0,
        k = .8;
    parent.children.forEach((sibling: any) => {
      x += reposition(sibling, x, sibling === node
          ? parent.x1 * k / node.value
          : parent.x1 * (1 - k) / (parent.value - node.value));
    });
  } else {
    reposition(node, 0, node.x1 / node.value);
  }

  //node.parent.children[0].x1 =  node.parent.children[0].x1 +1
  // node.parent.children[1].x0 =  node.parent.children[1].x0 +1
//sunburst.remove();
  sunburst.append("g").selectAll("path").data(annotatedCastedHierarchy)
      .join("path").transition()
      .duration(750)
      .attr("d", d => arc(d)) .attr("fill", d => {
                    if (d.data.fqn == clicked){
                        return "#007dbc"
                    } else {
                        return d.data.col
                    }
            });
}

I try for days to convert this nice sunburst d3 v3 example to the new version of d3 v4:

https://bl.ocks.org/mbostock/1306365

But i cannot get it work as d.x and d.dx no longer exists on a parititioned hierarchy. Any kind developer here who can point me towards a solution? how to change things to get that example working in v4 and above?

I am this far(i think x1/x0 are not properly computed + arctween do not work somehow) :

    // Recursively reposition the node at position x with scale k.
const reposition = (node:any, x: any, k:any) => {
  node.x0 = x;
  if (node.children && (n = node.children.length)) {
    var i = -1, n;
    while (++i < n) x += reposition(node.children[i], x, k);
  }
  return node.x1 = node.value * k;
}

// Stash the old values for transition.
const stash = (d: any) => {
  d.x00 = d.x0;
  d.dx10 = d.x1;
  return d;
}

// Interpolate the arcs in data space.
const arcTween = (a: any):any => {
  var i = d3.interpolate({x0: a.x00, x1: a.dx10}, a);
  return (t:any):any => {
    var b = i(t);
    a.x00 = b.x0;
    a.dx10 = b.x1;
    return arc(b);
  };
}


const magnify = (node:any) => {
  if (node.parent) {
    var parent = node.parent,
        x = parent.x0,
        k = .8;
    parent.children.forEach((sibling: any) => {
      x += reposition(sibling, x, sibling === node
          ? parent.x1 * k / node.value
          : parent.x1 * (1 - k) / (parent.value - node.value));
    });
  } else {
    reposition(node, 0, node.x1 / node.value);
  }

  //node.parent.children[0].x1 =  node.parent.children[0].x1 +1
  // node.parent.children[1].x0 =  node.parent.children[1].x0 +1
//sunburst.remove();
  sunburst.append("g").selectAll("path").data(annotatedCastedHierarchy)
      .join("path").transition()
      .duration(750)
      .attr("d", d => arc(d)) .attr("fill", d => {
                    if (d.data.fqn == clicked){
                        return "#007dbc"
                    } else {
                        return d.data.col
                    }
            });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2025-02-07 18:49:30

对于任何有兴趣的人,我完成了与V4合作的工作。这是我的代码:

// Recursively reposition the node at position x with scale k.
const reposition = (node:any, x: any, k:any) => {
  node.x0 = x;
  if (node.children && (n = node.children.length)) {
    var i = -1, n;
    while (++i < n) x += reposition(node.children[i], x, k);
  }
  node.x1 = node.x0 + node.value * k;
  return node.value * k;
}

// Stash the old values for transition.
const stash = (d: any) => {
  d.x00 = d.x0;
  d.dx10 = d.x1;
  return d;
}

// Interpolate the arcs in data space.
const arcTween = (a: any):any => {
  var i = d3.interpolate({x00: a.x0, dx10: a.x1}, a);
  return (t:any):any => {
    var b = i(t);
    a.x00 = b.x0;
    a.dx10 = b.x1;
    return arc(b);
  };
}


const magnify = (node:any) => {
  if (node.parent) {
    var parent = node.parent,
        x = parent.x0,
        k = .8;
    parent.children.forEach((sibling: any) => {
      x += reposition(sibling, x, sibling === node
          ? (parent.x1 - parent.x0) * k / node.value
          : (parent.x1 - parent.x0) * (1 - k) / (parent.value - node.value));
    });
  } else {
    reposition(node, 0, node.x1 / node.value);
  }
  
  sunburst.selectAll("path")
      .transition()
      .duration(750)
      .attrTween("d", arcTween)
}

For anyone interested I accomplished to get it working with v4. Here is my code:

// Recursively reposition the node at position x with scale k.
const reposition = (node:any, x: any, k:any) => {
  node.x0 = x;
  if (node.children && (n = node.children.length)) {
    var i = -1, n;
    while (++i < n) x += reposition(node.children[i], x, k);
  }
  node.x1 = node.x0 + node.value * k;
  return node.value * k;
}

// Stash the old values for transition.
const stash = (d: any) => {
  d.x00 = d.x0;
  d.dx10 = d.x1;
  return d;
}

// Interpolate the arcs in data space.
const arcTween = (a: any):any => {
  var i = d3.interpolate({x00: a.x0, dx10: a.x1}, a);
  return (t:any):any => {
    var b = i(t);
    a.x00 = b.x0;
    a.dx10 = b.x1;
    return arc(b);
  };
}


const magnify = (node:any) => {
  if (node.parent) {
    var parent = node.parent,
        x = parent.x0,
        k = .8;
    parent.children.forEach((sibling: any) => {
      x += reposition(sibling, x, sibling === node
          ? (parent.x1 - parent.x0) * k / node.value
          : (parent.x1 - parent.x0) * (1 - k) / (parent.value - node.value));
    });
  } else {
    reposition(node, 0, node.x1 / node.value);
  }
  
  sunburst.selectAll("path")
      .transition()
      .duration(750)
      .attrTween("d", arcTween)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文