D3仅呈现到一定深度

发布于 2025-02-04 03:49:25 字数 2866 浏览 1 评论 0 原文

对于D3,给定一系列嵌套对象。 是否可以仅渲染一定数量的深度?

我是在网上的一些阳光示例中取决于:

使用D3中的.selectall方法,我只能限制阳光固定来渲染'x'深度,而不是呈现整个嵌套的阵列对象?

我正在尝试渲染一个非常大的阵列,这会导致非常卑鄙的体验。

这是我从GitHub示例中使用的选择。

svg.selectAll('path')
        .data(partition(root)
        .descendants())
        .enter()
        .append('path')
        .style('fill', (d) => {
                let hue;
                const current = d;
                if (current.depth === 0) {
                    return '#33cccc';
                }
                if (current.depth <= 1) {
                    hue = hueDXScale(d.x0);
                    current.fill = d3.hsl(hue, 0.5, 0.6);
                    return current.fill;
                }
                if(current.depth <= 2){
                    console.log("depth > 2: ", d)
                    current.fill = current.parent.fill.brighter(0.5);
                    const hsl = d3.hsl(current.fill);
                    hue = hueDXScale(current.x0);
                    const colorshift = hsl.h + (hue / 4);
                    return d3.hsl(colorshift, hsl.s, hsl.l);
                }
            })
            .attr('stroke', '#fff')
            .attr('stroke-width', '1')
            .on('click', d => click(d, node, svg, x, y, radius, arc))
            .on('mouseover', function (d) {
                if (props.tooltip) {
                    d3.select(this).style('cursor', 'pointer');
                    tooltip.html(() => {
                        const name = utils.formatNameTooltip(d);
                        return name;
                    });
                    return tooltip.transition().duration(50).style('opacity', 1);
                }
                return null;
            })
            .on('mousemove', () => {
                if (props.tooltip) {
                    tooltip
                        .style('top', `${d3.event.pageY - 50}px`)
                        .style('left', `${props.tooltipPosition === 'right' ? d3.event.pageX - 100 : d3.event.pageX - 50}px`);
                }
                return null;
            })
            .on('mouseout', function () {
                if (props.tooltip) {
                    d3.select(this).style('cursor', 'default');
                    tooltip.transition().duration(50).style('opacity', 0);
                }
                return null;
            });

如您所见,从Current.depth中,我能够过滤出2个以上的深度。 是否可以添加显示:无需以2的深度到2,还是有更好的方法是不呈现到当前的2的深度。

For d3, given an array of nested objects.
Is it possible to only render a certain amount of depth?

I am basing off of some sunburst examples online like :
https://github.com/Nikhilkoneru/react-d3-zoomable-sunburst/blob/master/src/index.js

Using the .selectAll method from d3, can I only limit the sunburst to render 'X' depths, instead of rendering the entire array of nested objects?

I'm trying to render a really large array, and it causes a really laggy experience.

This is the selectAll that I'm using from the github example.

svg.selectAll('path')
        .data(partition(root)
        .descendants())
        .enter()
        .append('path')
        .style('fill', (d) => {
                let hue;
                const current = d;
                if (current.depth === 0) {
                    return '#33cccc';
                }
                if (current.depth <= 1) {
                    hue = hueDXScale(d.x0);
                    current.fill = d3.hsl(hue, 0.5, 0.6);
                    return current.fill;
                }
                if(current.depth <= 2){
                    console.log("depth > 2: ", d)
                    current.fill = current.parent.fill.brighter(0.5);
                    const hsl = d3.hsl(current.fill);
                    hue = hueDXScale(current.x0);
                    const colorshift = hsl.h + (hue / 4);
                    return d3.hsl(colorshift, hsl.s, hsl.l);
                }
            })
            .attr('stroke', '#fff')
            .attr('stroke-width', '1')
            .on('click', d => click(d, node, svg, x, y, radius, arc))
            .on('mouseover', function (d) {
                if (props.tooltip) {
                    d3.select(this).style('cursor', 'pointer');
                    tooltip.html(() => {
                        const name = utils.formatNameTooltip(d);
                        return name;
                    });
                    return tooltip.transition().duration(50).style('opacity', 1);
                }
                return null;
            })
            .on('mousemove', () => {
                if (props.tooltip) {
                    tooltip
                        .style('top', `${d3.event.pageY - 50}px`)
                        .style('left', `${props.tooltipPosition === 'right' ? d3.event.pageX - 100 : d3.event.pageX - 50}px`);
                }
                return null;
            })
            .on('mouseout', function () {
                if (props.tooltip) {
                    d3.select(this).style('cursor', 'default');
                    tooltip.transition().duration(50).style('opacity', 0);
                }
                return null;
            });

Result

As you can see, from the current.depth, i'm able to filter out what depth is more than 2.
Is it possible to add display: none to anything more than depth of 2, or is there a better way to not render anything more than depth of 2 from current

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文