简体   繁体   English

使用Pack的D3 v5气泡图未呈现

[英]D3 v5 bubble chart using pack not rendering

I am trying to render a bubble chart using .pack() in D3 v5. 我正在尝试在D3 v5中使用.pack()渲染气泡图。 The tutorial I'm following is outdated and uses v3 APIs which are no longer supported. 我正在关注的教程已经过时,并使用了不再受支持的v3 API。

I got stuck in rendering the nodes part, 我被困在渲染节点部分,

This is my data fetch code: 这是我的数据获取代码:

d3.json('/data').then((quotes) => {
    const root = d3.hierarchy(quotes).sum(d => Number(d.price));
    bubble(root);
    const node = svg.selectAll('.node')
      .data(root.descendants())
      .filter(d => !d.children)
      .enter().append('g')
      .attr('class', 'node')
      .each(function (d) { d.node = this })
      .attr('transform', d => `translate(${d.x}, ${d.y})`)

This is my bubble: 这是我的泡泡:

//declare layout
  const bubble = d3.pack()
    .size([width, height])
    .padding(1)
    .radius((d) => 20 + (sizeOfRadius(d) * 30))

And this is a subset of the data I'm trying to render: 这是我要呈现的数据的子集:

children: [
{
name: "Activision Blizzard Inc",
net_change: "-0.65",
percent_change: "-1.36",
price: "47.1",
symbol: "ATVI",
value: "-.4",
volume: "9458326"
},
]

My browser isn't showing any error and the svg container is empty. 我的浏览器没有显示任何错误,并且svg容器为空。 My guess is that I'm not feeding the correct structure of data into d3.hierarchy() but after some digging online, I was only able to come up with the above code which produces no error and no chart. 我的猜测是我没有将正确的数据结构输入到d3.hierarchy()但是在进行了一些在线挖掘之后,我只能拿出上面的代码,该代码不会产生错误并且没有图表。

Created this fully working example for you, based on your code. 根据您的代码为您创建了这个完全有效的示例。 Hope it helps! 希望能帮助到你! :) :)

const width = window.innerWidth
    const height = window.innerHeight
    const svg = d3.select("svg")

    d3.json('/data.json').then((quotes) => {
        var maxValue = d3.max(d3.entries(quotes.children).map(x => x.value.price))
        var minValue = d3.min(d3.entries(quotes.children).map(x => x.value.price))

        var scale = d3.scaleLinear().domain([minValue, maxValue]).range([5, 20])

        svg
            .attr("width", width)
            .attr("height", height)

        //declare layout
        const bubble = d3.pack()
            .size([width, height])
            .padding(5)
            .radius((d) => {
                return scale(d.data.price)
            })


        const root = d3.hierarchy(quotes)
        bubble(root);

        const node = svg.selectAll('.node')
            .data(root.descendants())
            //.filter(d => !d.children)
            .enter()
            .append('g')
            .attr('class', (d) => {
                if (d.depth === 0) return "node top"
                else return "node"
            })
            // .each(function (d) { d.node = this })
            .style('transform', d => `translate(${d.x}px, ${d.y}px)`)
            .append("circle")
            .attr("r", (d) => d.r)
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM