Search code examples
d3.jstooltipdata-visualization

How to add a tooltip that shows a bar chart onto a d3.js map


I have a map of the US with markers for stores in each state and currently when you hover over each marker, the name and address of that store shows up. I used a json file (us.json) to get the states coordinates and boundaries. The second json file (newstorelocations.json) contains information about each store and it's location which I used to display the markers. I used a tooltip for this.

What I currently have is at: http://bl.ocks.org/binishbaig/3969ec74b485d1021034

gist: https://gist.github.com/binishbaig/3969ec74b485d1021034

I have a third json file (newstorespend.json) that contains products and amount spent for each store. The variable StoreDescription exists in both the second and third json file. Any clue how can I make a simple vertical bar chart displaying amount and product for each store location when you hover over each marker? I am totally new to d3.js so I am guessing I would have to write a separate function creating the chart in a separate file, and then pass it into that variable d in the mouseover function but that's purely a guess. I did make a bar chart out of the data from the third file but how do I make individual bar charts for each store and show it when the mouse hovers over the corresponding store marker.

I would appreciate any help. Thanks in advance!


Solution

  • There's basically no difference between adding a chart to a tooltip vs doing it normally, you'll just need to grab the data and filter it so that it only applies to the data point that you're hovering over and then create/update the chart based on that data at the correct DOM element.

    I've forked your gist and done just that. I just used the data that you had for Anchorage in your newstorespend.json file in your gist. Hover over the data point for the store in Anchorage to see what shows up.

    You can take a look at http://bl.ocks.org/benlyall/37e757a1e6922dccb077

    The onHover and offHover functions do all the work that you're probably interested in.

    Note: this is only one way to do it, and possibly not even the best.