[英]Displaying circles using d3 from data
在這個小提琴中,我試圖繪制從文件讀取的圓圈:
http://jsfiddle.net/zzz8svuq/17/
但是沒有圓圈出現。
在這里,我將數據綁定到svg:
svg.data(data)
.enter().append("circle")
.attr("stroke", "red")
.attr("stroke-width" , "2px")
.attr("cx", xMap)
這是不正確的?
小提琴代碼:
var data = d3.csv.parse( d3.select("pre#data").text() );
var margin = {top: 20, right: 20, bottom: 130, left: 140},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height",500)
.append("g");
var xValue = function (data) {
return data.x;
}
xScale = d3.scale.linear().range([0, width - margin.left - margin.right]) // value -> display
xMap = function (d) {
return xScale(xValue(d));
}
svg.data(data)
.enter().append("circle")
.attr("stroke", "red")
.attr("stroke-width" , "2px")
.attr("cx", xMap)
<pre style="display:none" id="data">
label,x,y,r
l1,100,30,50
l2,5,5,100
l3,50,50,20
</pre>
body {
font: 11px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
更新:
添加
.attr("cy", 5)
.attr("r", 5)
但結果相同
圓需要3個屬性cx,cy(位置)和r(半徑)。 您未提供cy或r,並且r的默認值為0,這表示您的圓未渲染。
如果要從json數據中將圓附加到容器,則需要將數據綁定到容器中的選定內容,而不是將數據綁定到容器。 嘗試這種方式。
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("stroke", "red")
.attr("stroke-width" , "2px")
.attr("cx", xMap) //Center point X
.attr("cy", yMap) // Center point Y
.attr("r", 5); //Radius
確認cx,cy和r屬性有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.