d3.js學習3

來源:互聯網
上載者:User

標籤:

Enter-update-exit模式

select.data(data),代表selection圖形與data資料的交集->Update Mode

select.data(data).enter(),代表data資料排除selection圖形與data資料的交集->Enter Mode

select.exit,移除所有資料,代表selection圖形的部分->Exit Mode

E-U-E即為d3.js的基礎

數組資料繫結

 

 

var data=[10,15,24,46,13,6,96,12,44,61,17];function render(data){    //進入    d3.select("body").selectAll("div.h-bar")//選擇頁面上所有即將產生的css為h-bar的元素    .data(data)    .enter()    .append("div")    .attr("class","h-bar")    .append("span");//用來顯示當前值d    //更新    d3.select("body").selectAll("div.h-bar")    .data(data)    .style("width",function(d){//設定柱狀圖的長度為d的3倍    return (d*3)+"px";  })    .select("span")//在span內輸出d值    .text(function(d){    return d;    });    //退出    d3.select("body").selectAll("div.h-bar")    .data(data)    .exit()    .remove();}setInterval(function(){    data.shift();//去除第一個元素,用於動態迴圈展示資料    data.push(Math.round(Math.random()*100));//在結尾處加一個隨機資料,同樣用於動態迴圈示範    render(data);},15000);//1.5秒每次迴圈更新資料render(data);//初次展示資料

CSS:

<style type="text/css">.h-bar {    min-height: 15px;    min-width: 10px;    background-color: steelblue;    margin-bottom: 2px;    font-size: 11px;    color: #f0f8ff;    text-align: right;    padding-right: 2px;}</style>

 

d3.js學習3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.