This is my d3.js piece of code :
<script>
var width = 300,
height = 300,
margin = 20;
var x_centre = width/2;
var y_centre = height/2;
var nuclear_radius = 15;
var vis = d3.select("#chart_container").append("svg:svg")
.attr("width", width)
.attr("height", height);
var radiuses = [1,2,3];
var multiplier = (d3.min([width, height]) - 2*margin - nuclear_radius) / (d3.max(radiuses) * 2);
var shells = vis.selectAll("circle.shell")
.data(radiuses)
.enter().append("circle")
.attr("class", "shell")
.attr("cx", x_centre)
.attr("cy", y_centre)
.attr("r", function(d, i) {
return (d * multiplier);
});
var nucleus = vis.selectAll('circle.nucleus')
.data(['nucleus'])
.enter().append("circle")
.attr("class", "nucleus")
.attr("cx", x_centre)
.attr("cy", y_centre)
.attr("r", nuclear_radius);
function showGraph(de) {
var electrons = de
var radius_counts = {};
for (var i = 0; i < electrons.length; i++) {
if (electrons[i].distance in radius_counts) {
radius_counts[electrons[i].distance] += 1;
} else {
radius_counts[electrons[i].distance] = 1;
}
}
// Calculate the x- and y-coordinate values
// for each electron.
var keep_count = {};
electrons.forEach(function(d) {
var siblings = radius_counts[d.distance];
if (d.distance in keep_count) {
keep_count[d.distance] += 1;
} else {
keep_count[d.distance] = 1;
}
var angle = (360/siblings) * keep_count[d.distance];
var hyp = d.distance * multiplier;
if (angle <= 90) {
var use_angle = (90 - angle) * (Math.PI/180);
var opp = Math.sin(use_angle) * hyp;
var adj = Math.cos(use_angle) * hyp;
d.x = x_centre + adj;
d.y = y_centre - opp;
} else if (angle <= 180) {
var use_angle = (180 - angle) * (Math.PI/180);
var opp = Math.sin(use_angle) * hyp;
var adj = Math.cos(use_angle) * hyp;
d.x = x_centre + opp;
d.y = y_centre + adj;
} else if (angle <= 270) {
var use_angle = (270 - angle) * (Math.PI/180);
var opp = Math.sin(use_angle) * hyp;
var adj = Math.cos(use_angle) * hyp;
d.x = x_centre - adj;
d.y = y_centre + opp;
} else {
var use_angle = (360 - angle) * (Math.PI/180);
var opp = Math.sin(use_angle) * hyp;
var adj = Math.cos(use_angle) * hyp;
d.x = x_centre - opp;
d.y = y_centre - adj;
}
});
var electron_nodes = vis.selectAll('circle.electron')
.data(electrons)
.enter().append("circle")
.attr("class", "electron")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 7)
.on("mouseover", function(d) {
// how to drag the electron around the circle?
});
var svgData = vis.selectAll("circle.electron")
.data(electrons);
svgData.exit().remove();
}
</script>
Now i am calling the above showGraph js function
every time on click event which takes the count
parameter
function add_py()
{
var count = $( "#count" ).val();
$.ajax({
type: "get",
url: "abc.py",
data: {'count': count},
datatype:"script",
async: false,
success: function(response) {
var data= JSON.parse(response);
//alert(response)
showGraph(data);
}, // success closed
error:function(xhr,err)
{
alert("Error connecting to server, please contact system administator.");
}
})//ajax closed
}
Now when I give count 10 , the SVG is like this :
<svg width="300" height="300">
<circle class="shell" cx="150" cy="150" r="40.833333333333336">
<circle class="shell" cx="150" cy="150" r="81.66666666666667">
<circle class="shell" cx="150" cy="150" r="122.5">
<circle class="nucleus" cx="150" cy="150" r="15">
<circle class="electron" cx="231.66666666666669" cy="150" r="7">
<circle class="electron" cx="256.08811196359375" cy="211.25" r="7">
<circle class="electron" cx="43.91188803640625" cy="211.25" r="7">
<circle class="electron" cx="185.36270398786456" cy="170.41666666666669" r="7">
<circle class="electron" cx="114.63729601213541" cy="170.41666666666666" r="7">
<circle class="electron" cx="150" cy="231.66666666666669" r="7">
<circle class="electron" cx="150" cy="27.5" r="7">
<circle class="electron" cx="150" cy="109.16666666666666" r="7">
<circle class="electron" cx="68.33333333333333" cy="150" r="7">
<circle class="electron" cx="150" cy="68.33333333333333" r="7">
</svg>
Works fine exactly 10 circles with class electron
with different cx and cy
plots . But if i give first count 7 or anything less then 10 then 10 , the svg is like :
<svg width="300" height="300">
<circle class="shell" cx="150" cy="150" r="40.833333333333336">
<circle class="shell" cx="150" cy="150" r="81.66666666666667">
<circle class="shell" cx="150" cy="150" r="122.5">
<circle class="nucleus" cx="150" cy="150" r="15">
<circle class="electron" cx="150" cy="68.33333333333333" r="7">
<circle class="electron" cx="150" cy="272.5" r="7">
<circle class="electron" cx="150" cy="27.5" r="7">
<circle class="electron" cx="150" cy="190.83333333333334" r="7">
<circle class="electron" cx="150" cy="109.16666666666666" r="7">
<circle class="electron" cx="150" cy="231.66666666666669" r="7">
<circle class="electron" cx="150" cy="27.5" r="7">
<circle class="electron" cx="150" cy="109.16666666666666" r="7">
<circle class="electron" cx="68.33333333333333" cy="150" r="7">
<circle class="electron" cx="150" cy="68.33333333333333" r="7">
</svg>
Plots exactly 10 circles with class electron
but the cx
and cy
repeats so they overlap . But it does not overlap if I give count 10 first time .It works fine if i give a big count then small. But not if small then big. Thanks in advance for helping me.
See Question&Answers more detail:
os