没什么卵用的ajax+D3.js
如题 用来画图谱
<script type
="text/javascript">
$(document
).ready(function(){
$('button').click(function DoAjax(){
var temp
=$('#search-recipeKG-text').val()
$
.ajax({
url
:'',
type
:'GET',
data
:{'search-recipeKG-text':temp
},
success
:function(arg
){
var obj
=jQuery
.parseJSON(arg
)
console
.log(obj
)
console
.log(arg
)
console
.log('request.POST 提交成功')
var str
='';
str
+='<div class="col-lg-8">';
str
+='<div class="card">';
str
+='<div class="card-body" >';
str
+='<div id="id1">';
var height
=600;
var width
=650;
var colors
= ['#6ca46c', '#4e88af', '#ca635f', '#d2907c', '#d6744d', '#ded295'];
var svg
=d3
.select("#id1")
.append("svg")
.attr("width",width
)
.attr("height",height
);
{# 利用d3
.forceSimulation()定义关系图 包括设置边link、排斥电荷charge、关系图中心点#
}
var simulation
= d3
.forceSimulation()
.force("link", d3
.forceLink().id(function(d
) {
return d
.id
;
}))
.force("charge", d3
.forceManyBody())
.force("center", d3
.forceCenter(width
/ 2, height
/ 2));
{# 存储关系图的数据#
}
var graph
=obj
;
var link
= svg
.append("g").attr("class","links").selectAll("line").data(graph
.links
).enter()
.append("line").attr("stroke-width", function(d
) {
return 1;
});
var node
= svg
.append("g").attr("class", "nodes").selectAll("circle").data(graph
.nodes
).enter()
.append("circle").attr("r", function(d
) {
return d
.size
;
}).attr("fill", function(d
) {
return colors
[d
.group
];
}).attr("stroke", "none").attr("name", function(d
) {
return d
.id
;
}).call(d3
.drag()
.on("start", dragstarted
)
.on("drag", dragged
)
.on("end", dragended
)
);
var text
= svg
.append("g").attr("class", "texts").selectAll("text").data(graph
.nodes
).enter()
.append("text").attr("font-size", function(d
) {
return d
.size
;
}).attr("fill", function(d
) {
return colors
[d
.group
];
}).attr('name', function(d
) {
return d
.id
;
}).text(function(d
) {
return d
.id
;
}).attr('text-anchor', 'middle').call(d3
.drag()
.on("start", dragstarted
)
.on("drag", dragged
)
.on("end", dragended
)
);
node
.append("title").text(function(d
) {
return d
.id
;
})
simulation
.nodes(graph
.nodes
)
.on("tick", ticked
);
simulation
.force("link")
.links(graph
.links
);
function dragstarted(d
) {
if (!d3
.event
.active
) simulation
.alphaTarget(0.3).restart();
d
.fx
= d
.x
;
d
.fy
= d
.y
;
}
function dragged(d
) {
d
.fx
= d3
.event
.x
;
d
.fy
= d3
.event
.y
;
}
function dragended(d
) {
if (!d3
.event
.active
) simulation
.alphaTarget(0);
d
.fx
= null;
d
.fy
= null;
}
function ticked() {
link
.attr("x1", function(d
) {
return d
.source
.x
;
})
.attr("y1", function(d
) {
return d
.source
.y
;
})
.attr("x2", function(d
) {
return d
.target
.x
;
})
.attr("y2", function(d
) {
return d
.target
.y
;
});
node
.attr("cx", function(d
) {
return d
.x
;
})
.attr("cy", function(d
) {
return d
.y
;
});
text
.
attr('transform', function(d
) {
return 'translate(' + d
.x
+ ',' + (d
.y
+ d
.size
/ 2) + ')';
});
}
str
+='</div>';
str
+='</div>';
str
+='</div>';
str
+='</div>';
console
.log(str
)
$("#show").html(str
);
},
error
:function(){
console
.log('失败')
}
});
});
});
</script
>
转载请注明原文地址:https://ipadbbs.8miu.com/read-31102.html