我正在研究雷达图。我已经附加了一些我试图在十字路口定位的圆圈。但是,圆圈的属性似乎有问题cx and cy
,我无法弄清楚。
对此的任何帮助将不胜感激。
var dataset = [{
"day": 1,
"sales": 40
}, {
"day": 2,
"sales": 85
}, {
"day": 3,
"sales": 70
}, {
"day": 4,
"sales": 30
}, {
"day": 5,
"sales": 85
}, {
"day": 6,
"sales": 60
}, {
"day": 7,
"sales": 85
}, {
"day": 8,
"sales": 35
}, {
"day": 9,
"sales": 70
}, {
"day": 10,
"sales": 15
}];
var w = 460,
h = 460;
var margin = {
top: 20, bottom: 10, left: 10, right: 10
};
var width = w - margin.left - margin.right;
var height = h - margin.top - margin.bottom;
var circleConstraint = d3.min([height, width]);
var radius = d3.scale.linear()
.domain([0, 100])
.range([0, (circleConstraint / 2)]);
var centerXPos = width / 2 + margin.left;
var centerYPos = height / 2 + margin.top;
var svg = d3.select("body").append("svg")
.attr("width", w)
.attr("height", h)
.append("g")
.attr("transform", "translate(" + centerXPos + ", " + centerYPos + ")");
//Adds circular gridLines and labels
var gridLines = [0, 20, 40, 60, 80, 100];
var circleAxes = svg.selectAll(".circularGrid")
.data(gridLines)
.enter().append("g")
.attr("class", "circularGrid");
circleAxes.append("circle")
.attr("r", function(d) {
return radius(d);
})
.style("stroke", "#CCC")
.style("fill", "none");
circleAxes.append("text")
.attr("text-anchor", "middle")
.attr("dy", function(d) {
return radius(-d - 1);
})
.text(String)
.style("font-size", "10pt")
.style("font-family", "sans-serif");
//Append path for salesLine
var sales = [];
dataset.forEach(function(d) {
sales.push(d.sales);
});
svg.selectAll(".salesLine")
.data([sales])
.enter().append("path")
.attr("class", "salesLine")
.style("fill", "none")
.style("stroke", "#000")
.attr("d", d3.svg.line.radial()
.radius(function(d) {
return radius(d);
})
.angle(function(d, i) {
return (i / dataset.length) * 2 * Math.PI;
}));
// Appending circles at intersections
svg.selectAll(".datapoints")
.data(dataset).enter()
.append("circle")
.attr("class", "datapoints")
.attr("r", 3)
.style("fill", "#4393c3")
.attr("cx", function(d) {
return radius(d.day);
})
.attr("cy", function(d) {
return radius(d.sales);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>