最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

D3.js入門(mén)之D3?DataJoin的使用

 更新時(shí)間:2022年11月08日 09:18:05   作者:FinGet  
DataJoin(數(shù)據(jù)連接)是D3中很重要的一個(gè)概念。D3是基于數(shù)據(jù)操作DOM的js庫(kù),DataJoin使我們能夠根據(jù)現(xiàn)有?HTML?文檔中的數(shù)據(jù)集注入、修改和刪除元素。本文主要和大家詳細(xì)聊聊DataJoin的使用,感興趣的可以學(xué)習(xí)一下

DataJoin(數(shù)據(jù)連接)是D3中很重要的一個(gè)概念。上一章有提到D3是基于數(shù)據(jù)操作DOM的js庫(kù),DataJoin使我們能夠根據(jù)現(xiàn)有 HTML 文檔中的數(shù)據(jù)集注入、修改和刪除元素。

上一章中我們用forEach循環(huán)的方式,畫(huà)了三個(gè)圓,那么在D3中該怎樣優(yōu)雅的處理呢?

const circles = [
    { text: 1, color: "red" },
    { text: 2, color: "green" },
    { text: 3, color: "blue" }
];

svg.selectAll("circle")
    .data(circles) // 綁定數(shù)據(jù)
    .enter() // 獲取有數(shù)據(jù)沒(méi)dom的集合
    .append("circle")
    .attr("class", "circle")
    .attr("id", (d) => `circle${d.text}`) // d 表示 data綁定的數(shù)據(jù)中的一項(xiàng)
    .attr("cx", (d) => 50 * d.text)
    .attr("cy", 50)
    .attr("r", 20)
    .attr("fill", (d) => d.color);

data、enter、exit

  • data 將指定數(shù)組的數(shù)據(jù) data 與已經(jīng)選中的元素進(jìn)行綁定并返回一個(gè)新的選擇集和enter 和 exit
  • enter 返回選擇集中有數(shù)據(jù)但沒(méi)有對(duì)應(yīng)的DOM元素
  • exit 選擇集中有DOM元素但沒(méi)有對(duì)應(yīng)的數(shù)據(jù)

<div id="dataEnter">
    <p></p>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
</div>
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

const update = d3.select("#dataEnter")
  .selectAll("p")
  .data(arr)
  .text((d) => d);

因?yàn)橛?個(gè)p標(biāo)簽,所以從1開(kāi)始渲染到5。

enter返回的是有數(shù)據(jù)沒(méi)有dom的集合,也就是數(shù)據(jù)比dom多,所以enterappend基本上都是成對(duì)出現(xiàn)的。

d3.select("#dataEnter")
  .selectAll("p")
  .data(arr)
  .enter()
  .append("p")
  .text((d) => d);

exit返回的是有dom沒(méi)有數(shù)據(jù)的集合,也就是dom比數(shù)據(jù)多,所以exitremove基本上也是成對(duì)出現(xiàn)的。

const arr = [1,2,3]

d3.select("#dataEnter")
  .selectAll("p")
  .data(arr)
  .text((d) => d)
  .exit()
  .remove();

點(diǎn)擊查看data, enter, exit示例

datum

如果datum()綁定的值是數(shù)組,那么整個(gè)數(shù)組會(huì)綁定到每個(gè)被選擇的元素上。

而使用data()的話,那么會(huì)依次綁定數(shù)據(jù)。

const datumDom = d3.select("#datum")
    .selectAll("p")
    .datum(circles)
    .text((d) => {
        console.log(d);
        return JSON.stringify(d);
    });

selection.datum().append()如果選擇集是空的,那么并不會(huì)添加元素,所以使用datum要確保選擇項(xiàng)(dom)存在。實(shí)際項(xiàng)目中,圖表都是從0到1繪制,所以一般都是使用data().append()。

join

const arr = [1, 2, 3];

svg.selectAll("circle")
    .data(arr)
    .join("circle")
    .attr("cx", (d) => d * 50)
    .attr("cy", (d) => d * 50)
    .attr("r", 16)
    .attr("fill", "#F89301");

join 根據(jù)需要追加、刪除和重新排列元素,以匹配先前通過(guò)選擇綁定的數(shù)據(jù),返回合并后的enter和update集合。

也就是說(shuō)join是一個(gè)簡(jiǎn)化操作,我們可以把join分解一下:

const circle = svg.selectAll("circle").data(arr);

const newCircle = circle.enter()
    .append("circle")
    .merge(circle)
    .attr("cx", (d) => d * 50)
    .attr("cy", (d) => d * 50)
    .attr("r", 16)
    .attr("fill", "#F89301");

最后

這里有一個(gè)示例動(dòng)態(tài)的顯示了enter(綠色)、update(灰色)、exit(紅色)效果:

點(diǎn)擊查看 join 示例

以上就是D3.js入門(mén)之D3 DataJoin的使用的詳細(xì)內(nèi)容,更多關(guān)于D3.js DataJoin的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

焉耆| 邻水| 江城| 本溪| 兴海县| 荃湾区| 荃湾区| 新余市| 长丰县| 高雄市| 海林市| 南雄市| 渝北区| 兴和县| 阿巴嘎旗| 宁南县| 普安县| 沈阳市| 奉化市| 三门县| 新泰市| 惠东县| 莒南县| 林周县| 武陟县| 巴林右旗| 荃湾区| 东乡县| 铜陵市| 洛南县| 佛山市| 苏尼特左旗| 敦化市| 岳阳市| 麦盖提县| 桐柏县| 若羌县| 泰兴市| 志丹县| 板桥市| 河间市|