score:1
Accepted answer
First of all you can call the update function like this:
var inter = setInterval(updateChart, 5000);
The logic which would simulate the fetch is the following:
function fetchData() {
console.log('fetching');
return new Promise(function(resolve, reject) {
var data = [{
attendee: "Paul",
coins: Math.floor(Math.random() * 40) + 1
}, {
attendee: "Bessy the Cow",
coins: Math.floor(Math.random() * 40) + 1
}, {
attendee: "Zeke",
coins: Math.floor(Math.random() * 40) + 1
}];
setTimeout(function() { // Adding timeout to simulate latency
resolve(data);
}, 4000)
})
}
Then we create an update function which will use the newly retrieved data:
function updateChart() {
fetchData()
.then(function(data) {
// Update our y domain with new coin values
y.domain([0, d3.max(data, function(d) {
return d.coins;
})]);
// Update our axis because our y domain just changed
svg.select('g.y')
.transition()
.duration(300)
.ease("linear")
.call(yAxis);
// Create a new data join with the simuldated data
var bars = svg.selectAll('.bar').data(data);
// Remove extra elements (say new data just has 2 bars, this would remove third one)
bars.exit().remove();
// Update existing elements
bars.transition()
.duration(300)
.ease("linear")
.call(renderBar);
// Add new elements (say new data has 5 bars, this would add the additional 2)
bars.enter().append('rect')
.transition()
.duration(300)
.ease("linear")
.call(renderBar);
})
}
I created the renderBar function since we are basically repeating the same routine at adding and updating.
function renderBar(rect) {
rect.attr("class", "bar")
.attr("x", function(d) {
return x(d.attendee);
})
.attr("width", x.rangeBand())
.attr("y", function(d) {
return y(d.coins);
})
.attr("height", function(d) {
return height - y(d.coins);
});
}
This plunkr shows the working code, I removed the d3.tip part:
Source: stackoverflow.com
Related Query
- D3 - transition after reading updated data from external file
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- queue.js pass data from local variable not from external file
- reading d3 data in from a file rather than hard coding it into a programme
- data is undefined after importing from a csv file with d3
- ScatterChart in NVD3 – Reading the data from csv file
- Loading internal JSON data rather than from an external resource / file for a sunburst in d3
- JSON data undefined from external file in d3.js?
- d3.js and dimple, reading data in from a file rather than hard coded
- Reading json data from a file in d3.js not working
- d3.js not reading my JSON data from PHP file
- using variable to parse data with D3js when reading in from csv file
- How do I access an external data file from withtin a javascript file in Spring MVC?
- d3.js Chart breaks after moving json data to external file
- read updated data on client from file created by server JSP
- D3js take data from an array instead of a file
- How to load data from a CSV file in D3 v5
- d3 - reading JSON data instead of CSV file
- Cannot import data from csv file in d3
- D3.js loading local data file from file:///
- Dynamically loading external data from database into d3.js
- d3 - sunburst - transition given updated data -- trying to animate, not snap
- Date and time transition from data with line
- How to render a directive only after data is loaded from tsv in angular js
- display data from csv file into BarGraph using d3.js
- Plotly/D3 – Reading data from CSV into separate traces
- Data file with illegal column names, reading header as keys
- Label names are not displayed from input data in pie transition chart of d3.js
- Unable to reference d3.js data imported from a csv file with spaces in the header
- How to load external JSON data from a Web API in d3.js?
More Query from same tag
- Incomplete line chart with missing points and missing lines
- Replacing JSON file with CSV for d3js
- Circle clip and projection with D3 orthographic
- Javascript sorting for a SunburstR Graph
- Insert element as sibling using D3js
- How to get a D3.js pie chart to render data dynamically from the DOM
- D3.js circles order
- D3.js: x-axis with variable interval between ticks
- Drawing heatmap with d3
- Confused by this Javascript shorthand
- On map of US I would like to add hyperlink to each state
- How to animate elements for every swap in a array using d3.js
- Problems with event listening while re-rendering SVG
- Scale and Center D3-Graphviz Graph
- d3 - how to add color to path object
- Attempting to implement a svg:clipPath but end up with almost a blank screen
- dimple.js Cannot Fix these issues
- d3fc: passing different data to rendering methods
- Histogram with ordinal data
- How do you set periodic timers in d3.js?
- automate color pick for the stacked bar plots
- D3 - Add background fill to rect
- Add labels to nodes in Hobbelt's "Group/Bundle Nodes" D3 force layout example?
- Find intersection of SVG path
- How to make lines in d3 line charts smoother?
- d3.js to remove two elements with matching attribute when clicked consecutively
- show data of each circle of bubble chart in table d3.js
- d3 label on stack bar which has enough space to show it
- Scale path to fit SVG height without blind trial and error
- I want to run d3 from a Cakefile