score:1
Accepted answer
I answered that previous question. In your case, you are missing that the "x" variable you feed to line
is supposed to be an angle (in radians). You are giving it a "year".
Regardless, in your case I'd code this differently. Here's the proper transform with some basic trigonometry:
.attr("transform", function(d){
// get angle and radius
var an = l(d.Birth),
ra = r(d.Point),
// calc x, y position
x = ra * Math.cos(an * Math.PI/180),
y = ra * Math.sin(an * Math.PI/180);
return "translate(" + [x,y] + ")";
})
Also note, that I would just position the g
and add the circle and text to it. Not, position the two elements separately.
Running code:
<!DOCTYPE html>
<html>
<head>
<style>
.frame {
fill: none;
stroke: #000;
}
.axis text {
font: 10px sans-serif;
}
.axis line,
.axis circle {
fill: none;
stroke: #777;
stroke-dasharray: 1, 4;
}
.axis:last-of-type circle {
stroke: #333;
stroke-dasharray: none;
}
.line {
fill: none;
stroke: orange;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1200,
height = 700,
radius = Math.min(width, height) / 2 - 30;
//radius
var r = d3.scale.linear()
.range([0, radius]);
//Line
var l = d3.scale.linear()
.range([0, 360]);
//point
var c = d3.scale.linear()
.range([0, 15]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var data = [{
'Name': 'A',
'Birth': 1700,
'Point': 20,
'size': 120
}, {
'Name': 'B',
'Birth': 1696,
'Point': 45,
'size': 30
}, {
'Name': 'C',
'Birth': 1660,
'Point': 89,
'size': 50
}, {
'Name': 'D',
'Birth': 1784,
'Point': 12,
'size': 80
}, {
'Name': 'E',
'Birth': 1793,
'Point': 33,
'size': 150
}, {
'Name': 'F',
'Birth': 1750,
'Point': 56,
'size': 67
}, {
'Name': 'G',
'Birth': 1738,
'Point': 62,
'size': 25
}, {
'Name': 'H',
'Birth': 1813,
'Point': 87,
'size': 83
}, {
'Name': 'I',
'Birth': 1723,
'Point': 76,
'size': 100
}, {
'Name': 'J',
'Birth': 1786,
'Point': 53,
'size': 90
}]
var youngest = d3.min(data, function(d) {
return d.Birth;
});
var oldest = d3.max(data, function(d) {
return d.Birth;
});
l.domain([Number(youngest), Number(oldest)]);
r.domain([0, d3.max(data, function(d) {
return d.Point;
})]);
c.domain([0, d3.max(data, function(d) {
return d.size;
})]);
var line = d3.svg.line.radial()
.radius(function(d) {
return r(d[1]);
})
.angle(function(d) {
return l(d[0]) + Math.PI / 2;
});
var gr = svg.append("g")
.attr("class", "r axis")
.selectAll("g")
.data(r.ticks(5).slice(1))
.enter().append("g");
gr.append("circle")
.attr("r", r);
gr.append("text")
.attr("y", function(d) {
return -r(d) - 4;
})
.attr("transform", "rotate(15)")
.style("text-anchor", "middle")
.text(function(d) {
return d + '%';
});
var ga = svg.append("g")
.attr("class", "a axis")
.selectAll("g")
.data(l.ticks(12).slice(0, -1))
.enter().append("g")
.attr("transform", function(d) {
return "rotate(" + (l(d)) + ")";
});
ga.append("line")
.attr("x2", radius);
ga.append("text")
.attr("x", radius + 6)
.attr("dy", ".35em")
.style("text-anchor", function(d) {
return l(d) < 270 && l(d) > 90 ? "end" : null;
})
.attr("transform", function(d) {
return l(d) < 270 && l(d) > 90 ? "rotate(180 " + (radius + 6) + ",0)" : null;
})
.text(function(d) {
return d;
});
var color = d3.scale.category20();
var gc = svg.append("g")
.attr("class", "circles")
gc.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("class", "cpoint")
.attr("transform", function(d) {
// get angle and radius
var an = l(d.Birth),
ra = r(d.Point),
x = ra * Math.cos(an * Math.PI / 180),
y = ra * Math.sin(an * Math.PI / 180);
return "translate(" + [x, y] + ")";
})
.append("circle")
.attr("class", "point")
.attr("r", function(d) {
return c(d.size);
})
.attr("fill", function(d, i) {
return color(i);
});
gc.selectAll(".cpoint")
.append("text")
.attr("text-anchor", "middle")
.text(function(d) {
return d.Name + ' ' + d.Point + '%' + ' ' + d.Birth;
})
.style("font-size", '13px');
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- How to find coordinates to put circle on polar scatterplot using d3
- how to get the coordinates of the center of a circle using d3.js
- Using D3js, How to find the x,y coordinates after rotation of rectangle in plan?
- d3.js Plot elements using polar coordinates
- How to set circle at the right end of the last bar / item using D3
- How to place text on the circle when using D3.js force layout?
- How to make circle to appear one after another using d3 transition?
- How to add a line graph to a scatterplot using d3?
- How to make a scatterplot based upon certain criteria using d3.js
- How to convert latitude and longitude into (x,y) coordinates using d3.js
- how to update data form file json using d3.js (zoomable circle pack)
- how to adjust parent circle radius using d3js pack layout
- In a d3 scatterplot using data from a csv file, how do i draw lines connecting related points when the mouse is over a point?
- How to create multi-polygon intersecting a circle / necklace using d3.js?
- How do I get the x,y coordinates of the center of the circle the user mouses over?
- how i can put more lines in Line Plus Bar Chart using nvd3.js?
- How to find the coordinates of a text in an svg?
- How do you find the last data point in d3 and draw a circle and a line on it?
- How to get a circle position in a g element in SVG using D3.js
- D3.js: How can I put an image on the background of a circle with the remaining parts trimmed and make it resize along with the circle onclick?
- How to find a div using jquery when it is appended using d3.js
- How to find max value from a 2D matrix using D3.js
- How to add labels to my scatterplot from data using d3.js
- How to create variable number of image or circle elements in an SVG element using a loop in D3 js(v3)
- Transform mouse svg to polar plot coordinates using d3.js
- How would I stack an SVG circle on top of another one using D3?
- How to show x and y coordinates of XY chart with mouseover using d3.js
- How can I put a space between the lines of a line chart using d3.js
- How do I change the position of a circle svg element using the transform attribute?
- how to find position of circles between lines in d3 polar scatter chart
More Query from same tag
- JS Recursion Maximum call stack size exceeded: difference between 2 functions
- using json data in a flask endpoint for d3 script
- ngIf inside custom directive and $watch
- how to implement elements to d3.js?
- Precalculate and set initial positions of nodes in D3.js
- How to smooth in zoom in and zoom out in D3.js with 50,000 nodes?
- How to make d3 area transition originate from bottom
- Extending d3.selection in v5—is this the correct way to do it?
- Line graph using integers on y axis and strings on x axis
- d3.js - Dendrogram display adjusted to the tree diagram
- d3.js: ordinal scale and zoom/pan
- d3: canvas zooming with multiple images
- Add html content with line break inside d3 circle
- How to tell when a D3 force layout has stopped
- How to create linked (href) labels in a d3 circle packing chart?
- React + d3 multiple charts components with different data
- d3.js How to draw line chart with vertical x axis labels
- 'nvd3' is not a known element: 1. If 'nvd3' is an Angular component, then verify that it is part of this module
- Issue with rectangles not drawing equal with yaxis labels in d3v4
- Creating D3 word-cloud
- dc.js - is it possible to show the user grab handles upon page load
- D3 cardinal line interpolation looks wrong
- Unexpected Data Binding Behavior
- Using d3 in Leaflet
- D3.js: Pause button not working (in a callback function used for animation)
- D3.js not defined with Electron
- Toggle D3 shape depending on Boolean
- How to match up scaleBand with scaleLinear in D3.js
- Writing simple force layout app using D3.js
- NVD3: Have labels at certain tick values at the top of the graph