简体   繁体   English

D3不会更新数据

[英]D3 doesn't update Data

Im making a graphic with d3. 我用d3制作图形。
I have 2 checkboxes, they reference to 2 different datasets. 我有2个复选框,它们引用了2个不同的数据集。 I need that when I click in one of them that data updates and redraw graphic. 当我点击其中一个数据更新并重绘图形时,我需要它。
I use this method but doesn't work: 我使用这种方法但不起作用:

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]);
                 })
            }

nowData is the array to show. nowData是要显示的数组。 eaepunt1 and arabpunt1 are the datasets of checkbox, they have the x and y to show in the graphic. eaepunt1arabpunt1是复选框的数据集,它们具有在图形中显示的xy

I start the dataset with this: 我用这个开始数据集:

    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;
       })
       ;

Try with this: 试试这个:

    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;
           })              
           ;
    }

Edited 编辑

    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;
               })              
               ;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM