score:3
As Matt Walters said, you can change the text colour and add underlining with CSS, including adding hover effects.
However, I would highly recommend that you implement your links as actual <a>
element hyperlinks, which are perfectly acceptable in SVG. I've tested it, and you can either wrap the text element in a link, or put the link inside the text element, but surrounding the actual text content. The only complication is that you have to use xlink:href
for the url attribute, instead of simply href
.
Here's a force-directed graph I created for someone else's debugging, adapted to turn all the text labels into links:
http://codepen.io/AmeliaBR/pen/AoFHg
Key code in the update function:
hypertext = hypertext.data(force.nodes());
// hypertext refers to a selection of text elements, joined to the data
// Add labels for new nodes:
hypertext.enter().append("text") //add the text element
//add any unchanging attributes of the <text>:
.attr("text-anchor", "middle")
//add a link element within each text element
.append("a")
//add unchanging attributes of the <a> link
.attr("target", "_blank") //set the link to open in an unnamed tab
.attr("xlink:show", "new");
//show="new" is an XML way to tell the link to open in a new tab/window.
//Either attribute *should* work on its own, but best use both to be safe.
// At this point, each new <text> element contains an <a> element, but the
// variable hypertext still refers to the text elements.
// Remove any outgoing/old text elements for non-existent nodes:
hypertext.exit().remove();
// Compute data-based attributes for entering and updating texts:
hypertext.attr("x", 8) //attributes of the <text> elements
.attr("y", "0.3em")
//Select the existing <a> element within each <text>
.select("a")
//Update the link attributes. Note that the <a> element was never given
//its own data, so it inherits the data from the parent <text> element.
.attr("xlink:href", function (d) {
return "http://example.com/" + d.name; //create url from data
})
//Set the text within the link, which in this case is the only text
//within the text element.
.text(function (d) {
return d.name; //link text content
});
Using the link element created all the necessary functionality, but it didn't add the default HTML link styles. For that, there's CSS:
text a {
fill: navy;
}
text a:visited {
fill:darkpurple;
}
text a:hover, text a:active {
text-decoration: underline;
fill:darkred;
}
score:1
I think you should be able to do all of this in css. Thats part of the beauty of d3.js
Without seeing some code it'll be hard to say exactly what you need, but Ill bet you could do something like this for the underlining and something similar for the other effects you have.
Can you post some code, so we can help you further?
Source: stackoverflow.com
Related Query
- How to underline text within a node's label in d3 force based layouts?
- How to have a text label on links in d3 force directed graphs
- How to layer D3 Force Simulation nodes based on element and not node order?
- How do I create a custom force that constrains nodes to a specific area within an SVG?
- Add text label to d3 node in Force directed Graph and resize on hover
- Labels / text on the nodes of a D3 force directed graph
- How can I dynamically resize an SVG rect based on text width?
- Add text label to d3 node in Force layout
- How do I change nodes to be rectangles instead of circles in a d3 force layout?
- d3.js: How to remove nodes when link-data updates in a force layout
- How to display a text when mouseover a node in D3 force layout
- How to place text on the circle when using D3.js force layout?
- How to change the distance between nodes in a force layout?
- D3 force layout text label overlapping
- How do I hide the text labels in d3 when the nodes are too small?
- Circle packing within force graph nodes in D3?
- How to check d3 js force graph for nodes with no links and remove them?
- How to align text vertically on a node within a Sankey diagram (D3.JS)?
- D3: How to create slow transition of circle radii for nodes in force directed graphs?
- d3js (v4) canvas force layout with text on nodes
- D3.js how do I arrange nodes of a force layout to be on a circle
- How to append text in d3 force layout?
- How to add text to Cluster Force Layout bubbles
- Optimize d3 force directed layout, via charge/gravity properties, based on number of nodes
- How to add text in the center of node in force directed graph?
- d3js How to gather nodes by cluster un force layout?
- Force layout inside force layout: How to drag inner nodes
- How to use D3 force layout with existing SVG elements as nodes
- How to attach nodes and links to simulation force in d3js version 4?
- d3js v4: How can I apply force to nodes onclick and make it look like a tween?
More Query from same tag
- Setting axis' style without CSS
- D3 TopoJSON rendering artifact
- How to invert direction of nodes in diagram in d3.js
- How to set domain to avoid overlapping dots and axis in d3 plot?
- Correctly filling augmented svg path created by D3
- D3 animation in winjs windows app
- "Pinning" nodes in a D3 force-directed graph
- Find a point on an SVG path
- Bold the text partially in a text box which popos up on mouseover-D3
- How to use call function?
- D3JS or SNAPSVG drag and drop limited to loaded svg shape area
- The XY coordinates in a d3 drag event are inverting
- What is `svg.call(zoom.event)` for?
- d3 svg attrtween each by each
- Translating EmberJS D3-Charts to AngularJS
- D3js-Topojson : how to move from pixelized to Bézier curves?
- SVG doesn't render on screen, console log shows "false" suggesting promise was rejected
- how to manipulate d3js tree layout
- Unrecognised Token <
- d3 vertical bar chart
- Dynamically Centering Filtered Node within SVG Element in D3
- How do I use a Java JSONArray with D3 and Dimple, passed using Spring MVC? I'm getting a 404 error
- Force directed graph drawn out of the allocated SVG size
- Changing the angle of a D3 doughnut chart to 180
- Can't load TSV when using D3.JS
- jquery .ready() equivalent in d3js?
- C3.js chart title overlapped if I change the size of title
- Why d3.transition() has no effect where theUpdateSelection.transition() works?
- Why text goes out of the box while using D3 Word Cloud example by Jason Davies?
- How to improve my simple nested data binding in d3?