score:2
Accepted answer
i think your problem here is confusion around the nest
function and that your nest
variable isn't holding a reference to that function but your data instead.
that said, i find your code overly confusing and not written to how you should be using d3
data binding abilities. so here's my commented refactor working example:
<!doctype html>
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<input type="radio" id="passed" name="stack" value="passed" />
<label for="passed">passed</label><br />
<input type="radio" id="category" name="stack" value="category" checked />
<label for="category">category</label><br />
<input type="radio" id="owner" name="stack" value="owner" />
<label for="owner">owner</label>
<div id="chart"></div>
<br/><br/>
<div id="legend"></div>
<script>
var doc = url.createobjecturl(
new blob([
`passed,category,owner,dup
yes,msg,hailey,yes
yes,msg,hailey,yes
yes,doc,hailey,yes
yes,doc,robert,yes
yes,msg,laquila,yes
no,spreadsheet,hailey,yes`,
])
);
// had to switch to csv for stacksnippet editor
d3.csv(doc)
.row(function (d) {
return {
passed: d.passed,
category: d.category,
owner: d.owner,
dup: d.dup,
};
})
.get(function (error, data) {
// container for legend
var legend = d3
.select('#legend')
//make the legend
.append('svg')
.attr('class', 'legend');
// initial draw
drawlegend('category');
// function to redraw legend
// based on d3 enter, update, exit
function drawlegend(currentselection) {
// apply nest function for new selection
let nesteddata = d3
.nest()
.sortkeys(d3.descending)
.key(function (d) {
return d[currentselection];
}).entries(data);
// update selection
let lu = legend
.selectall('g')
// the key function here is important
// so that d3 can calculate the enter, update, exit
.data(nesteddata, d => d.key);
// exit selection
lu.exit().remove();
// enter selection
// add container g
let le = lu
.enter()
.append('g')
.attr('transform', function (d, i) {
return 'translate(0,' + i * 25 + ')';
});
// on enter add rect
le.append('rect')
.attr('class', 'rect')
.attr('width', 18)
.attr('height', 18)
.style('fill', function (d,i) {
return d3.schemecategory20[i];
})
.style('stroke', 'grey');
// on enter add text
le.append('text')
.attr('x', 24)
.attr('y', 9)
.attr('dy', '.35em');
// enter + update
lu = le.merge(lu);
// on enter or update, change text
lu.selectall('text')
.text(function(d){
return d.key;
});
}
d3.selectall("input[name='stack']").on('change', function (d) {
drawlegend(this.value);
});
});
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- Is there a way to make a D3 legend change based on user input to radio buttons?
- Is there a way to change C3 legend item tiles to be icons?
- Is there a way to make a D3 force layout continually move?
- How to make label in histogram respond to dynamic user input
- Is there a way to make Internet Explorer understand this.style.setProperty?
- D3 Change color and scale based on radio button
- Is there a way to make the nodes draggable/to another parent or child node to rearrange the nodes in d3.js
- Updating NVD3 /D3 chart as per user input radio button
- Is there a way to change the type of the subcharts in c3.js
- Using Django, AJAX and JSONs to update charts based on user input
- Is there a way to make interactive standalone SVGs in a webpage?
- Update bar chart in d3 based on user input
- Filtering CSV records in d3.js enter/update based on user input
- Is there a way to make the correct toggled legends appear when downloading?
- Filter .csv data based on user input and refresh d3.js map
- How do I update a D3 SVG chart based on user input
- are there any way to change c3js stacked area chart opacity on mouse hover
- Is there a way to change the fill of a Cell in a Bar Chart on Hover? - Recharts
- Make appearance of one svg change based on action taken in other svg
- D3js value of .data(...) field does not change on window resize. Is there a way to change it?
- Dynamically change opacity of links and nodes in d3 force graph based on radio button filter
- d3 dendogram, is there a way to make a single node standout?
- Is there a way to make an SVG USE subsequently modifiable (or a different technique)?
- d3 input resets radio buttons
- Is there a way to display the value together with the label on the legend in C3js pie chart?
- Is there a way to make d3 scale.range dynamic so that it works with window resizing?
- What's the best way to make a d3.js visualisation layout responsive?
- Is there a way to zoom into a D3 force layout graph?
- Is there a way to tell crossfilter to treat elements of array as separate records instead of treating whole array as single key?
- Is there a way to convert CSV columns into hierarchical relationships?
More Query from same tag
- D3js labels on grouped bar chart
- D3: Bars not resizing upon window resize, for bar-chart
- Understanding a multi-line d3 chart example
- d3 arc transition error when toggling sweep-flag
- Modify Barwidth or Display Size in Collapsible Indented Tree
- D3@6 Generic direction on addressing Map data structures in arrow functions
- D3.js nested data - how can i display my circles
- D3 blur out / lower the opacity of non-related links when dragging a particular node
- Generating custom elements in d3
- javascript and d3.js variable scope: variable resets outside of function call
- how to sort dictionary object based on values in d3.js?
- D3 Sticky Force responsive
- javascript, convert an array of objects to an array of arrays vertically
- d3.js click and apply zoom and pan to distribute points located inside a targeted division to the triggered subdivisions
- Javascript data structure for json data
- D3 JSON file with source and index as strings rather than indices
- d3v4 - zooming (Equivalent to d3.zoom.x)
- d3.js and dimple.js two identical elements
- Setting different images for D3 force-directed layout nodes
- D3 .on change works on text not on chart
- Why is my d3.treemap() returning a data viz with big gaps?
- Change: Add Label to Draggable Circle
- Update path when point in graph is moving
- How to make a d3 multiple line chart with irregular data?
- How does Mike Bostock's (D3js's creator) pan & zoom example work?
- d3 timeseries, reading date from data, counting entries by date
- Making a sunburst diagram with D3.js
- Getting array length of items with a specific attribute
- D3 Zooming in graph
- Preserve the size of the svg group when changing the canvas size