Using the D3 JavaScript Library
for Interactive Graphs and Maps

OPTIONAL CHALLENGE: Interactively Linking Map and Chart

The optional challenge is to create the following:

  1. A webpage that holds both the Overijssel Bar chart and the municipal Proportional Point Symbol map;
  2. If the user moves the mouse over a bar in the graph, the bar colour changes, and the corresponding municipality in the map is also highlighted;
  3. The attributes of the highlighted municipality are shown to the user.

You can see a working example by following this link. Note that this is only one way to achieve the required results, other solutions are possible! Below we show some of the building blocks needed, you should figure the rest out yourself, and test the results in your browser...

  • PLACEMENT: In order to place the graph and map next to each other, you can use different lay-out possibilities. The simplest way of doing that is styling the HTML <div> element we use to hold the map. You can change the CSS style of the mapDiv to include CSS positioning properties. The meaning of the most useful properties is:
    • position: this lets you define positioning absolute or relative to other divs.
    • left & top: the location with respect to the upper left corner of the window (when position: absolute) or with respect to the previous div (when position: relative).
    • width & height: the size of the element (in pixels)
    • overflow: if the content is larger than fits the div, it will not be shown if this is set to hidden. Other settings are visible (will overflow), scroll (will make scrollbars) and auto (let browser decide).
    • border: the border look (width, type and colour). You can also set the fill of the div in a similar way.
  • INTERACTIVITY: This is triggered by an event connected to the appropriate selection. D3 supports interactivity of all its selections, by the use of selection.on(type,listener). This adds an event listener to each element in the current selection, reacting on the specified type of event. The type is a string event type name, such as "click", "mouseover", "mouseout", or "submit". Any DOM event type supported by your browser may be used. The specified listener is invoked in a similar manner as any other operator functions, but the first element passed is now the event (not the current data as in other D3 functions), because the standard javascript events work like that. The data is now added as the second element returned, in such a way:
        selectedObject
            .on("mouseover", function(event, d) {
                ... code run per data instance, e.g. to show information , change bar colour...
            );
    
    The interactivity can of course be improved by having the reverse highlighting also working (mouse in map highlights bar), by having the data displayed, etcetera.

  • SHOWING ATTRIBUTES: There are many ways to achieve this. The simplest might be to have the interactivity (see above) of elements trigger a simple Javascript alert("message"), which will just pop up an alert window with the message string. In our solution we employ a somewhat nicer solution of creating yet another <div>, selecting that using D3, and putting the attributes in as HTML content by using the D3 selection.html("html content") method.

We will make the unscrambled code for the working example available below, after a few days. Happy puzzling...!