D3js code when called twice duplicates the graph instead of refreshing

前端 未结 2 1364
广开言路
广开言路 2021-01-21 13:28

Here is my D3js code

    function ShowGraph(data)
{

    var w = 600,
                h = 600,
                padding = 36,
                p = 31,
                     


        
2条回答
  •  走了就别回头了
    2021-01-21 14:03

    Here you always append a new SVG element (.append('svg')):

    var svg = d3.select("#D3line").append("svg")
                            .attr("width", w)
                            .attr("height", h);
    

    So instead of using a new SVG element (and thus a new graph), just maintain a link to the first selected graph and override it or select the SVG again:

       var svg = d3.select( '#D3line svg' );
       if ( !svg ) {
    
          svg = d3.select("#D3line").append("svg")
                                          .attr("width", w)
                                          .attr("height", h);
       }
    

    Or you clear all content of you element, where the SVG resides:

    document.querySelector( '#D3line' ).innerHTML = '';
    

提交回复
热议问题