如何使用JSON数据制作给定“x”和“y”值的圆圈并使用网站名称标记圆圈?此外,圆的半径应等于给定的量。 JSON文件看起来像这样
{
“节点”:[
{ “id”:“site01”, “x”:317.5, “y”:282.5 },
{ “id”:“site02”, “x”:112, “y”:47 },
{ “id”:“site03”, “x”:69.5, “y”:287 },
{ “id”:“site04”, “x”:424.5, “y”:99.5 },
{ “id”:“site05”, “x”:432, “y”:467 },
{ “id”:“site06”, “x”:567, “y”:482 },
{ “id”:“site07”, “x”:592, “y”:164.5 },
{ “id”:“site08”, “x”:782, “y”:397 },
{ “id”:“site09”, “x”:829.5, “y”:262 },
{“id”:“site10”, “x”:902, “y”:169.5}
],
“链接”:[
{"node01": "site01", "node02": "site08", "amount": 170},
{"node01": "site01", "node02": "site02", "amount": 100},
{"node01": "site01", "node02": "site03", "amount": 70},
{"node01": "site01", "node02": "site07", "amount": 50},
{"node01": "site01", "node02": "site09", "amount": 220},
{"node01": "site01", "node02": "site10", "amount": 350},
{"node01": "site02", "node02": "site03", "amount": 1000},
{"node01": "site02", "node02": "site04", "amount": 50},
{"node01": "site02", "node02": "site05", "amount": 60},
{"node01": "site02", "node02": "site08", "amount": 70},
{"node01": "site02", "node02": "site09", "amount": 80},
{"node01": "site03", "node02": "site06", "amount": 120},
{"node01": "site03", "node02": "site07", "amount": 130},
{"node01": "site03", "node02": "site09", "amount": 110},
{"node01": "site03", "node02": "site10", "amount": 120},
{"node01": "site03", "node02": "site04", "amount": 140},
{"node01": "site04", "node02": "site01", "amount": 50},
{"node01": "site04", "node02": "site05", "amount": 200},
{"node01": "site04", "node02": "site07", "amount": 210},
{"node01": "site04", "node02": "site09", "amount": 220},
{"node01": "site04", "node02": "site10", "amount": 190},
{"node01": "site05", "node02": "site06", "amount": 170},
{"node01": "site05", "node02": "site07", "amount": 150},
{"node01": "site05", "node02": "site08", "amount": 160},
{"node01": "site05", "node02": "site09", "amount": 130},
{"node01": "site05", "node02": "site10", "amount": 140},
{"node01": "site06", "node02": "site02", "amount": 800},
{"node01": "site06", "node02": "site07", "amount": 760},
{"node01": "site06", "node02": "site04", "amount": 780},
{"node01": "site06", "node02": "site09", "amount": 50},
{"node01": "site06", "node02": "site10", "amount": 80}
] }
答案 0 :(得分:0)
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
.bar{
fill: steelblue;
}
.bar:hover{
fill: brown;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
// set the dimensions of the canvas
var margin = {top: 20, right: 20, bottom: 70, left: 40},
width = 600 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
// set the ranges
var x = d3.scale.ordinal().rangeRoundBands([0, width], .05);
var y = d3.scale.linear().range([height, 0]);
// define the axis
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10);
// add the SVG element
var svg = d3.select("body").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 + ")");
// load the data
d3.json("/Volumes/Macintosh%20HD/Users/Lakshya/Downloads/data.json", function(error, data) {
data.forEach(function(d) {
d.id = d.id;
d.x = +d.x;
});
// scale the range of the data
x.domain(data.map(function(d) { return d.id; }));
y.domain([0, d3.max(data, function(d) { return d.x; })]);
// add axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-90)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 5)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("x");
// Add bar chart
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.id); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.x); })
.attr("height", function(d) { return height - y(d.x); });
});
</script>
</body>