- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用从 requirejs 加载的 d3 库创建一个基本的 sankey 图。在过去,我使用了核心 d3 库并使一切都很好,但是在这种情况下尝试加载 d3-sankey 时它似乎没有注册。我也对垫片进行了修补,但我似乎无法弄清楚问题具体出在哪里。每次我尝试加载附加代码时,我都会收到错误消息“Uncaught TypeError: d3.sankey is not a function”。
我假设问题是正在加载的库的顺序,但我还是不确定如何解决这个问题。谁能帮忙?
Codepen - https://codepen.io/quirkules/pen/wYoXxQ
// define URLs
require.config({
paths: {
cloudflare: 'https://cdnjs.cloudflare.com/ajax/libs'
},
map: {
'*': {
'd3': 'cloudflare/d3/4.8.0/d3',
'd3-timer': 'cloudflare/d3-timer/1.0.5/d3-timer',
'd3-array': 'cloudflare/d3-array/1.2.2/d3-array',
'd3-collection': 'cloudflare/d3-collection/1.0.5/d3-collection',
'd3-interpolate': 'cloudflare/d3-interpolate/1.3.0/d3-interpolate',
'd3-color': 'cloudflare/d3-color/1.2.1/d3-color',
'd3-shape': 'cloudflare/d3-shape/1.2.0/d3-shape',
'd3-path': 'cloudflare/d3-path/1.0.5/d3-path',
'd3-sankey': 'cloudflare/d3-sankey/0.7.1/d3-sankey'
}
},
shim : {
d3 : {
exports : 'd3'
},
'd3-sankey' : {
deps: ['d3']
}
}
});
// load d3
require(['d3', 'd3-timer', 'd3-array', 'd3-collection', 'd3-interpolate', 'd3-color', 'd3-shape', 'd3-sankey'], function(d3) {
//****** CREATE DIVS ******//
// Add the divs to the DOM
$(document.body)
.append('<div id="header"></div>')
.append('<div id="chart"></div>')
.append('<div id="footer"></div>');
//add text to the divs
document.getElementById("header").innerHTML = '<b>Title</b>';
// set the dimensions and margins of the graph
var margin = { top: 20, right: 20, bottom: 40, left: 50 },
width = d3.select('#chart').node().getBoundingClientRect().width - margin.left - margin.right,
height = d3.select('#chart').node().getBoundingClientRect().height - margin.top - margin.bottom;
//
var svg = d3.select("#chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var graph = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
var path = graph.link();
var freqCounter = 1;
//****** END CREATE CHART ******//
});
最佳答案
为什么要使用 require?
如果我加载以下 2 个脚本,它会在 var path = graph.link();
行失败,因此 d3.sankey
是已知的函数。
撤消对require
的所有使用并添加
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.8.0/d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-sankey/0.7.1/d3-sankey.min.js"></script>
d3.js
已经包含大部分其他模块,它不包含 d3-sankey
。出于某种原因,d3.js
没有使用 require()
注册它包含的各个模块,这就是为什么 require 会尝试加载模块 d3-array
、d3-collection
、d3-shape
、d3-path
可能是版本问题,怎么知道各个模块属于哪个版本的d3.v4.8.0。
编辑
如果我在其他模块之后加载 d3-sankey
,错误就会消失,但我不知道它是否会在以后工作。 (没有链接,因为没有经过 sankey 处理的数据。
require(['d3-selection', 'd3-timer', 'd3-array', 'd3-collection', 'd3-interpolate', 'd3-color', 'd3-shape'], function(d3) {
require(['d3-sankey'], function(dsankey) {
//****** CREATE DIVS ******//
// .....
var graph = dsankey.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
//****** END CREATE CHART ******//
});
});
关于javascript - requirejs - d3.sankey 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138023/
plotly 库有一些漂亮的 sankey 图 https://plotly.com/python/sankey-diagram/ 但数据要求您传递源/目标对的索引。 link = dict(
我试图在 Sankey diagram 中绘制 3 个集群之间的患者流量.我有一个 pd.DataFrame counts使用 from-to 值,见下文。要重现此 DF,here是 counts d
下面sankey图中字体的白色阴影是否可以去除? import plotly.graph_objects as go fig = go.Figure(go.Sankey(
我想使用 highcharter 包在 R 中绘制桑基图。我在格式化时遇到问题。这是示例。 # devtools::install_github("jbkunst/highcharter") libr
想知道是否有人可以帮忙找出为什么这个桑基图不起作用。我很确定我遵循了正确的语法和约定来使用该模块。因为这个我一直用头撞 table 。 import plotly.offline data_trace
我用 R 中的 rChars 包做了一个桑基图。 但是我想添加一个功能,当我们移动到链接或目标节点时,它会在工具提示中显示源节点的所有名称(或在图形右上角的新框)。例如,当我们移动到节点“target
我创建了一个 d3 Sankey 图。我面临的问题是我无法拖动节点。这是节点的创建方式: var node = svg.append("g").selectAll(".node") .d
Here是 Sankey 图的jsfiddle: 我正在尝试修改链接的颜色,以便每个链接的颜色实际上是从其源节点颜色到其目标节点颜色的渐变。 (假设不透明度将保持 0.2 或 0.5,具体取决于鼠标是
我正在使用 d3.js 的 Sankey 插件。有没有办法影响节点的自动放置? 如果你比较下面两张图片。 自动生成桑基图:https://files.secureserver.net/0s3DEU5s
我正在寻求为时间 1 的 5 个节点和时间 2 的 5 个节点之间的流制作大量桑基图。我希望每次都以相同的顺序绘制节点,无论节点或流的大小如何。然而,其中一些图表是用乱序的节点绘制的。我试图动态计算预
我希望在 R Markdown 中使用 HTML 小部件/Javascript 创建一个交互式 Sankey 图,所以我正在考虑使用 networkD3 package .我希望利用的主要交互性是单击
是否可以使用 sankey 按名称排序?例如: 示例: https://jsfiddle.net/Khrys/1s3shf2m/ google.setOnLoadCallback(drawChar
问题 1:我无法在拖动事件上更新 d3-sankey 链接。我正在尝试复制与此类似的 d3 垂直和水平拖动事件:https://bl.ocks.org/d3noob/5028304 当使用拖动事件时,
我想向 Plotly Sankey 图 ( https://plot.ly/python/sankey-diagram/ ) 中的每个节点添加节点计数,使其看起来像红色箭头引用的计数。 这可能吗?我无
我在 R 中创建了一个 Sankey 图,使用 networkD3包,我想保存为静态图像,使用代码而不是单击“导出”-->“另存为图像...”。 我尝试过的当前代码(以 this Sankey 图为例
我想知道如何在 plotly 中将包名称添加到 Sankey 图中。通过捆绑,我理解一列中的节点。从 plotly 教程: import plotly.graph_objects as go fig
我正在通过 plotly 绘制桑基图来比较不同的观察分类。但是,我对两个以上的分类有一些问题,其中每个分类中的观察顺序在每个节点的输入和输出之间发生变化。 我使用的代码如下: def pl_sanke
在他的d3 Sankey Diagram page上,Mike Bostock 说“该算法将来可以改进,比如尽量减少链接交叉”。 我想投入一些时间并做到这一点,但我不确定是否要开始。有人对如何实现这一
我正在按照本教程制作桑基图 https://bost.ocks.org/mike/sankey/ 当节点和链接太多时,它不会绘制任何内容,这不是圆的问题。 它会说 我有 349 个节点和 1776 个
我正在尝试使用 d3 sankey plugin 创建桑基图具有动态运输数据。它在大多数情况下都工作得很好,除非我得到这样的数据集: [{"DeparturePort":"CHARLESTON","A
我是一名优秀的程序员,十分优秀!