D3.js TreeMap - 错误:<rect>属性宽度:预期长度,“NaN”

时间:2017-08-02 16:51:39

标签: javascript reactjs d3.js web

我正在尝试在ReactJS中创建D3中的树形图,下面是我的代码,我收到此错误但无法找出原因:

错误:属性宽度:预期长度,“NaN”。

    const chart = d3.select(this.node)

    const treeMap = d3.treemap()
        .size(this.state.width, this.state.height)

    d3.json("treemap.json", (error, data) => {
        if (error) throw error;
        console.log(data)
        const root = d3.hierarchy(data)
            .sum((d) => { return d.size })

        treeMap(root);
        console.log(root)

        const cell = chart.selectAll("g")
            .data(root.leaves())
            .enter()
            .append("g")

        cell.append("rect")
            .attr("id", (d) => { return d.data.id; })
            .attr("width", (d) => { return d.x1 - d.x0; })
            .attr("height", (d) => { return d.y1 - d.y0; })

任何帮助都会非常感激。谢谢!

编辑:请参阅下面的我的json

 {
                "Skill": "General", "children": [
                    {
                        "Skill": "Dev", "count": 24, "children": [
                            { "Skill": "Java", "count": 13 },
                            { "Skill": "Javascript", "count": 6 },
                            { "Skill": "Analytics", "count": 5 }
                        ]
                    },
                    {
                        "Skill": "Functional", "count": 11, "children": [
                            { "Skill": "Business Analysis", "count": 7 },
                            { "Skill": "PMO", "count": 1 },
                            { "Skill": "Agile", "count": 3 }
                        ]
                    }

                ]
            }

1 个答案:

答案 0 :(得分:1)

两个潜在问题:

一,你的尺码没有正确指定:

   const treeMap = d3.treemap()
        .size(this.state.width, this.state.height)

这应该是一个数组:.size([width,height])

并且,根据您的数据,这里总结了什么:

const root = d3.hierarchy(data)
    .sum((d) => { return d.size })

我相信你试图总结d.count而不是d.size,这是未定义的(运行此函数时请参阅console.log语句)。总而言之,这给出了:

&#13;
&#13;
var data =  { "Skill": "General", "children": [{
  "Skill": "Dev", "count": 24, "children": [
    { "Skill": "Java", "count": 13 },
    { "Skill": "Javascript", "count": 6 },
    { "Skill": "Analytics", "count": 5 }
   ]
  },
  { "Skill": "Functional", "count": 11, "children": [
    { "Skill": "Business Analysis", "count": 7 },
    { "Skill": "PMO", "count": 1 },
    { "Skill": "Agile", "count": 3 }
  ]
 }
]
};

var width = 400; var height = 200;

var chart = d3.select("body")
  .append("svg")
  .attr("width",width)
  .attr("height",height);

const treeMap = d3.treemap()
  .size([width, height])

const root = d3.hierarchy(data)
  .sum((d) => { console.log(d); return d.count })

treeMap(root);


const cell = chart.selectAll("g")
  .data(root.leaves())
  .enter()
  .append("g")
  
color = d3.schemeCategory20;

cell.append("rect")
  .attr("id", (d) => { return d.data.id; })
  .attr("fill", (d,i) => { return color[i%20]; })
  .attr("width", (d) => { return d.x1 - d.x0; })
  .attr("height", (d) => { return d.y1 - d.y0; })
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
&#13;
&#13;
&#13;