Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active October 21, 2018 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetricity/0f86fa4d4aa80fb237f9c5818f1e5395 to your computer and use it in GitHub Desktop.
Save codetricity/0f86fa4d4aa80fb237f9c5818f1e5395 to your computer and use it in GitHub Desktop.
d3 BTS part 3 - Show Data

Show Data

image

Demo

https://codetricity.github.io/d3-bts/profile.html

Demo 2 with Transitions

https://codetricity.github.io/d3-bts/transition.html

Code Example

https://github.com/codetricity/d3-bts/blob/master/js/profile.js

Create New Function to Show Text Data

Call the new function showData()

...
   showData(memberName);
});

function showData(memberName) {

}

Read in Data from File

Put around the buttons.on('change'... event handler.

d3.csv('data/bts-profiles.csv').then((data) => {

  buttons.on('change', function(d) {
    let memberName = this.value;
    showImage(memberName);
    showData(memberName, data);
  });
});

Verify that You can Get Data

function showData(memberName, data) {
  console.log(data);
}

Add Group in SVG Element for Profile Text Data

const profileListing = svg.append('g');

Put this above the code to read in the data from file. Put it below the code you set the svg constant (close to the top of the code).

Remove Previous Text Elements

Use this simpler method of removing all text elements.

function showData(memberName, data) {
  profileListing.selectAll('text').remove();

Go Through Array of Objects

Go through array and search for memberName, which was taken from the button value.

function showData(memberName, data) {
 profileListing.selectAll('text').remove();
 data.forEach((element) => {
     if (element.name == memberName) {

Add Text Data to Screen

function showData(memberName, data) {
  profileListing.selectAll('text').remove();
  data.forEach((element) => {
    if (element.name == memberName) {
      var yPosition = 80;
      for (var memberInfo in element) {
        let memberValue = element[memberInfo];
        profileListing
          .append('text')
          .attr('x', '50')
          .attr('y', yPosition)
          .text(`${memberInfo}:  ${memberValue}`);
        yPosition += 50;
      }
    }
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment