d3 可点击森伯斯特不适用于 json 数据

如何解决d3 可点击森伯斯特不适用于 json 数据

我正在使用 d3 的 v5 来创建可缩放的旭日图。 我正在使用 d3.json 来获取甚至在控制台中打印但在浏览器上什么也没有显示的数据。我在 http-server 上运行它,控制台中没有记录任何错误,但浏览器上也没有显示任何内容。如果我使用 observablehq 但不适用于 d3.json,它可以正常工作。

以下是我的代码供参考,感谢您的帮助。

(function () {
    'use strict';

    const format = d3.format(",d");
    const width = 932;
    const radius = width / 6;

    const arc = d3.arc()
            .startAngle(d => d.x0)
            .endAngle(d => d.x1)
            .padAngle(d => Math.min((d.x1 - d.x0) / 2,0.005))
            .padradius(radius * 1.5)
            .innerRadius(d => d.y0 * radius)
            .outerRadius(d => Math.max(d.y0 * radius,d.y1 * radius - 1));

    const partition = data => {
        const root = d3.hierarchy(data)
                .sum(d => d.size)
                .sort((a,b) => b.value - a.value);
        return d3.partition()
                .size([2 * Math.PI,root.height + 1])
                (root);
    };

    const {require} = new observablehq.Library;
    
    d3.json("./flare.json")
    //.then(function(data){
    //require()('@observablehq/flare')
    //fetch("./flare.json")
    //.then((data1) => {
        //return data.json();
    //})
    .then(data => {
        console.log(data);
        const root = partition(data);
        const color = d3.scaleOrdinal().range(d3.quantize(d3.interpolaterainbow,data.children.length + 1));

        root.each(d => d.current = d);

        const svg = d3.select('#partitionSVG')
                .style("width","100%")
                .style("height","auto")
                .style("font","10px sans-serif");

        const g = svg.append("g")
                .attr("transform",`translate(${width / 2},${width / 2})`);

        const path = g.append("g")
                .selectAll("path")
                .data(root.descendants().slice(1))
                .join("path")
                .attr("fill",d => {
                    while (d.depth > 1)
                        { d = d.parent; }
                    return color(d.data.name);
                })
                .attr("fill-opacity",d => arcVisible(d.current) ? (d.children ? 0.6 : 0.4) : 0)
                .attr("d",d => arc(d.current));

        path.filter(d => d.children)
                .style("cursor","pointer")
                .on("click",clicked);

        path.append("title")
                .text(d => `${d.ancestors().map(d => d.data.name).reverse().join("/")}\n${format(d.value)}`);

        const label = g.append("g")
                .attr("pointer-events","none")
                .attr("text-anchor","middle")
                .style("user-select","none")
                .selectAll("text")
                .data(root.descendants().slice(1))
                .join("text")
                .attr("dy","0.35em")
                .attr("fill-opacity",d => +labelVisible(d.current))
                .attr("transform",d => labelTransform(d.current))
                .text(d => d.data.name);

        const parent = g.append("circle")
                .datum(root)
                .attr("r",radius)
                .attr("fill","none")
                .attr("pointer-events","all")
                .on("click",clicked);

        function clicked(p) {
            parent.datum(p.parent || root);

            root.each(d => d.target = {
                    x0: Math.max(0,Math.min(1,(d.x0 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,x1: Math.max(0,(d.x1 - p.x0) / (p.x1 - p.x0))) * 2 * Math.PI,y0: Math.max(0,d.y0 - p.depth),y1: Math.max(0,d.y1 - p.depth)
                });

            const t = g.transition().duration(750);

            // Transition the data on all arcs,even the ones that aren’t visible,// so that if this transition is interrupted,entering arcs will start
            // the next transition from the desired position.
            path.transition(t)
                    .tween("data",d => {
                        const i = d3.interpolate(d.current,d.target);
                        return t => d.current = i(t);
                    })
                    .filter(function (d) {
                        return +this.getAttribute("fill-opacity") || arcVisible(d.target);
                    })
                    .attr("fill-opacity",d => arcVisible(d.target) ? (d.children ? 0.6 : 0.4) : 0)
                    .attrTween("d",d => () => arc(d.current));

            label.filter(function (d) {
                return +this.getAttribute("fill-opacity") || labelVisible(d.target);
            }).transition(t)
                    .attr("fill-opacity",d => +labelVisible(d.target))
                    .attrTween("transform",d => () => labelTransform(d.current));
        }

        function arcVisible(d) {
            return d.y1 <= 3 && d.y0 >= 1 && d.x1 > d.x0;
        }

        function labelVisible(d) {
            return d.y1 <= 3 && d.y0 >= 1 && (d.y1 - d.y0) * (d.x1 - d.x0) > 0.03;
        }

        function labelTransform(d) {
            const x = (d.x0 + d.x1) / 2 * 180 / Math.PI;
            const y = (d.y0 + d.y1) / 2 * radius;
            return `rotate(${x - 90}) translate(${y},0) rotate(${x < 180 ? 0 : 180})`;
        }
    });

}());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?