D3 does not show me updated data

0

I'm doing a graph with d3, I have two checkboxes, each one refers to a series of data, what I want is when I click on one and it's activated, it shows the data. I already did this method for jqplot, but when it came to updating the data in jqplot it was as easy as using redraw . Now in d3.js I do not understand very well how to do it. I leave the method:

function mostrarDatos(){
                nowData=[]
                if($("#eae").prop('checked') == true)
                {
                    nowData.push(eaepunt1[coundata])
                }

                if($("#arab").prop('checked') == true)
                {
                    nowData.push(arabpunt1[coundata])
                }
                console.log(nowData)
                svg.selectAll("circle")
                .data(nowData)
                .attr("cx", function(d) {
                    return xScale(d[0]);
                 })
                .attr("cy", function(d) {
                    return yScale(d[1]);
                 })
            }

Finally add some information, nowData is the Data Array I want to show. eaepunt1 is the Data Array that contains x e y of the graph, this refers to the first checkbox, arabpunt1 is the Data Array of the second checkbox, it also contains x e y for the graphic.

The data at the beginning I paint them in this way:

    var datos = svg.selectAll("circle")
       .data(nowData)
       .enter()
       .append("circle")
       .attr("cx", function(d) {
            return xScale(d[0]);
       })
       .attr("cy", function(d) {
            return yScale(d[1]);
       })
       .attr("r", function(d) {
            return 5;
       })
       ;

I think I have left all the primary information here, if you have any doubt about something, share it with me and I will edit the question.

EDITED

I have managed to show the data by changing the mostrarDatos() method in this way:

    function mostrarDatos(){
        nowData=[]
        if($("#eae").prop('checked') == true)               
        {   
            nowData.push(eaepunt1[coundata])                                    
        }               

        if($("#arab").prop('checked') == true)
        {
            nowData.push(arabpunt1[coundata])                                                               
        }                                           
        console.log(nowData)                            
        svg.selectAll("circle")
           .data(nowData)
           .enter()            
           .append("circle")                           
           .attr("cx", function(d) {
                return xScale(d[0]);
           })
           .attr("cy", function(d) {
                return yScale(d[1]);
           })
           .attr("r", function(d) {
                return 5;
           })              
           ;
    }

But when I uncheck the check the data is still displayed

    
asked by Lombarda Arda 20.03.2017 в 10:30
source

2 answers

0

In the end I managed to do it. It was necessary to add an exit and a remove at the beginning of the method. I do not understand very well because it works:

function mostrarDatos(){
                nowData=[]
                svg.selectAll("circle")
                    .data(nowData)
                    .exit()
                    .remove()
                if($("#eae").prop('checked') == true)               
                {   
                    nowData.push(eaepunt1[coundata])                                    
                    svg.selectAll("circle")

                }               

                if($("#arab").prop('checked') == true)
                {
                    nowData.push(arabpunt1[coundata])                                                               

                }                                           
                console.log(nowData)

                svg.selectAll("circle")
                   .data(nowData)
                   .enter()            
                   .append("circle")                           
                   .attr("cx", function(d) {
                        return xScale(d[0]);
                   })
                   .attr("cy", function(d) {
                        return yScale(d[1]);
                   })
                   .attr("r", function(d) {
                        return 5;
                   })              
                   ;
            }
    
answered by 20.03.2017 в 11:33
0

Hi, thanks for your comments, hear if you have worked with JqPlot, Sabras because when I convert the graphic to the image it distorts a bit, it looks bigger and it does not come out completely, I hope you could help me, thank you very much

Greetings

    
answered by 27.09.2017 в 21:15