I am making a plot with different circles on it. Each circle has a fixed position, but the size of each circle can be changed seperately via an html input field.
The code is use for the input field is this:
<input type="text" id="size1" class="size" value="" style="width: 30px;">
I have saved the data in this dataset:
var dataset = [{name: "circle1", xpos: -413, ypos: 278, color: "black", radius: $('#size1').val(),},
{name: "circle2", xpos: -161, ypos: 290, color: "black", radius: $('#size2').val(),}];
This is how I draw the circle:
function drawCircle () {
svg.selectAll("circle").remove();
svg.selectAll("circle")
.data(dataset)
.enter()
.append("svg:circle")
.attr("cx", function(d){
return xScale(d.xpos);})
.attr("cy", function(d){
return yScale(d.ypos);})
.attr("r", function (d){
return rScale(d.radius);})
.attr("fill", "rgb(100,0,0)")
.attr("opacity", "0.7");
}
Lastly I make a trigger that when something changes, the circle is drawn again:
$('.size').change(function(){
radius = $(this).val();
svg.selectAll("circle")
.transition()
.duration(750)
.attr("r", function (d){
return rScale(radius);})
});
This way when I change a value in #size1 or #size2, both circles are redrawn with the value that is last inputted.
My question is: How can I update the dataset in a way that each circle will ‘listen’ to his own input field?
I assume that you have multiple input-fields.
What you can do when you have a change on one of the fields, recreate the dataset and repaint the circles.
The static data for your dataset can be stored in a data-attribute on the input.
Like this:
HTML
javascript