score:1
Whatever you are trying to do (which is not clear at all, mainly because there are many alternatives to properly creating an animation), the problem with your code is that you are appending a new SVG at every loop.
Instead of that, append the SVG just once and change its height:
var height1 = 0;
var svgContainer = d3.select("body").append("svg")
.attr("width", 100);
for (let height1 = 0; height1 < 20; height1++) {
setTimeout(function timer() {
//make SVG container
svgContainer.attr("height", height1);
//Draw circle
var circle = svgContainer.append("circle")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 50);
}, height1 * 500)
}
<script src="https://d3js.org/d3.v4.min.js"></script>
On the other hand, if you really want to append a SVG at every loop and end up with a bunch of SVGs on top of each other (for whatever reason, it's not my place to judge), then you have to use position: absolute
:
var height1 = 0;
for (let height1 = 0; height1 < 20; height1++) {
setTimeout(function timer() {
//make SVG container
var svgContainer = d3.select("body").append("svg")
.attr("width", 100)
.attr("height", height1);
//Draw circle
var circle = svgContainer.append("circle")
.attr("cx", 50)
.attr("cy", 50)
.attr("r", 50);
}, height1 * 500)
}
svg {
position: absolute;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
score:1
To complement Gerardo Furtado’s response, pay attention to those two height1
variables in your script. I guess you didn’t intend to have two of them.
One is declared with var
at the top scope of your script; the other one is declared with let
and is local to your loop. I insist on the point that they are two different variables, and I suggest you remove the one you declared with var
.
Source: stackoverflow.com
Related Query
- How do you stack svgs on top of each other that are generated from a for loop using javascript?
- How to stack these circles on top of each other in D3.js?
- How to update elements of an HTML that the elements are created using data from a CSV file?
- D3 V4 zoom via mouse and programmatic are zooming independent from each other
- How to read JSON data from same json for tree layout insteadof reading from other .JSON file?
- How do I add bubbles on each of the y-axis values that I have and how do I add mouseover function for each of them?
- d3.js: How to deal with svg elements that are being appended on the same position, one on top of the other?
- How to create separate tags for each for loop iteration?
- Why the bars are on top of each other
- How to force d3's date axis to draw ticks for first and last months that are not full [FIDDLE UPDATED]?
- How do I properly sort the data for my d3 bubble map so that smaller bubbles show up on top of larger bubbles?
- In d3 how can you format a date with single digits for month and year if they are under t0?
- How do you create a d3 line function that will draw 2 lines for x, y1, y2 data?
- How to add text on top the heatmap to properly show value for each rectangle when using D3.js?
- d3 drag when elements are on top of each other
- In d3js, how to plot line chart from csv file, taking data from seperate columns for each row
- How to select elements from array field in dc.js / crossfilter / d3 and use each of them as separate data point for chart?
- Progressive path animation in d3.js: how do I loop over individual elements of a d3 selection, for eg, each path segment of an area chart?
- How to write vertical text from bottom to top without using transform rotate?
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How are svg, d3, dagre, dagre-d3 and graphlib dependent on each other?
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- How to simulate mouse move in D3 so when you drag nodes, other nodes move automatically?
- How can I get "call" to execute for each data element in D3?
- How to create a reusable component in D3 with an API that manipulates each instance of the component?
- Where can I get the .geojson file for India and not separate files for each state/territory or any other distinction?
- In d3 for javascript, how do you create different elements for your data?
- d3.js create objects on top of each other
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- How to make a tiled image pyramid for leaflet from a non-geographic source
More Query from same tag
- d3js zooming results in lines going below x axis (not being scaled)
- Create graphs similar to Pew Research site
- Cubism rendering wrongly
- Basic JSON Formatting for C3.js when data loaded via URL using Rails
- Updating D3 force layouts - adding and removing nodes corrupt the page
- Only select subset in d3
- Converting Query result to D3.js hierarchical JSON format
- How to draw growth rate line in dc.js barChart?
- What does this d3 time format function mean?
- D3JS V4 Stop append of a tspan based on value
- Using d3 SVG Scale without translate
- d3 domain y not animating during legend toggles
- Handling extraneous characters/wildcards in d3 time format
- Add data in pie chart using D3
- d3 multiple y axis for multiple bar chart
- Why can I input an argument on a scale?
- How to fix map boundaries on d3 cartographic raster reprojection?
- How to summarize an array with group and rollup from d3-array?
- Why is the 'this' variable not the expected value?
- D3 LineGraph Circles Remove Overlap && Sort dates
- D3 slider issue
- Data bound to children is not updated with new data
- Edge on interactive d3 visualization
- old elements not removed from d3 bar chart on update
- Transform flat table to hierarchy
- d3.min.js Uncaught TypeError: a.map is not a function
- D3 brushing on grouped bar chart
- Make an SVG linearGradient follow the path
- d3.js -- stacked bar with color categories
- Why isn't my map function returning all the csv file rows in d3 js?