score:3
As mentioned by echonax, the issue is you're creating multiple circles based on the dataset. To get the smooth transition, draw only one circle, and update the radius based on 'myCounter'. An example:
var dataset = [2184,2184,3460,2610,2610,2452,842,1349,2430];
var myCounter = 0;
//svg dimensions
var h = 200;
var w = 200;
var svg = d3.select('body')
.append('svg')
.attrs({
width: w,
height: h
})
.classed('middle',true);
//color mapping
var colorsScale = d3.scaleLinear()
.domain([d3.min(dataset),d3.max(dataset)])
.range(['#FFB832','#C61C6F']);
//radius mapping
var rScale = d3.scaleLinear().domain([0, d3.max(dataset)]).range([0,50])
//labels
var label = svg.append("text").attrs({
x: w/2,
y: 20
}).text(function(){ return dataset[myCounter] });
//draw the circles
var circle = svg.append('circle')
.attrs({
cx: w/2,
cy: h/2,
fill: function() { return colorsScale(dataset[myCounter]) },
r: function() { return rScale(dataset[myCounter]) }
});
//button click
d3.select('#theButton')
.on('click',function(){
updateData()
});
function updateData(){
myCounter++;
if ( myCounter > dataset.length - 1 ) myCounter = 0;
circle.transition().attr('r',function(){ return rScale(dataset[myCounter]) }).attr('fill', function() { return colorsScale(dataset[myCounter]) });
label.text(function(){ return dataset[myCounter] });
};
html, body{
height: 100%;
}
.middle{
margin: 100px auto;
}
#theButton{
position: absolute;
left:50px;
top:50px;
}
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
<button id="theButton" type="button">Click Me!</button>
Based on your data, there are a couple of times that the circle won't change as the data is the same, but the transition should work when it does.
Source: stackoverflow.com
Related Query
- D3.js v4 circle radius transition not working as expected
- circle radius not updating on transition in d3
- D3 Transition not working as expected
- In D3 chart, On Click is not working on circle as expected
- d3js Opacity Transition Not Working as Expected
- d3 transition for transform translate not working for DIV
- Increase and decrease radius of a circle using d3 transition
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- D3 transition not working
- d3 v4 line chart transition not working
- D3.v4 Projection Transition not Working
- Conditional transition not behaving as expected
- Collision detection for nodes of varying sizes not working as expected
- d3.js update pattern not working as expected
- 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
- Drag rect not working as expected
- brush extents not working as expected
- Transition circle radius in d3 to cancel scaling transition--works but timing is off
- C3 Extent not working as expected
- Transition not working
- d3.js circle plotting does not working properly while trying to load after first time
- d3.js v5.9.1 - some methods in this version are not working as expected
- d3.js ticks values not working as expected
- d3 transition on tooltip not working
- d3.js - dragmove circle with v6 not work as expected
- Transition on path in d3.js is not working
- D3 Zoom's translateExtent not working as expected
- D3 inside for loop not working as expected
More Query from same tag
- Refactoring d3js: multiple arcs & text blocks into tidy js functions
- D3 topology rendering issue in Chrome
- adding padding to svg g element
- heatmap.js - plotting multiple data points from .csv
- How does `d3.forceSimulation` adds `{x:number,y:number...}` on the data items?
- data binding in d3.js
- Fire zoom event (or simulate zoom event)
- Get coords on any chart point
- Fable D3 map sample
- Javascript closure and d3
- D3.js packed circle layout - how to adjust child radius
- d3 How to limit drag and drop area
- d3.js 3D array interpolation
- Use value of select option as name for a variable in Javascript
- d3.js controlling initial animation in force layout
- multiline tooltips show and hide d3.js
- Problems positioning multiple d3 graphs on html page
- D3.js stick parent object in middle of the svg
- Data model for creating a radial bar chart with d3js
- Break D3 Each Loop Without a flag
- Panning a bar chart in d3.js
- Multi Line Graph D3.js
- How to change point size using geojson file in D3
- How to highlight only certain coordinates in NVD3 line graph?
- How to convert/save d3.js graph to pdf/jpeg
- Line not following x os correctly & first tick isn't being shown
- TopoJSON - adding properties to a topoJSON file
- d3.brush does not work in Firefox or Edge when embedded
- nvd3: How highlight whole stacked bar on mouseover?
- Select segments of the same name in d3.js sunburst