score:1
Accepted answer
With d3v3 (as I can see from your code you use this version) you should update your chart this way:
In update
function set the new domain for yScale
:
function update(newData) {
yScale.domain([0, d3.max(newData)]);
After that, apply new selection with selectAll("rect").data(newData)
, store selection in rects
variable and set new value for appropriate attributes (if you do not want animation effect, remove .transition() .duration(300)
):
var rects = d3.select("#chart svg")
.selectAll("rect")
.data(newData);
// enter selection
rects
.enter().append("rect");
// update selection
rects
.transition()
.duration(300)
.attr("y", function(d) {
return h - yScale(d);
})
.attr("height", function(d) {
return yScale(d);
})
Exit selection with exit
method:
rects
.exit().remove();
Do the same way with text
. I rewrite your code, look at the example in a hidden snippet below:
var myData = [21, 3, 5, 21, 15];
//Width and height
var w = 250;
var h = 250;
var yScale = null;
function draw(initialData) {
var xScale = d3.scale.ordinal()
.domain(d3.range(initialData.length))
.rangeRoundBands([0, w], 0.05);
yScale = d3.scale.linear()
.domain([0, d3.max(initialData)])
.range([0, h]);
//Create SVG element
var svg = d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(initialData)
.enter()
.append("rect")
.attr("x", function(d, i) {
return xScale(i);
})
.attr("y", function(d) {
return h - yScale(d);
})
.attr("width", xScale.rangeBand())
.attr("height", function(d) {
return yScale(d);
})
.attr("fill", "steelblue");
svg.selectAll("text")
.data(initialData)
.enter()
.append("text")
.text(function(d) {
return d;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return xScale(i) + xScale.rangeBand() / 2;
})
.attr("y", function(d) {
return h - yScale(d) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
}
function update(newData) {
yScale.domain([0, d3.max(newData)]);
var rects = d3.select("#chart svg")
.selectAll("rect")
.data(newData);
// enter selection
rects
.enter().append("rect");
// update selection
rects
.transition()
.duration(300)
.attr("y", function(d) {
return h - yScale(d);
})
.attr("height", function(d) {
return yScale(d);
})
// exit selection
rects
.exit().remove();
var texts = d3.select("#chart svg")
.selectAll("text")
.data(newData);
// enter selection
texts
.enter().append("rect");
// update selection
texts
.transition()
.duration(300)
.attr("y", function(d) {
return h - yScale(d) + 14;
})
.text(function(d) {
return d;
})
// exit selection
texts
.exit().remove();
}
window.onload = draw(myData);
setInterval(function() {
var data = d3.range(5).map(function() {
return parseInt(Math.random() * 20 + 1);
});
update(data);
}, 3000)
<div id="chart"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
Source: stackoverflow.com
Related Query
- How to update d3.js bar chart with new data
- D3 How to update the chart after selection from drop down menu with new data
- update d3 chart with new data
- Making a d3 chart update with new data
- Update chart with new data not working
- D3 updating bar chart with new data set cause update issue
- d3js - How to update an existing svg path in a simple d3 line chart with new data flowing through?
- Compare/Diff new data with previous data on d3.js update
- update d3 pie chart with new data.json
- d3.js chart not updated with new data
- D3.js with update button adds new data to old instead of replacing old data
- How to smoothly update a D3 v4 force diagram with new data
- how to update d3 chart when receiving new DATA
- Update d3 graph with new data
- Trying to update d3.js circles with new data
- Redraw D3 Pie Chart with new data when clicking button or enter search field
- Why doesn't my pie chart update with the new size?
- How to update data in d3 chart with an onchange slider event?
- d3 bar chart labels not getting updated on updating the chart with the new data
- JavaScript D3 bar chart data will not update when new source is selected from drop down filter
- How to update a d3 grouped barchart with new data by using join in v6
- Updating d3 bar chart labels with new data of variable length
- Chart not updating with new data with transitions in D3
- update graph with new data
- updating d3 chart with new data, the old data points not removed
- How to update (but not redraw) a d3-geomap with new data
- Unable to plot chart with new data format
- How to update selection with new data in D3?
- Updating a pie chart with a new data set with more values
- Update Bar chart on new data
More Query from same tag
- .delay() is not a function in d3
- Transitioning basis line to linear line shortens/hide part of the line
- change font size of axisLabel
- Changing the color of the objects of SVG in D3
- D3 - Binding CSV Data to Array
- Understanding Typescript definition
- Performing double mouseup events with other element in d3
- How to send HTML to an embedded WebView without running a local webserver?
- Is there any way that I can covert bar to circle in D3 JS
- How to increase NVD3JS Pie-Chart label size
- Getting a parent's child to find parent's child text width
- Create Google Maps MarkerImage from SVG in DOM
- How can I fix my chart so that the scale of the x axis is defined by a group of labels?
- D3 Pie chart tooltip and animation issue
- Possible to visualize neo4j graph in ruby on rails application?
- Trying to Assign Color for Null values in a map D3,js v4
- How to avoid the overlapping of text elements on the TreeMap when child elements are opened in D3.js?
- Overlapping legend d3.js
- Label only 10 N in D3 graph nodes
- Can I run a component method only in the client using Angular2 Universal?
- trouble understanding d3 tree node transitions
- Bars are going beyond the chart range in d3 bar chart
- How to remove a single child node in tree layout
- Pre-projected geometry v getting the browser to do it (aka efficiency v flexibility)
- Importing data into d3.js from javascript variables
- Select element with double dot in id, error: "#octo:cat" is not a valid selector
- d3 v4 how to add data labels to bar graph
- how to dynamically place lat, lon data onto a topoJSON map
- Combining Bivariate Graph with Line Graph in a single chart
- How to change the existing html text in a D3 rect?