Skip to content

Instantly share code, notes, and snippets.

@d3noob
Created June 19, 2021 04:40

Revisions

  1. d3noob revised this gist Jun 19, 2021. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    The above example is intended to demonstrate adding a hyperlink to an object (the rectangle) and instructing the text object to be transparent to the pointer so that the link on the rectangle is recognised 'through' the text.

    This is one of the code samples for the update to the book [D3 Tips and Tricks](https://leanpub.com/d3-t-and-t-v6) to version 6 of d3.js.


    forked from <a href='http://bl.ocks.org/d3noob/'>d3noob</a>'s block: <a href='http://bl.ocks.org/d3noob/5e894c1981c355b0b552a9dd10c05131'>Adding links to objects in v7</a>
    This is one of the code samples for the update to the book [D3 Tips and Tricks](https://leanpub.com/d3-t-and-t-v7) to version 7 of d3.js.
  2. d3noob created this gist Jun 19, 2021.
    1 change: 1 addition & 0 deletions .block
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    license: mit
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    The above example is intended to demonstrate adding a hyperlink to an object (the rectangle) and instructing the text object to be transparent to the pointer so that the link on the rectangle is recognised 'through' the text.

    This is one of the code samples for the update to the book [D3 Tips and Tricks](https://leanpub.com/d3-t-and-t-v6) to version 6 of d3.js.


    forked from <a href='http://bl.ocks.org/d3noob/'>d3noob</a>'s block: <a href='http://bl.ocks.org/d3noob/5e894c1981c355b0b552a9dd10c05131'>Adding links to objects in v7</a>
    45 changes: 45 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <!DOCTYPE html>
    <meta charset="utf-8">

    <body>

    <!-- load the d3.js library -->
    <script src="https://d3js.org/d3.v7.min.js"></script>

    <script>

    var width = 449;
    var height = 249;
    var word = "gongoozler";

    var holder = d3.select("body")
    .append("svg")
    .attr("width", width)
    .attr("height", height);

    // draw a rectangle
    holder.append("a")
    .attr("xlink:href", "http://en.wikipedia.org/wiki/"+word)
    .append("rect")
    .attr("x", 100)
    .attr("y", 50)
    .attr("height", 100)
    .attr("width", 200)
    .style("fill", "lightgreen")
    .attr("rx", 10)
    .attr("ry", 10);

    // draw text on the screen
    holder.append("text")
    .attr("x", 200)
    .attr("y", 100)
    .style("fill", "black")
    .style("font-size", "20px")
    .attr("dy", ".35em")
    .attr("text-anchor", "middle")
    .style("pointer-events", "none")
    .text(word);

    </script>

    </body>