Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Bram Huisman
Bram Huisman
7,387 Points

D3 question, please help.. school project stressss

Hi Guys, i'm making a d3 visualization for a school project.. but i'm stuck! Everything works.. all the dots are on the right place but when i trying to filter stuff it breaks..

I've a data_sheet that looks like this (it's much longer than the picture) http://postimg.org/image/68jzjj2br/

I wrote this code to show all the data in a d3 visualization

var all_data;
    var display_data;
    var svg;
    var positief = "positief";
    var neutraal = "neutraal";
    var negatief = "negatief";

    d3.csv("data/oh_data.csv", function(dataset){
      all_data = dataset;
      display_data = all_data;
      drawGraphic();
    });

    function drawGraphic(){
      svg = d3.select("#svg-3");
      svg.selectAll("circle")
        .data(display_data)
        .enter()
        .append("circle")
        .attr("cy", function(d){
          return (d["y"]);
        })
        .attr("cx", function(d){
          return (d["x"]);
        })

        svg.selectAll("circle")
          .data(display_data)
          .transition()
          .duration(1000)
          .attr("r", 5)

        .style("fill",function(d){
          if(d["waarde"] == positief){
            return "green";
          }else if(d["waarde"] == neutraal){
            return "yellow";
          }else{
            return "red";
          }
        })

    }//Einde drawGraphic

This works and looks like this http://postimg.org/image/ofxpeoiet/

But now the problem comes.. i want to filter Let's say i only want to see the circles of Romeo I wrote this code

   function filterRomeo(){
      clearGraphic();

      display_data = new Array();
      for(i=0; i<all_data.length; i++){
        if(all_data[i]["persoon"] == "Romeo"){
          display_data.push(all_data[i]);
        }
      }
      setTimeout("drawGraphic()",500);
    }

    function clearGraphic(){
      svg.selectAll("circle")
        .transition()
        .duration("1000")
        .attr("r", 0);

    }

What happens looks like this http://postimg.org/image/3vkvuuydj

It gets everything of Romeo but places it at te wrong x and y coordinates. For example.. i finds the first Romeo Match at line 8 but places it at the coordinates of line 2.. what am i doing wrong?

Peter Smith
Peter Smith
12,347 Points

Hi, without having your csv and the full code available I can only point you to this medium article on exit, enter and update.

the core concept you're trying to accomplish with your cleargraphic() function is to exit() the datum you no longer want.

perhaps you could share your code on GitHub than I could be more helpful?