score:0
here is a basic start to what you're trying to achieve. you need to start by adding a click function to your circle.
circle.on('click', function(d){
//grabs all the circles from your array
var allcircles = circle[0]
//used to cycle through to see if any of them are classed with the 'selected' class'
for (var i=0; i<allcircles.length; i++){
d3.select(allcircles[i]).classed('selected', false)
d3.select(allcircles[i]).attr('style', 'fill: rgb(0,128,0);')
}
//set the circle you clicked on to have no inline style and classed to 'selected'
d3.select(this).attr('style', '')
d3.select(this).classed('selected', true)
})
i've updated the fiddle so you can see. you can edit the css however you need. currently, it just fills the circle with a blue fill, but you can use css similar to the example you posted.
score:1
when you click the bubble, the g-selected
class is added to the nodes. that changes the css that applies from
.g-node .g-democrat {
fill: #c5d7ea;
}
.g-node .g-republican {
fill: #f9caca;
}
to
.g-node.g-selected .g-democrat {
fill: #99c0e5;
stroke: #6081a3;
stroke-width: 1.5px;
}
.g-node.g-selected .g-republican {
fill: #fda4a7;
stroke: #af5e61;
stroke-width: 1.5px;
}
adding a class to a clicked element is pretty straightforward. the class is added using the selection.classed
method.
node.classed("g-selected", function(d) { return d === topic; });
if you are looking at the source, look at the updateactivetopic
function.
the code in your fiddle is quite a bit simpler than the example you linked. i would change the part where you create the circle elements so that it uses css, rather than inline style, i.e.
circle {
fill: green;
}
instead of
.style("fill", function (d, i) {
return "green";
})
now, you'll add a click handler to the circles:
circle.on("click", function(d) {
// remove selected class from all circles that have it
circle.classed('selected', false);
//add the selected class to the element that was clicked
d3.select(this).classed('selected', true)
});
and a style to highlight the circle when it is selected
circle.selected {
fill: blue;
stroke: black;
stroke-width: 1.5px;
}
see the fiddle for the complete example.
Source: stackoverflow.com
Related Query
- Bubble highlight feature in d3js bubble chart
- Stacked D3JS Bubble Chart
- Using External json file for creating bubble chart using d3js
- d3js: Apply transition on d3js bubble chart
- Highlight pie chart on hover d3js
- Highlight closest point d3js line and dot chart
- d3js donut chart - highlight first data
- d3js v4 bubble chart - getting force/gravity effects back
- How to distribute horizontally a bubble chart in d3js v4
- Bubble Chart - d3js & json (data with 2 columns)
- how to add title to d3js bubble chart with force layout
- 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
- D3 bubble chart / pack layout - How to make bubbles radiate out from the largest bubbles to the smallest?
- d3js line chart error - drawing weird area
- d3js timeline chart - with one rectangle block for a scrub
- d3js Redistributing labels around a pie chart
- D3 Bubble Chart
- How do I add labels to d3.js force bubble chart
- Build D3js Area Chart with negative values
- Add border-radius property to D3js Donut Chart
- How to Update bubble chart in d3.js?
- D3js mulitline chart mouseOver
- in d3js bar chart i want to have fixed bar width
- d3js Create legend for bar chart
- Creating a legend for a bubble chart using D3
- Interactive sorting of grouped bar chart in D3js
- Different states in D3/Coffee Bubble chart
- D3 Bubble Chart 'bubble.nodes not a function'
More Query from same tag
- D3.js - Use newline character in rendered text
- NVD3 (D3 JS) how to remove y2 axis
- Can't change fill of d3.js rectangle
- D3.js tree diagramme External Data import debug
- How to draw spiral bubble charts with d3.js
- how to simulate simple orbiting behaviour in d3
- Explaining Mike Bostock's d3.js dragmove function
- appending multiple polygons on a single image using d3
- d3 csv returns object without rows just functions
- html doesnt render in partial view
- implementing a scatterplot in d3
- d3.js - stacked graphs that are both zoomed in and out/scrolled when using scrollwheel/drag on either graph
- Angularjs d3 custom directive with filter on data
- Cannot get D3 y axis and columns to align properly along x axis
- d3.js on click zoom function not working
- Connect two circles with mouse click event
- How to remove event listener of an element when I remove the corresponding element in the midst of the event being triggered in D3?
- d3 js bar chart not working in mozilla firefox
- D3 using classed() to add and remove class with checkbox
- How to use call function?
- trying to generate bar chart in d3 using object-oriented approach
- how to make xAxis hourly ticks(like 0,1,2,...,23) in StackedAreChart use nvd3.js?
- d3 ghosting zoom pan ie
- Placing two elements - charts - under each other on the page
- How to make sure that all data are pushed to the array before resolving promise?
- How pass json from angularjs to d3?
- d3 choropleth map - proper domain/range config
- How to call a function (requiring args) with string name - Clojurescript | d3
- d3.js force directed graph sphere
- Change style on points, d3