liveBook · Manning

Map

As explained in D3.js in Action, Third Edition

A JavaScript Map is a key-value collection that preserves associations between keys and values and supports lookup by key.

A JavaScript Map stores associations between keys and values so a value can be retrieved by its key. In a Canvas interaction layer, a color key identifies the node drawn with that color. section 15.4

Use a Map to connect hidden Canvas colors with visible visualization nodes:

  • Create a Map for node colors. section 15.4
  • Generate a color and check whether it already exists with get(). section 15.4
  • Store the color and node together with set(). section 15.4
  • Retrieve the node after getImageData() returns the color under the pointer. section 15.4
Listing 15.15 Giving a unique color to each node (Paintings.svelte)
<script>
  ...

  let canvasElement;
  let context;
  let hiddenCanvasElement; 
  let hiddenContext;       
  onMount(() => {
    ...
  });

  const nodesColors = new Map();  #1

  const generateColor = () => {                              #2
    let colorArray = [];                                    
    for (let i = 0; i < 3; i++) {                           
      const randomNumber = Math.floor(Math.random()         
        * 255);                                             
      colorArray.push(randomNumber);                        
    }                                                       
    return colorArray;                                      
  };                                                        

  const addNodeColor = (node) => {
    let isNewGeneratedColor = false;
    while (!isNewGeneratedColor) {
      const colorArray = generateColor();               #3
      const colorRGB = `rgb(${colorArray.join(",")})`; 
      if (!nodesColors.get(colorRGB)) {   #4
        nodesColors.set(colorRGB, node); 
        isNewGeneratedColor = true;      
        return colorRGB;                
      }                                  
    }
  };

  const handleSimulationEnd = () => {
    nodes.forEach((node) => {
      ...

      const color = addNodeColor(node);   #5
      hiddenContext.fillStyle = color;   
      hiddenContext.strokeStyle = color; 

      hiddenContext.beginPath();                                   #6
      hiddenContext.arc(                                          
        node.x,                                                  
        node.y,                                                  
        node.area_cm2                                            
          ? Math.sqrt(paintingAreaScale(node.area_cm2)          
              / Math.PI)                                         
          : paintingDefaultRadius,                               
        0,                                                       
        2 * Math.PI                                               
      );                                                           #7
      hiddenContext.fill();                                       
      hiddenContext.stroke();                                     
    });
  };

  ...
</script>

Inspect the complete color-to-node Map implementation, including collision checking and insertion. section 15.4

How D3.js in Action, Third Edition covers it

How deep it goes
Explains initialization, uniqueness checking, insertion, and retrieval, but does not survey the broader Map API.
Examples
Demonstrates a nodesColors Map that associates generated RGB colors with circles representing paintings.
What it assumes
Assumes JavaScript, Canvas elements, mouse events, and RGB color handling.
What you can do after
Apply Map with Canvas getImageData() to identify hovered visualization nodes and display their metadata.
How it connects
Connects Map with Canvas 2D API, getImageData(), mouse events, hidden Canvas layers, and data visualization.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.