score:4
Accepted answer
Things are getting messed up because you've added mouseover
and mouseout
event listeners that try to perform actions that conflict with the ongoing transition events. To fix the problem, don't add the mouseover
/mouseout
listeners until after the chart bars have performed their initial transition. You can add a listener for the end of the transition using transition.on('end', function(){...}
and then add the mouse event listeners to the DOM elements once the transition has completed.
d3.select('#whateverItIs')
// stuff to do prior to transition
.transition()
// transition stuff
.on('end', function() {
d3.select(this)
.on("mouseover", function() {
// handler code here
})
.on("mouseout", function() {
// handler code here
})
})
With your code:
function draw() {
var width = $(window).width();
var height = document.body.clientHeight;
var data = [{
country: "Pichonita",
growth: 15
},
{
country: "Andromeda",
growth: 12
},
{
country: "India",
growth: 33
},
{
country: "Indonesia",
growth: 22
},
{
country: "Russia",
growth: 6
},
{
country: "Mars",
growth: 41
},
{
country: "Pluton",
growth: 16
},
{
country: "Earth",
growth: 24
},
{
country: "Neptune",
growth: 8
}
];
//set margins
var margin = {
top: 30,
right: 30,
bottom: 30,
left: 40
};
var width = width - margin.left - margin.right * 2.5;
var height = height - margin.top - margin.bottom;
//set scales & ranges
var xScale = d3.scaleLinear()
.range([0, width - 100])
var yScale = d3.scaleBand()
.range([0, height]).padding(.2)
//draw the svg
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right * 3)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left * 2 + "," + margin.top + ")")
//force data
data.forEach(function(d) {
return d.growth = +d.growth;
});
//set domains
yScale.domain(data.map(d => d.country))
xScale.domain([0, d3.max(data, d => d.growth)])
//add X & Y axes and append the bars to Y axis
var xAxis = svg.append("g")
.attr("class", xAxis)
.attr("transform", "translate(" + 0 + "," + height + ")")
.call(d3.axisBottom(xScale))
var yAxis = svg.append("g")
.attr("class", yAxis)
.call(d3.axisLeft(yScale))
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("stroke", "transparent")
.attr("stroke-width", 4)
.attr("class", "bar")
.attr("height", yScale.bandwidth())
.attr("x", 0.5)
.attr("y", function(d) {
return yScale(d.country)
})
.attr("width", 0)
.transition()
.duration(3800)
.delay((d, i) => (i + 1) * 200)
.ease(d3.easeElastic)
.attr("width", function(d) {
return xScale(d.growth)
})
.style("fill", "#00338D")
.on('end', function() {
d3.select(this)
.on("mouseover", function() {
d3.select(this)
.transition().duration(600)
.attr("stroke", "#6D2077")
.attr("stroke-width", 3)
.style("fill", "#6D2077")
d3.selectAll(".textCircle")
.transition().duration(600)
.attr("r", yScale.bandwidth() / 1.9)
.attr("stroke", "#6D2077")
.attr("stroke-width", 1)
})
.on("mouseout", function() {
d3.select(this)
.transition()
.duration(600)
.attr("stroke", "transparent")
.attr("stroke-width", 0)
.style("fill", "#00338D")
d3.selectAll(".textCircle")
.transition().duration(600)
.attr("r", yScale.bandwidth() / 2)
.attr("stroke", "transparent")
})
})
var newG = svg.append("g")
newG.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("class", "textCircle")
.attr("cx", d => xScale(d.growth))
.attr("cy", d => yScale(d.country) + yScale.bandwidth() / 2)
.attr("r", 0)
.transition()
.duration(1200)
.delay((d, i) => (i + 1) * 450)
.attr("r", yScale.bandwidth() / 2)
.attr("opacity", 1)
.style("fill", "#0091DA")
.attr("stroke", "transparent")
}
draw();
$(window).resize(function() {
$("body").empty();
draw();
});
html{
height: 98%;
margin: 0;
padding: 0;
}
body{
min-height: 98%;
margin: 0;
padding: 0;
}
svg{
text-rendering: geometricPrecision;
shape-rendering:geometricPrecision;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- D3JS chart: Hovering on some transitioning elements while others are still transitioning too stops the execution of the chart animation
- D3js : mouseover of one element change opacity of several others elements
- d3/dc.js - How to create a stacked bar chart while telling crossfilter to treat elements in an array as separate records?
- Why selectAll("element") is important in d3js while generating a chart
- d3js - line chart `circle` placement to the lines are not properly sets
- How to add svg rectangles as background behind axis tick labels in d3js if ticks are inside chart
- Issue scalling width of svg elements while updating D3 bar chart
- D3 remove old elements from time scale while transitioning
- d3js error while drawing the chart
- text labels are wrong in grouped bar chart d3js
- Y axis some text cut of in d3js bar chart
- Add text on top of bar in d3js chart -- no <text> elements added
- d3js - Stacked Bar Chart in Horizontal Axis - How do I set x so the bars are seperated?
- If condition never met and hence my bars in a simple bar chart are always blue, while I want them to be red for values below a certain number
- cant access like `d3.svg(), d3.scale()` and some others properties in d3js with angular2
- Painless method to zoom&pan so that all elements are within drawing area - d3js
- Using d3js to make a candlestick or ohlc chart
- D3js - change Vertical bar chart to Horizontal bar chart
- D3js - Creating and easily updating a multi-line chart
- Small Box to display where we are hovering in force layout (D3.js)
- d3js line chart error - drawing weird area
- Problems while reproducing Sankey chart example with d3_sankey
- d3js timeline chart - with one rectangle block for a scrub
- issue while using d3 brush in a line+bar chart (zoom in line+bar chart)
- d3.js nvd3 date on x axis: only some dates are show
- NVD3 Line Chart X Axis Ticks Are Missing
- How to avoid the overlapping of text elements on the TreeMap when child elements are opened in D3.js?
- d3js Redistributing labels around a pie chart
- Why are programmatically inserted SVG <tspan> elements not drawn, except with d3.js?
- Elements appended to SVG with D3 are not appearing
More Query from same tag
- d3 svg path attr d assignment fails in Jupyter Notebook
- Cannot translate initial d3.zoomIdentity without jerkiness on zoom
- HTML5 FileReader output to D3.js chart
- change Axis domain/range doesn't work in nvds
- d3js v5 Normalized Stacked Bar Chart
- d3.js - How to add url link in for collapsible indented tree
- Responsive line label for d3js piechart
- How to add an html form in D3?
- Adding nvd to map in systemjs
- d3.js, collapsible tree - how to connect different parents with same child
- semantic zooming of the force directed graph in d3
- Use RxJS for D3 selection events
- How to make SVG responsive in D3 with viewbox?
- D3.js Get Histogram / Bar column width
- Using objects to simplify code in d3.js
- Different Image Sizes in Force-Directed Images and Labels Layout in d3js
- d3 map with checkbox filtering
- d3/nvd3: how can I set the size of ticks on x-axis
- D3.js: Code not working
- d3 issue with getting data out of a table created from a variable (works in D3.js v3.5.5 but not in v4.2.2)
- Issues with Javascript closures and scope
- Updating graph with new data
- D3: incorporate two datasets in bin - histogram layout
- Nvd3 charts not getting updated using dropdown button
- D3 Geo map zoom is not working correctly (Canvas)
- D3 tooltip that is also a a D3 viz
- Drag multiple items inside another item in d3
- What is the difference between D3.js and Cytoscape.js?
- Javscript Plotly error: 'calling plotly as if redrawing but this container doesn't have a plot yet"
- Simplest D3.js example not working