D3js基礎(chǔ)課11:數(shù)據(jù)更新

我們先繪制一個(gè)原始的條形圖

<script src="https://d3js.org/d3.v6.min.js"></script>
<div id="view011101"></div>
<script>
    data = [120, 140, 150, 180]  // define width is array

    svg = d3.select("#view011101")
        .append("svg")
        .attr("width", 250)
        .attr("height", 150)
    chart = svg     // select svg
        .selectAll('rect')
        .data(data).enter()
        .append('rect')
        .attr('x', 0)
        .attr('y', function(d,i) {return i * 30})
        .attr('width', function(d) {return d})
        .attr('height', 25)
</script>
原始條形圖

更新數(shù)據(jù)

現(xiàn)在,點(diǎn)擊【Click to Update】按鈕

我們用隨機(jī)數(shù)據(jù)更新條形圖。

<script src="https://d3js.org/d3.v6.min.js"></script>
<p id="click">Click to Update</p>
<div id="view011102"></div>
<script>
    data = [120, 140, 150, 180]  // define width is array

    svg = d3.select("#view011102")
        .append("svg")
        .attr("width", 250)
        .attr("height", 150)
    chart = svg     // select svg
        .selectAll('rect')
        .data(data).enter()
        .append('rect')
        .attr('x', 0)
        .attr('y', function(d,i) {return i * 30})
        .attr('width', function(d) {return d})
        .attr('height', 25)  

    d3.select("#click")
        .on("click", function() {  // Event listener - later about that!
            // create random data
            data = [Math.round(Math.random()*120), 
                    Math.round(Math.random()*140), 
                    Math.round(Math.random()*150), 
                    Math.round(Math.random()*180)]         // Custom function, we can write your own!
            // update rect widths with enw data
            svg
                .selectAll("rect")     // select rects to be updated
                .data(data)            // New data binded to rects
                .attr('width', function(d) {return d}) // visual attribute to be updated
        })
</script>
更新數(shù)據(jù)

同時(shí)更新標(biāo)簽

<script src="https://d3js.org/d3.v6.min.js"></script>
<p id="click2">Click to Update</p>
<div id="view011103"></div>
<script>
    data = [120, 140, 150, 180]  // define width is array

    svg = d3.select("#view011103")
        .append("svg")
        .attr("width", 250)
        .attr("height", 150)
    chart = svg     // select svg
        .selectAll('rect')
        .data(data).enter()
        .append('rect')
        .attr('x', 0)
        .attr('y', function(d,i) {return i * 30})
        .attr('width', function(d) {return d})
        .attr('height', 25)  

    // After creating basic barchart
    svg.selectAll("text")
        .data(data)
        .enter()
        .append("text")
        .text(function(d) {return d})
        .attr("y", function(d,i) {return i*30 + 15})
        .attr("x", function(d) {return d - 20})
        .attr("font-family", "sans-serif")
        .attr("font-size", "10px")
        .attr("fill", "white")        
        
    d3.select("#click2")
        .on("click", function() {  // Event listener - later about that!
            // create random data
            data = [Math.round(Math.random()*120), 
                    Math.round(Math.random()*140), 
                    Math.round(Math.random()*150), 
                    Math.round(Math.random()*180)]         // Custom function, we can write your own!
            // update rect widths with enw data
            svg
                .selectAll("rect")     // select rects to be updated
                .data(data)            // New data binded to rects
                .attr('width', function(d) {return d}) // visual attribute to be updated
            svg
                .selectAll("text")     // select text tags to be updated
                .data(data)           // New data binded to elements
                .attr('x', function(d) {return d-20}) // placed at new positions                    
                .text(function(d) {return d})         // value updated                                 
        })
</script>
同時(shí)更新標(biāo)簽

關(guān)于代碼運(yùn)行效果

由于在簡(jiǎn)書上不能展示代碼的運(yùn)行結(jié)果,我在網(wǎng)上搭建了一個(gè) jupyter notebook http://jupyter.cyber-life.cn/lab,我將本套教程的筆記和源代碼寫在了上面,可以在線查看代碼運(yùn)行效果,還可以調(diào)試代碼,有需要的同學(xué)私信我。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 現(xiàn)在,如果我們要在SVG中手動(dòng)創(chuàng)建條形圖。我們會(huì)這樣做: 用 D3 創(chuàng)建條形圖 讓我們用 D3 來(lái)做同樣的事情 添...
    編程浪子閱讀 458評(píng)論 0 0
  • 動(dòng)態(tài)的圖表,是指圖表在某一時(shí)間段會(huì)發(fā)生某種變化,可能是形狀、顏色、位置等,而且用戶是可以看到變化過(guò)程的。而這個(gè)變化...
    編程浪子閱讀 661評(píng)論 4 2
  • 比例尺是比較重要的工具,可以實(shí)現(xiàn)抽象數(shù)據(jù)的一個(gè)維度到一個(gè)可視化表示的映射(mapping)??梢园驯壤呦胂癯梢粋€(gè)...
    編程浪子閱讀 903評(píng)論 0 0
  • 軸將比例尺渲染成更可讀的標(biāo)記。提供了四個(gè)方向的軸: d3.axisTop(scale) d3.axisRight(...
    編程浪子閱讀 445評(píng)論 0 1
  • 二維數(shù)據(jù)通常使用散點(diǎn)圖進(jìn)行可視化。其中兩個(gè)尺寸表示在兩個(gè)不同的軸上,水平x軸和垂直y軸。 數(shù)據(jù) 數(shù)組:每個(gè)主數(shù)組包...
    編程浪子閱讀 630評(píng)論 0 0

友情鏈接更多精彩內(nèi)容