我试着从这一页重现简单的日出:

https://observablehq.com/@unipept/sunburst-example

我创建了下面的html,但由于某种原因,当我在Visual Studio中运行它时,我得到:

未捕获引用Error ReferenceError:未定义SunBurst

...这一点我不太理解,因为SunBurst函数是在unipept-visalizations.js文件中定义的.这里可能出了什么问题?

<!DOCTYPE html>
<html>
<head>
  <title>Sunburst Visualization</title>
</head>
<body>
  <div id="simpleSunburst"></div> 
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/unipept-visualizations@latest/dist/unipept-visualizations.js"></script>
  <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
      const family = {
        "id": 1,
        "name": "John",
        "children": [
        {
        "id": 2,
        "name": "Sarah",
        "children": [
            {
            "id": 11,
            "name": "Lary",
            "children": [
            {
            "id": 15,
            "name": "George",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
            ],
            "count": 2,
            "selfCount": 1
            },
            {
            "id": 12,
            "name": "Susan",
            "children": [
            {
            "id": 13,
            "name": "Martha",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 14,
            "name": "Zack",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
            ],
            "count": 3,
            "selfCount": 1
            }
        ],
        "count": 6,
        "selfCount": 1
        },
        {
        "id": 3,
        "name": "Charles",
        "children": [
            {
            "id": 5,
            "name": "Rick",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
        ],
        "count": 2,
        "selfCount": 1
        },
        {
        "id": 4,
        "name": "Janet",
        "children": [
            {
            "id": 6,
            "name": "Aaron",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 7,
            "name": "Kim",
            "children": [
            {
            "id": 8,
            "name": "Patricia",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 9,
            "name": "Jack",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 10,
            "name": "Thomas",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
            ],
            "count": 4,
            "selfCount": 1
            }
        ],
        "count": 6,
        "selfCount": 1
        }
        ],
        "count": 15,
        "selfCount": 1
        };
      const simpleSunburst = document.getElementById("simpleSunburst");
      new Sunburst(simpleSunburst, family);
    });
  </script>
</body>
</html>

推荐答案

使用new UnipeptVisualizations.Sunburst(simpleSunburst, family);

<!DOCTYPE html>
<html>
<head>
  <title>Sunburst Visualization</title>
</head>
<body>
  <div id="simpleSunburst"></div> 
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/unipept-visualizations@latest/dist/unipept-visualizations.js"></script>
  <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {
      const family = {
        "id": 1,
        "name": "John",
        "children": [
        {
        "id": 2,
        "name": "Sarah",
        "children": [
            {
            "id": 11,
            "name": "Lary",
            "children": [
            {
            "id": 15,
            "name": "George",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
            ],
            "count": 2,
            "selfCount": 1
            },
            {
            "id": 12,
            "name": "Susan",
            "children": [
            {
            "id": 13,
            "name": "Martha",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 14,
            "name": "Zack",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
            ],
            "count": 3,
            "selfCount": 1
            }
        ],
        "count": 6,
        "selfCount": 1
        },
        {
        "id": 3,
        "name": "Charles",
        "children": [
            {
            "id": 5,
            "name": "Rick",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
        ],
        "count": 2,
        "selfCount": 1
        },
        {
        "id": 4,
        "name": "Janet",
        "children": [
            {
            "id": 6,
            "name": "Aaron",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 7,
            "name": "Kim",
            "children": [
            {
            "id": 8,
            "name": "Patricia",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 9,
            "name": "Jack",
            "children": [],
            "count": 1,
            "selfCount": 1
            },
            {
            "id": 10,
            "name": "Thomas",
            "children": [],
            "count": 1,
            "selfCount": 1
            }
            ],
            "count": 4,
            "selfCount": 1
            }
        ],
        "count": 6,
        "selfCount": 1
        }
        ],
        "count": 15,
        "selfCount": 1
        };
      const simpleSunburst = document.getElementById("simpleSunburst");
      new  UnipeptVisualizations.Sunburst(simpleSunburst, family);
    });
  </script>
</body>
</html>

Javascript相关问答推荐

在JavaScript中对大型级联数组进行切片的最有效方法?

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

是什么原因导致此Angular 16应用程序中类型错误时属性结果不存在?

如何在RTK上设置轮询,每24小时

django无法解析余数:[0] from carray[0]'

yarn安装一个本地npm包,以便本地包使用main项目的node_modules(ckeditor-duplicated-modules错误)

Exceljs:我们在file.xlsx(...)&#中发现了一个问题'"" 39人;

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

我可以使用空手道用户界面来获取网页的当前滚动位置吗?

更改预请求脚本中重用的JSON主体变量- Postman

当我点击一个按钮后按回车键时,如何阻止它再次被点击

如何修复使用axios运行TSC index.ts时出现的错误?

Node.js错误: node 不可单击或不是元素

JavaScript将字符串数字转换为整数

本地损坏的Java脚本

通过解构/功能组件接收props-prop验证中缺少错误"

对不同目录中的Angular material 表列进行排序

JSON Web令牌(JWT)错误:RSA密钥对的签名无效

不允许在对象文本中注释掉的属性

检测带有委托的元素内部的点击,以及元素何时按其类名被选中