Skip to main content
100%

D3 Links Example

✓ Published0🌍 Public
CCurran Kelleher
Last edited May 28, 2015
Created on May 28, 2015

This example shows how to link SVG visual marks to external web pages by constructing URLs from data properties. Three rectangles each link to a different Wikipedia article, with the hyperlink target generated dynamically from an array of strings. The code uses D3.js (version 3.5.5) to bind data to anchor elements via `selectAll("a").data(data).enter()`, then sets the `xlink:href` attribute using a function that concatenates the data value with the Wikipedia base URL. Each rectangle is positioned with the `attr` method, and the rendering is SVG.

AI-generated description

This example shows how to make several SVG elements link to different things by constructing URLs from data properties. Each square links to a different Wikipedia page, and the link URL is generated dynamically from the data array.

This has the expected behavior of normal links when you open it in a new window. Clicking the rectangle doesn't work correctly in the bl.ocks viewer, because it is in an iFrame (open the debugging console to see the iFrame-related error), but this technique of adding links to visual marks should work fine in your own D3 projects.

This is addressing the thread Hyperlinks - error "undefined"/ NaN in the D3 Google Group.

Similar vizzes