Angular D3 TopoJSON: mouseover tooltip coordinates wrong
NickName:OJT Ask DateTime:2022-02-23T14:16:03

Angular D3 TopoJSON: mouseover tooltip coordinates wrong

I am trying to implement a div tooltip on mouseover for counties in a TopoJSON map of the United States, using D3 on a page built in Angular.

I have tried every combination of the x/y coordinates available in the D3 mouseover event, as well as some combinations suggested online (D3.pointer, Element.getBoundingClientRect, etc.), but no matter what I do the tooltip seems to fall at incorrect coordinates, offset from my mouse and not necessarily by the same degree at each location on the svg.

Is there a proper way to do this? Examples of code are below. The relevant portion is within this.counties.on("mouseover", ...).

HTML

<div class="row g-0">
  <div class="container" id="mapContainer">
    <svg id="map"></svg>
    <div id="tooltip">TESTING</div>
  </div>
</div>

CSS

#mapContainer {
  position: relative;
}
#tooltip {
  position: absolute;
}

TypeScript

    this.svg = d3.select("#map")
      .attr("preserveAspectRatio", "xMidYMid meet")
      .attr("viewBox", "0 0 960 600")
      .attr("height", "100%")
      .attr("width", "100%")

    this.g = this.svg.append("g")
      .attr("id", "g")

    this.zoom = d3.zoom()
      .scaleExtent([1, 8])
      .translateExtent([[0, 0], [960, 600]])
      .on("zoom", (event, d) => {
        let {transform} = event
        this.g.attr("transform", transform)
        this.g.attr("stroke-width", 1 / transform.k)
      })

    this.counties = this.g.append("g")
      .attr("class", "county")
      .attr("fill", "#DFDFDF")
      .attr("stroke", "#FFFFFF")
      .attr("stroke-linejoin", "round")
      .attr("stroke-width", "0.25")
      .selectAll('path')
      .data(topojson.feature(this.topography, this.topography["objects"]["counties"])["features"])
      .join("path")
      .attr("id", function(d) {return d["id"]})
      .attr('d', this.path)
      .attr("fill", "#DFDFDF")
      .on("mousemove", (event, d) => {

        d3.select("#tooltip")
          .style("left", `${d3.pointer(event)[0]}px`)
          .style("top", `${d3.pointer(event)[1]}px`)

      })

      // PER DERISTNOCHDA'S SUGGESTION
      .on("mousemove", (event, d) => {

        d3.select("#tooltip")
          .style("left", `${event.pageY}px`)
          .style("top", `${event.pageX}px`)

      })

A few screenshots of results, e.g., when the cursor is in Maine in the top-right:

enter image description here

And when the cursor is in Arizona in the bottom-left:

enter image description here

Copyright Notice:Content Author:「OJT」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71232139/angular-d3-topojson-mouseover-tooltip-coordinates-wrong

More about “Angular D3 TopoJSON: mouseover tooltip coordinates wrong” related questions

Angular D3 TopoJSON: mouseover tooltip coordinates wrong

I am trying to implement a div tooltip on mouseover for counties in a TopoJSON map of the United States, using D3 on a page built in Angular. I have tried every combination of the x/y coordinates

Show Detail

Using D3 to retrieve a TopoJSON U.S. county from coordinates

I have a set of coordinates retrieved as: navigator.geolocation.getCurrentPosition(function(position) { let coordinates: [number, number] = [position.coords.longitude, position.coords.latitude] }...

Show Detail

TopoJSON arcs is undefined, coordinates is undefined, o is undefined

I am very new to d3 and topojson and I am trying to render a map of the UK to overlay some data onto later. I have taken a topojson file from an online repository which is clearly fine since GitHub...

Show Detail

How to use topoJSON with D3

I'm aiming to use topoJSON in my current D3 project due to its benefits over geoJSON such as greatly reduced file size. However, I've been a bit confused over how to accomplish this. My current tho...

Show Detail

d3 topojson incorrect datatype in webpack/react

Hi I'm trying to render a map with D3 in a react/webpack project. I'm using react-d3-wrap so that I can put d3 code straight into the react project. Additionally, I'm pulling in the data via having

Show Detail

D3 heatmap tooltip bug ~ On mouseover the cursor changes to text cursor icon

Heatmap Cursor Error Heatmap Cursor Correct The first link shows what happens to the cursor on mouseover, this only happens sometimes and not on the whole area of the box. The second link is what s...

Show Detail

d3 Topojson debugging transformations

Are there general d3/topojson debugging hints? I am relatively new to the format and have made a visualization through d3 for the US states. The JavaScript and d3 works for a topojson file I had on...

Show Detail

Tooltip in d3/topojson choropleth map not working

I have a Choropleth map where the tooltip is working for most of it, but the central states are now showing the tooltip...in face, they are not even running the mouseout callback function at all (t...

Show Detail

D3 tooltip - keep mouse on tooltip with html elements

I have implemented d3 tooltip acording to the answer of this question(How can I keep a d3 mouseover open while my mouse is over the tooltip?). (Example link http://bl.ocks.org/larsenmtl/

Show Detail

Add tooltip to D3 Icicle visual (Observable)

I am working with this icicle visual on D3 Observable. I need to add a tooltip, so that it works faster and looks better than native browser version. I tried this: var tooltip = d3.select("bo...

Show Detail