score:2
In your snippet...
d3.selectAll("g.type").filter(g_element => g_element.class !== "whatever");
... the first argument, which you named g_element
, is the datum bound to that element. As there is no data bound here, that's obviously undefined
.
To get the element instead, you have to use this
. However, since you're using a arrow function here, you need to use the second and third arguments combined:
d3.selectAll("g.type")
.filter((_,i,n) => console.log(n[i]))
Then to get the classes, you can simply use a getter...
d3.selectAll("g.type")
.filter((_,i,n) => console.log(d3.select(n[i]).attr("class")))
Or, even simpler, using classList
:
d3.selectAll("g.type")
.filter((_, i, n) => console.log(n[i].classList))
Here is the demo:
function create() {
let g = d3.select("body")
.append("svg")
.attr("height", "500")
.attr("width", "500")
.append("g");
g.append("g")
.attr("class", "type type-red")
.attr("data-color", "red")
.append("circle")
.attr("r", 50)
.attr("fill", "red")
.attr("cx", 50)
.attr("cy", 50);
g.append("g")
.attr("class", "type type-green")
.attr("data-color", "green")
.append("circle")
.attr("r", 50)
.attr("fill", "green")
.attr("cx", 200)
.attr("cy", 50);
g.append("g")
.attr("class", "type type-blue")
.attr("data-color", "blue")
.append("circle")
.attr("r", 50)
.attr("fill", "blue")
.attr("cx", 100)
.attr("cy", 150);
filter_out();
}
/***************** USING THE SELECTOR ********************/
function filter_out() {
d3.selectAll("g.type")
.filter((_, i, n) => console.log(n[i].classList))
.attr("opacity", 0.5);
}
create();
<script src="https://d3js.org/d3.v4.js"></script>
Source: stackoverflow.com
Related Query
- Function filter not working as expected in d3js
- d3js Force Layout Not Working As Expected
- d3js Opacity Transition Not Working as Expected
- filter data from csv using array filter not working as expected
- d3 .data() function not working as expected multiple data sources
- d3js tree.nodes() is not a function
- D3 function to parse the date not working
- Fisheye effect D3js image slideshow works on rects/objects but not image files, Error: <image> attribute x: Expected length, "NaN"
- LinkText is not working in tree layout d3js
- Remove event listener with d3js not working
- Collision detection for nodes of varying sizes not working as expected
- d3.js update pattern not working as expected
- controllerAs in directive's link function not working
- D3js "bars.enter().attr" is not a function
- d3.js date time format specifier not working as expected
- D3.js Pie Chart Dynamic Update Transitions not Working as Expected
- General update pattern not working as expected with d3.selection
- select function not working in 3.5.4 version of d3.js
- D3js responsive stacked bar chart - other topics solutions not working
- D3js arc animation not working when wrapping in object
- Drag rect not working as expected
- brush extents not working as expected
- C3 Extent not working as expected
- d3js does not enter into line function
- d3.js v5.9.1 - some methods in this version are not working as expected
- d3.map() function not returning expected results
- d3.js ticks values not working as expected
- D3.js v4 circle radius transition not working as expected
- d3js Text Transitioin - example code not working
- d3js - my line chart is not working
More Query from same tag
- Add svg pan zoom to dynamically created svg with D3js
- D3.js multi-series line chart using csv file
- Migrating from D3.js v3 to D3.js v4 not working - a select issue?
- Uncaught ReferenceError: d3 is not defined
- How can I add event in apex chart js?
- D3.JS unable to load very large .csv file
- d3.js changing data on click (multiple instances of Donut Chart )
- d3.js: How to add a data key?
- d3 update number - count up/down instead of replacing number immediately
- How do I select a specific parent using d3?
- Removing rows when reading data D3
- How to place marker head in the middle of the links
- 2 D3.js force layout graphs on same page?
- Error: <g> attribute transform: Expected number, "translate(NaN, 0)"
- D3 js unexpected behavior on rapid mouse clicks
- Best Way to Visualize Large Number of Categories
- How to get the total depth of an unknown JSON hierarchy?
- How to use an exit selection on text with tspan
- Block d3 .transition() until another transition has completed
- How to show values stacked bar chart utilizing dc.js and d3.js?
- Can't return a superformula in d3
- Legend in force-directed graph
- dc.js Using two reducers without a simple dimension and second grouping stage
- How to zoom in on a polygon in D3.js
- D3 V4 Pie Chart Angular2 Typescript path is incorrect
- Add labels or text to tooltip
- Access to spline tangents in D3
- GeoJson element rendered using D3 not scaling correctly on Leaflet map
- Overlay a function over a bar chart in D3
- How to reference CSV alongside GeoJSON for D3 rollover