使用d3.js创建Sankey图表时出现的问题(3)
这与我之前的问题“使用d3.js(1和2)创建Sankey图表的问题”有关 在这里,我试图复制马尔科姆·麦克莱恩(Malcolm Maclean)在其著作《D3技巧与技巧》(D3 Tips and Tricks)中提供的示例,他对“sankey格式的json”的实现是有效的,而我的不是:>) 我的测试数据集(TestDrive_ext'json)是 从Malcolm的示例复制的代码是使用d3.js创建Sankey图表时出现的问题(3),d3.js,sankey-diagram,D3.js,Sankey Diagram,这与我之前的问题“使用d3.js(1和2)创建Sankey图表的问题”有关 在这里,我试图复制马尔科姆·麦克莱恩(Malcolm Maclean)在其著作《D3技巧与技巧》(D3 Tips and Tricks)中提供的示例,他对“sankey格式的json”的实现是有效的,而我的不是:>) 我的测试数据集(TestDrive_ext'json)是 从Malcolm的示例复制的代码是 <!DOCTYPE html> <meta charset="utf-8"> <t
<!DOCTYPE html>
<meta charset="utf-8">
<title>SANKEY Experiment</title>
<style>
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
</style>
<body>
<p id="chart">
<script type="text/javascript" src="d3/d3.v3.js"></script>
<script src="js/sankey.js"></script>
<script>
var units = "Widgets";
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 700 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var formatNumber = d3.format(",.0f"), // zero decimal places
format = function(d) { return formatNumber(d) + " " + units; },
color = d3.scale.category20();
// append the svg canvas to the page
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Set the sankey diagram properties
var sankey = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
var path = sankey.link();
// load the data
d3.json("data/TestDrive_ext.json", function(error, graph) {
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout(32);
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
// add the link titles
link.append("title")
.text(function(d) {
return d.source.name + " → " +
d.target.name + "\n" + format(d.value); });
// add in the nodes
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() {
this.parentNode.appendChild(this); })
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) {
return d.color = color(d.name.replace(/ .*/, "")); })
.style("stroke", function(d) {
return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) {
return d.name + "\n" + format(d.value); });
// add in the title for the nodes
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function(d) { return d.name; })
.filter(function(d) { return d.x < width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// the function for moving the nodes
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + d.x + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
}
});
</script>
</body>
</html>
桑基实验
.node rect{
光标:移动;
填充不透明度:.9;
形状渲染:边缘清晰;
}
.节点文本{
指针事件:无;
文本阴影:0 1px 0#fff;
}
.链接{
填充:无;
行程:#000;
笔画不透明度:.2;
}
.link:悬停{
笔画不透明度:.5;
}
var units=“Widgets”;
var margin={顶部:10,右侧:10,底部:10,左侧:10},
宽度=700-边距。左侧-边距。右侧,
高度=300-margin.top-margin.bottom;
var formatNumber=d3.format(“,.0f”),//小数点后零位
format=函数(d){返回formatNumber(d)+“”+单位;},
颜色=d3.scale.category20();
//将svg画布追加到页面
var svg=d3.选择(“图表”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”);
//设置sankey图表属性
var sankey=d3.sankey()
.诺德维特(15)
.nodePadding(10)
.尺寸([宽度、高度]);
var path=sankey.link();
//加载数据
d3.json(“data/TestDrive_ext.json”),函数(错误,图形){
桑基
.nodes(图.nodes)
.links(graph.links)
.布局(32);
//添加链接
var link=svg.append(“g”).selectAll(“link”)
.数据(图表.链接)
.enter().append(“路径”)
.attr(“类”、“链接”)
.attr(“d”,路径)
.style(“笔划宽度”,函数(d){return Math.max(1,d.dy);})
.sort(函数(a,b){返回b.dy-a.dy;});
//添加链接标题
链接。附加(“标题”)
.文本(功能(d){
返回d.source.name+”→ " +
d、 target.name+“\n”+格式(d.value);});
//添加节点
var node=svg.append(“g”).selectAll(“node”)
.数据(图.节点)
.enter().append(“g”)
.attr(“类”、“节点”)
.attr(“转换”,函数(d){
返回“translate”(“+d.x+”,“+d.y+”);})
.call(d3.behavior.drag())
.origin(函数(d){return d;})
.on(“dragstart”,函数(){
this.parentNode.appendChild(this);})
.在(“拖动”,拖动)上);
//为节点添加矩形
node.append(“rect”)
.attr(“高度”,函数(d){返回d.dy;})
.attr(“宽度”,sankey.nodeWidth())
.style(“填充”,函数(d){
返回d.color=color(d.name.replace(/.*/,“”);})
.style(“笔划”,函数(d){
返回d3.rgb(d.color).dark(2);})
.附加(“标题”)
.text(功能(d){
返回d.name+“\n”+格式(d.value);});
//添加节点的标题
node.append(“文本”)
.attr(“x”,-6)
.attr(“y”,函数(d){返回d.dy/2;})
.attr(“dy”,“.35em”)
.attr(“文本锚定”、“结束”)
.attr(“转换”,null)
.text(函数(d){返回d.name;})
.filter(函数(d){返回d.x
同样,这不会在Javascript控制台中引发任何错误,但该图不会呈现,但正如我前面所说的,Malcolm的会呈现
我不太明白如何解释JSFIDLE,它可以在
再一次,任何有助于我理解我错在哪里的见解都将不胜感激
非常感谢
朱利安
<!DOCTYPE html>
<meta charset="utf-8">
<title>SANKEY Experiment</title>
<style>
.node rect {
cursor: move;
fill-opacity: .9;
shape-rendering: crispEdges;
}
.node text {
pointer-events: none;
text-shadow: 0 1px 0 #fff;
}
.link {
fill: none;
stroke: #000;
stroke-opacity: .2;
}
.link:hover {
stroke-opacity: .5;
}
</style>
<body>
<p id="chart">
<script type="text/javascript" src="d3/d3.v3.js"></script>
<script src="js/sankey.js"></script>
<script>
var units = "Widgets";
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width = 700 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var formatNumber = d3.format(",.0f"), // zero decimal places
format = function(d) { return formatNumber(d) + " " + units; },
color = d3.scale.category20();
// append the svg canvas to the page
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Set the sankey diagram properties
var sankey = d3.sankey()
.nodeWidth(15)
.nodePadding(10)
.size([width, height]);
var path = sankey.link();
// load the data
d3.json("data/TestDrive_ext.json", function(error, graph) {
sankey
.nodes(graph.nodes)
.links(graph.links)
.layout(32);
// add in the links
var link = svg.append("g").selectAll(".link")
.data(graph.links)
.enter().append("path")
.attr("class", "link")
.attr("d", path)
.style("stroke-width", function(d) { return Math.max(1, d.dy); })
.sort(function(a, b) { return b.dy - a.dy; });
// add the link titles
link.append("title")
.text(function(d) {
return d.source.name + " → " +
d.target.name + "\n" + format(d.value); });
// add in the nodes
var node = svg.append("g").selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; })
.call(d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", function() {
this.parentNode.appendChild(this); })
.on("drag", dragmove));
// add the rectangles for the nodes
node.append("rect")
.attr("height", function(d) { return d.dy; })
.attr("width", sankey.nodeWidth())
.style("fill", function(d) {
return d.color = color(d.name.replace(/ .*/, "")); })
.style("stroke", function(d) {
return d3.rgb(d.color).darker(2); })
.append("title")
.text(function(d) {
return d.name + "\n" + format(d.value); });
// add in the title for the nodes
node.append("text")
.attr("x", -6)
.attr("y", function(d) { return d.dy / 2; })
.attr("dy", ".35em")
.attr("text-anchor", "end")
.attr("transform", null)
.text(function(d) { return d.name; })
.filter(function(d) { return d.x < width / 2; })
.attr("x", 6 + sankey.nodeWidth())
.attr("text-anchor", "start");
// the function for moving the nodes
function dragmove(d) {
d3.select(this).attr("transform",
"translate(" + d.x + "," + (
d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
) + ")");
sankey.relayout();
link.attr("d", path);
}
});
</script>
</body>
</html>