Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active September 25, 2018 13:25
Show Gist options
  • Select an option

  • Save codetricity/1503597a58ab7705565b93b873913452 to your computer and use it in GitHub Desktop.

Select an option

Save codetricity/1503597a58ab7705565b93b873913452 to your computer and use it in GitHub Desktop.
Not Finished - Add HTML Form to Chart

Not Finished - Add HTML Form to Chart

Goal

Add form below the SVG viewport by using JavaScript.

Learning Objectives

  • append additional elements to HTML <body>
  • review radio buttons

Create Function createButtons

  1. Create a new function in your main JavaScript file called createButtons.

  2. The function receives the array of cities.

  3. use d3.select to select the body and append a form

  4. create a new variable form and assign it to the the result of d3.select...

     function createButtons(cities){
         var form = d3.select("body").append("form");
     }
    

Call function inside of the d3.csv promise

  1. pass the function the var cities.
  2. run the function below the area you append "svg" to "body"

form

append label and input

form.append("label").append("input");

Imgur

Add attributes for input form

form.append("label").append("input")
    .attr("type", "radio")
    .attr("name", "city_button")
    .attr("value", "honolulu");

Imgur

Put into for loop

Use the array of cities to create buttons for each city

for (var index = 0; index < cities.length; index++){
    form.append("label").append("input")
    .attr("type", "radio")
    .attr("name", "city_button")
    .attr("value", cities[index]);
}

5 buttons

Confirm Buttons Have Values

button HTML

Create Function to Get Value of Button

    function getCityChange(){
        console.log(this.value);
    }

run function in promise

Make sure you run the function after you create the buttons in HTML.

createButtons(cities);

// selecting "input" must be below createButtons
var buttons = d3.selectAll("input");
buttons.on("change", getCityChange);

return city

    function getCityChange(){
        var nameOfCity = this.value;
        return nameOfCity;
    }

Test.

var currentCity = buttons.on("change", getCityChange);

modify drawCircles

  1. comment out for loop in promise
  2. pass currentCity to drawCircle function
  3. drawCircle receives cityName instead of cities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment