javascript - D3 TreeMap v3 到 v4

标签 javascript d3.js pivottable.js

我是 D3 的新手,我正在尝试将 D3 Treemap 从 V3 转换为 V4。
我已经阅读了一些信息更新了更新的代码,但我不知道让它工作。
树形图来自 Pivottable.js D3 渲染器。
这是部分。

    color = d3.scale.category10();
    width = opts.d3.width();
    height = opts.d3.height();
    treemap = d3.layout.treemap().size([width, height]).sticky(true).value(function(d) {
      return d.size;
    });
    d3.select(result[0]).append("div").style("position", "relative").style("width", width + "px").style("height", height + "px").datum(tree).selectAll(".node").data(treemap.padding([15, 0, 0, 0]).value(function(d) {
      return d.value;
    }).nodes).enter().append("div").attr("class", "node").style("background", function(d) {
      if (d.children != null) {
        return "lightgrey";
      } else {
        return color(d.name);
      }
    }).text(function(d) {
      return d.name;
    }).call(function() {
      this.style("left", function(d) {
        return d.x + "px";
      }).style("top", function(d) {
        return d.y + "px";
      }).style("width", function(d) {
        return Math.max(0, d.dx - 1) + "px";
      }).style("height", function(d) {
        return Math.max(0, d.dy - 1) + "px";
      });
    });
使用 D3 V4 我会遇到一些错误。
例如“d3.scale.category10”不是一个函数。
我将其替换为“d3.scaleOrdinal(d3.schemeCategory10)”。
然后“粘性不是功能”。
我用“tile(d3.treemapResquarify)”替换了它。
但我也得到“值不是函数”和“输入不是函数”,这是我无法解决的。
而且我不确定是否必须更改其他任何内容才能使其与 D3 V4 一起使用。
这是我现在的代码。但它不起作用。
    // Updated to V4
    color = d3.scaleOrdinal(d3.schemeCategory10);
    width = opts.d3.width();
    height = opts.d3.height();

    // Updated sticky for V4
    treemap = d3.treemap().size([width, height]).tile(d3.treemapResquarify).value(function(d) {
      return d.size;
    });
    d3.select(result[0]).append("div").style("position", "relative").style("width", width + "px").style("height", height + "px").datum(tree).selectAll(".node").data(treemap.padding([15, 0, 0, 0]).value(function(d) {
      return d.value;
    }).nodes).enter().append("div").attr("class", "node").style("background", function(d) {
      if (d.children != null) {
        return "lightgrey";
      } else {
        return color(d.name);
      }
    }).text(function(d) {
      return d.name;
    }).call(function() {
      this.style("left", function(d) {
        return d.x + "px";
      }).style("top", function(d) {
        return d.y + "px";
      }).style("width", function(d) {
        return Math.max(0, d.dx - 1) + "px";
      }).style("height", function(d) {
        return Math.max(0, d.dy - 1) + "px";
      });
    });
任何人都可以帮我转换其余的吗?
//编辑
这是有效的 D3 v3 示例 fiddle 。 d3_renderer 在 JS 部分。
https://jsfiddle.net/qntc8e7w/
这是相同的示例,但使用 D3 v4
https://jsfiddle.net/ybctz0a8/

最佳答案

  /* The following code is written on the d3.v4.js plugin   */
                 treemap = d3.treemap().size([width, height]).paddingTop(15)
                .paddingInner(1);
                var div = d3.select(result[0]).append("div")
                .style("position", "relative")
                .style("width", (width + margin.left + margin.right) + "px")
                .style("height", (height + margin.top + margin.bottom) + "px")
                .style("left", margin.left + "px")
                .style("top", margin.top + "px");
    
                var root = d3.hierarchy(tree)
                root.sum(function(d) {
                    return d.value;
                });
                var treeObj = treemap(root);
                node = div.datum(root).selectAll(".node")
                .data(treeObj.descendants())
                .enter().append("div")
                .attr("class", "node")
                .style("left", (d) => d.x0 + "px")
                .style("top", (d) => d.y0 + "px")
                .style("width", (d) => Math.max(0, d.x1 - d.x0 - 1) + "px")
                .style("height", (d) => Math.max(0, d.y1 - d.y0  - 1) + "px")
                .style("cursor","pointer")
                .style("background", function(d) {
                    if (d.children != null) {
                        return "lightgrey";
                    } else {
                        return color(d.data.name);
                    }
                })
                .text(function(d) {
                    return d.data.name +" ( "+d.value +" )";
                })

关于javascript - D3 TreeMap v3 到 v4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62502071/

相关文章:

javascript - 在一定数量之后在新行/列中呈现枢轴属性

javascript - 使用javascript在数组中查找一组对象

javascript - 从另一个具有值的数组的键创建一个数组

javascript - AngularJS SPA 登录页面

javascript - 解析保存方法给出 400 错误

javascript - IE9 的 foreignObject 替代品

javascript - 如何为图形的整行设置一个标签

javascript - 如何在 d3.js 中像这个 gif 一样使圆圈动画连续悬停?