score:1
The point of the d3 data pipeline is to avoid unnecessary explicit looping and procedural code. Looping still has a place.
But let's say you want to be idiomatic, and not loop. Then when you need is d3.range()
. It turns an integer bound into a list. E.g. d3.range(4) == [0, 1, 2, 3]
. If you're going through a pipeline, have an integer, and need elements for every value up to that integer, the key snippet is:
g.selectAll('circle')
.data(d => d3.range(d.shapes.circle))
.enter()
.append('circle')
which converts the number of circles coming in from your data item to that many circles. Unfortunately, it's not quite that simple in practice. You're processing a multi-level structure, and you probably want to not just produce N different circles, but vary those circles based on some higher-level context. So in practice it's more like:
g.selectAll('circle')
.data((d, i) => d3.range(d.shapes.circle)
.map(j => [i, j]))
.enter()
.append('circle')
That is, for the i'th record in your data array, the data fed to create circles combines i with the values of j coming from d3.range()
.
The full code's a bit longer than fits comfortably here, but here's a live example on Plunker.
Colors are chosen by the order in the input data; shape positioning and opacity are chosen randomly.
Source: stackoverflow.com
Related Query
- d3: Appending multiple elements based on integer in data
- Appending multiple non-nested elements for each data member with D3.js
- In D3, how can I create multiple elements for each data element based on value in data?
- d3.js appending different SVG elements based on a data value
- D3: update data with multiple elements in a group
- dc.js add class to data points in multiple charts based on criteria from first chart
- select data based on multiple attributes d3
- Appending multiple elements in D3
- Conditionally appending based on nested data with D3
- How to update multiple elements for each data
- D3 with AngularJs, Multiple charts on same page with different data based on common key
- A more elegant way to bind new elements using data bound to multiple group elements
- D3 - reordering elements based on their data
- Generating multiple DOM elements from a single data point with D3.js
- d3js - Add elements based only on nested data
- How to rearrange the DOM order of svg elements based on their data values in a dynamically changing d3 vis?
- Conditional fill on multiple svg based on data
- D3 - Appending SVG based on JSON Data
- Appending multiple elements in a single Join
- Binding data to existing number of elements based on object property
- Selection of elements based on data
- d3: Data Distribution to Elements Based on Specific Field
- Can I update DOM elements in a D3 selector based on a data join to a different data object?
- Can I update DOM elements in a D3 selector based on a data join to a different data object?
- d3.js : group dynamic SVG elements based on data
- Appending nested svg elements with D3js data binding?
- D3.js patterns for managing multiple elements bound to same data
- How to create elements depending on data in D3?
- D3.js binding an object to data and appending for each key
- Importing data from multiple csv files in D3
More Query from same tag
- how to use moment and date time in D3
- D3 Angular Tree
- Not all d3 points are showing in correct position on leaflet map
- Starting a d3 axis timescale from zero
- D3: Draw merged lines as a single
- D3 - loading xml data from file, using scales for barchart
- Fix x-axis position in scrollable Heatmap d3
- d3 v4 force layout with boundary
- TypeScript D3 v4 import not working
- d3 line graph with smooth update animation
- Examples showing animated transitions between linear and log scales in D3JS?
- D3 Pie chart initial render issue
- d3.brush is selecting surrounding divs with radial chart; only occurs d3 3.3+
- cant draw belgium on dc.js choropleth map
- outputing array variables javascript
- d3.js unique colors when plotting an arbitrary number of lines from an array of array of objects
- Increasing font size in C3 charts without cutting off the text
- Datamaps not displaying in my browser
- Sending HTTP request headers in C3.js to source URL
- How To Align Text Inside SVG In D3.js
- Angular Directive not updating DOM on first load
- d3 with multiple charts reading separate files in two different divs
- d3j : Remove a data set
- d3 bar graph error says xScale.bandwidth() is not a function
- How to use d3.js to visualize a JSON tree graph?
- d3 bar chart using ionic 4 failed to load on device on -- prod mode
- D3 VUEjs adding Attr and or call
- Fail to parse/project coordinates onto JSON
- (c3.js) how to capture a column state, wheter it's shown or not in the graph?
- Import a iPython Custom Widget Function (File Structure?)