javascript - I have an svg element that i want to alter the color of with a button click. Involves, json, and csv -


what code create map projection using g.selectall("path)... assigns small circles perimeters of associated countries. want able create buttons in html file when pressed "select"/"deselect" or color/uncolor circles and/or associated countries. want able choose countries , circles colored. know how create buttons , reference them , such, calling functions them. hangup not sure how svg or g element redraw or reupdate canvas. if debug, shows call stack not going g.selectall more once method executes multiple times. first time writing js forgive inconsistencies or rough code outline.thanks!

var svg = d3.select("#container").append("svg")         .attr("width", width)         .attr("height", height)         .append("g");    //create secondary layer canvas svg  var g = svg.append("g");  function buildmap() {        d3.json("countries.geo.json", function(json) {   d3.csv("map_of_the_gods.csv", function(data) {     // go through each element of csv      (var = 0; < data.length; i++) {         // data csv element         var csvname = data[i].name;         var csvculture = data[i].culture;         var csvlocation = data[i].location;         var csvgender = data[i].gender;         var csvspecies  = data[i].species;         var csvtype = data[i].type;         var csvwikilink = data[i].linkwik;         var csvgclink = data[i].linkgc;         var csvpicture = data[i].picture;           // go through each element of json looking country         //      match country of csv.         (var j = 0; j < json.features.length; j++) {             var jsoncountry = json.features[j].properties.name;             //console.log(jsoncountry);             //console.log(j);             if (csvlocation == jsoncountry) {                 // assign color retrieved csv                 //      matching json element.                 json.features[j].properties.name = csvname;                 json.features[j].properties.culture = csvculture;                 json.features[j].properties.location = csvlocation;                 json.features[j].properties.gender = csvgender;                 json.features[j].properties.species = csvspecies;                 json.features[j].properties.type = csvtype;                 json.features[j].properties.wikilink = csvwikilink;                 json.features[j].properties.picture = csvpicture;                 json.features[j].properties.initialcolor = 1;                   break;             }//if(csvlocation == jsoncountry         }//for loop     }//outer loop      g.selectall("path")      .data(json.features)      .enter()      .append("path")      .attr("d", path)      .style("fill", function(d) {         var country = d.properties.location;         var colorchange = d.properties.initialcolor;          if( country === "mexico") {             console.log("yo");                 return "#410d66";}//purple                    if(country === "greece") {                return "#000072";}//aqua         if(country === "japan") {                return "#c90e0e";}//red         if(country === "united kingdom") {                return "#194719";}         if(country === "ireland") {                return "#194719";}//green                 if(country === "nigeria") {                return "#ff9900";}//light brown         if(country === "peru") {                return "#85ad33";}//green yellow         if(country === "egypt") {                return "#ff5050";}//salmony         if(country === "denmark") {                return "#75a3a3";}//bluegrey         if(country === "finland") {                return "#75a3a3";}         if(country === "norway") {                return "#75a3a3";}         if(country === "sweden") {                return "#75a3a3";}         else {             return "grey";}     });//this stlye attribute path     // g.selectall("circle").remove();  //we create of circles , gods     g.selectall("circle")      .data(data)      .enter()      .append("circle")      .attr("cx", function(d) {             return projection([d.lon,d.lat])[0];})       .attr("cy", function(d) {             return projection([d.lon, d.lat])[1];})       .attr("r", 3)       .style("fill", function(d) {                 var typeofgod = d.type;                  if(typeofgod === "storm" ) {                     return "yellow";}                 else if(typeofgod ==="sky"){                     return "#87cefa";}                 else if(typeofgod === "death" || typeofgod ==="chaos") {                     return "black";}                 else if(typeofgod === "agriculture") {                     return "#006300";}                 else if(typeofgod === "motherhood") {                     return "#99ff99";}                 else if(typeofgod === "war" ) {                     return "#9e0e0e";}                 else if(typeofgod === "moon") {                     return "white";}                 else if(typeofgod === "sun") {                     return "orange";}                 else if(typeofgod === "love") {                     return "#ff99ff";}                 else if(typeofgod ===  "wisdom") {                     return "#9900ff";}                 else if(typeofgod === "sea") {                     return "blue";}                  else{                     return "grey";}              })//these brackets match style circles , end selectall()       .on('mouseover', function(d) {         tooltip.transition()                .duration(400)                .style("opacity", 0);         tooltip.transition()                .duration(100)                .style("fill", "black")                .style("opacity", ".9");         tooltip.html("<img src = " + d.picture+">" + "<br>" + "name: " + d.name + "<br>"                      + "type: " + d.type + "<br>"                      + "culture: " + d.culture + "<br>" + "region: " + d.location + "<br>"                     + "gender: " + d.gender + "<br>" + "species: " + d.species + "<br>" +                     '<a href = "' + d.linkwik + '">' + "wikipedia" + "</a>" + "<br>"                     + '<a href = "' + d.linkgc + '">' +"godchecker" + "</a>")                .style("left", (d3.event.pagex ) + "px")                .style("top", (d3.event.pagey) + "px")});        });//these bracets d3.csv line above });//these brackets d3.json line //this bracket function countriesselect 


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -