BTS Project Lesson #3 — Using D3.js to Show Images

Craig Oda
CodeCakes
Published in
2 min readNov 10, 2018

Change image based on button press. Use D3.js update pattern

Previous Lessons

Learning Resources

Review

In the top viewport, divide program into two logical structures
Show Text Data about Each Member
Show image data

Overview

  • modify code in showImage function
  • create variable imageFile and assign to name of image file on disk based on the value of the button pressed
  • apply D3.js data update pattern

Pattern

Note: we are using the 6 step data update pattern for practice. As there is only one data element in the array you are updating, you can use an easier method in a real-world project

  1. select image assign the result of selectAll('...') to a constant called images
  2. data — chain the data command to the line above. The data is a single element array called holding imageFile. Bind the array to the constant images with .data([...]);
  3. exit — chain to the constant images add .remove() to delete the original image file. Example: images.exit().remove()
  4. update — chain to the constant images. Update is not a keyword command. It is a step in the process where you update the data that has changed. The main data that changed is the location of the image file to display. Example: images.attr(‘xlink:href’, location of file’)
  5. enter chain to the constant images
  6. append — chain append to the line above. The HTML element to append is image Example: .append(‘image’

Image Attributes

  • x: 300
  • y: 50
  • height: 150
  • width: 150
  • xlink:href: location of file
HTML is generated from your JavaScript file. This is not the code to type in.

Add in transitions.

Next Lesson

Lesson #4 — statistic chart setup

--

--

Craig Oda
CodeCakes

open source advocate, writer, Flutter developer, father, husband, fly fisherman, surfer