D3js 解决方案的主要来源是observableHq.com
,但似乎不可能(?)通过复制/粘贴重用算法......是吗?即使检查这样的教程,也没有简单的方法(使用更少的插件或程序员的时间消耗!)来检查和重用.
示例:我需要一个新的 2020 D3js v5算法用于缩进树可视化,并且有一个很好的解决方案:observableHq.com/@d3/indented-tree。
下载没有用,因为是基于复杂的 运行时类...
但是,似乎是一个简单的图表生成算法,
chart = { // the indented-tree algorithm
const nodes = root.descendants();
const svg = d3.create("svg")// ...
// ...
return svg.node();
}
我是否可以通过简单的人工逐步将其转换为简单的 HTML,无需复杂的改编,以不使用Runtime 类<script src="https://d3js.org/d3.v5.min.js"></script>
开始和结束?
更多细节作为例子
想象一下我对引用的 indented-tree algorithm的逐步说明,我无法精通并需要您的帮助:
假设从一个干净的 HTML5 模板开始。例如:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>Indented Tree</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
function onLOAD() {
console.log("Hello onLoad!")
// all copy-paste and adaptations HERE.
console.log("!Bye!")
} // \onLOAD
</script>
</head>
<body onload="onLOAD()">
<script>
console.log("Hello!")
// global INITIALIZATIONS HERE.
</script>
</body>
</html>
准备全局变量,似乎
root
,,nodeSize=17
和width
准备数据... JSON数据很丑
./files/e6537420...
,我用它的真实名称移动到项目的根目录,flare-2.json
.读取 JSON 数据的简单而经典的 D3js 方法:
d3.json("./flare-2.json").then( data=> console.log(data) );
必须测试并检查无 CORS 错误等。准备数据作为
root
变量。全部进入data => {}
块以避免同步问题......
似乎root
基于function(d3,data) { let i = 0; return d3.hierarchy(data).eachBefore(d => d.index = i++); }
.chart =
上面引用的复制粘贴,在root
用数据初始化之后。...
常问问题
评论问题和答案:
@Mehdi - 你能解释一下在代码中包含 D3 脚本标签和使用运行时库的问题吗?
当原始的 ObservableHq 算法很简单时,我需要另一种方法,一种简单的方法来重用它,通过复制/粘贴和最小的调整。
@Mehdi - 你读过下载和嵌入笔记本教程吗?
是的,那里没有消息:没有关于如何重用代码的“人工指令”......只有“安装它”和“安装那个”。没有关于我上面解释过的“复制/粘贴和最小调整”的说明。
(@nobody) - 你需要什么作为答案?
正如我在上面展示的,一个简单的人类可读的逐步转换过程......理想情况下,最终结果可以通过测试,证明它可以在例如 JSFiddle 上工作,并带有复制/粘贴代码等等适应线来表明你的观点。