score:2
Do not remove elements only to re-append them in a D3 code, as suggested in the comments: this is not the idiomatic solution, it's a slow and ineffective workaround and it can make things way harder further in the code. This is what I call a lazy update.
Instead of that, use proper enter/update/exit selections. Here is a refactor of your code, in which I'm purposely avoiding merge()
, which is difficult to understand at first, hence some code duplication.
Here it is, this is the update selection:
var cards = svg_sum.selectAll(".hour")
.data(flatList);
Now comes the exit selection:
cards.exit().remove();
Then, you have the enter selection, with the transition:
cards.enter().append("rect")
.attr("x", function(d, i) {
return (d.hour) * gridSize;
})
.attr("y", function(d) {
return (d.day - 1) * gridSize;
})
.attr("rx", 4)
.attr("ry", 4)
.attr("class", "hour bordered")
.attr("width", gridSize)
.attr("height", gridSize)
.style("fill", colors[0])
.transition().duration(1000)
.style("fill", function(d) {
return colorScale_sum(d.value);
});
And, again, the same transition for the update selection.
cards.transition().duration(1000)
.style("fill", function(d) {
return colorScale_sum(d.value);
});
As I said, you can avoid that repetition using merge()
. Things are more complicated regarding the legend, since you have groups there (have a look at my fork to see the selections).
Here is your updated bl.ocks: http://bl.ocks.org/GerardoFurtado/d938a52519b4701704137e8c8e6c826d/bacf8134a81c37e977094fe80afd6c12866b7c58
Source: stackoverflow.com
Related Query
- Interactive update does not work in heatmap
- d3 multiline update path and transition path does not work with nested data
- mpld3 interactive legend does not work in IE
- Importing local json file using d3.json does not work
- Svg clip-path within rectangle does not work
- d3.transition().attr('x', y) does not work when d3.attr('x', y) does
- d3.on("mouseover") event does not work with nested SVG elements
- d3.tree => transform does not seem to work
- Brushing on ordinal data does not work
- d3 zoom on scroll does not work anymore on Chrome
- Selection does not work if "too exact"
- bisectLeft function does not work if the second parameter is numerical
- The mouseover event for D3.js does not work in Leaflet
- Transition duration does not work on page refresh
- D3.js - DOM reorders after sort but chart does not update
- d3.js: styling <text> does not work
- Transition and opacity does not work well together
- Why does this title graphic not update with World
- Data Join with Custom Key does not work as expected
- d3 .tsv file does not work
- Why does this code work for counting one item in a list but not the others?
- d3 js transition over the array of rectangles does not work
- Integrating AngularJS and DagreD3 - AngularJS directive does not update on model change.
- d3.js node translation does not work when changed the node to image
- Simple function does not work within if-statement
- embedding an image via d3 does not work
- d3 svg zoom does not work when neighbouring element exists
- D3: dataset update does not remove elements from DOM
- Constructed SVG does not work where HTML-drawn does
- Updating the table multiple times using D3 does not work
More Query from same tag
- angular2: apply class to d3 element from component's style
- D3.js: what is 'g' in .append("g") D3.js code?
- unable to plot polygons in d3.js "format" over leaflet.js
- How to load local JSON to create a d3 chart without a local server?
- d3js diagonal paths in tree layout don't look right
- D3.js multiple force layout graphs from json file
- Graphs update only when I write enter() at the end
- Pattern not rendering when using group element (g) in d3, but rendering when made as an individual SVG
- avoid enumerating group names in d3.layout.stack()["group 1", "group 2"]
- Button doesn't work when updating cards category
- D3 Chart does not redraw on new data
- d3.js firefox mouse events perormance
- Make simple bar chart using C3 with separate columns on the x axis
- javascript / d3 - passing parameters to a function
- adding word wrap for D3.js tree chart labels
- NVD3 Sparkline not rendering correctly from CSV file
- How do you create a d3 line function that will draw 2 lines for x, y1, y2 data?
- How to create histogram with fixed bin width in D3.js v4?
- Reversing the Data Array in D3.js Given a JavaScript Object with Key, Val Pair
- how to handle the padding when brush moving
- Using dc.js on nested JSON to create bar chart
- What is the difference between require('d3') and require('d3-selection')
- Injecting dynamically created components into dynamically created tree graph
- Curve tension is weird in d3 v4
- Why isn't d3 text appearing on bar chart when using new dataset?
- How to fix ".join is not a function" when trying join after selectAll?
- add Text inside the Boxgroup in heat map dc.js
- Mapping with D3: Feature clipping error in my spinning globe
- How do i plot multiple lines in the same line graph using the D3 framework?
- Live Horizontal Bar Chart keeps adding nodes