第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

更改類時(shí)不應(yīng)用 D3 css

更改類時(shí)不應(yīng)用 D3 css

Helenr 2023-09-07 16:10:39
我有一個(gè)條形圖,我想在單擊條形圖時(shí)更改顏色。我想在單擊的元素上設(shè)置 CSS 類。一切正常,除了填充樣式(單擊的欄的顏色)沒有改變,但應(yīng)用了綠色描邊。我不明白為什么。這是單擊圖表最后一個(gè)柱之前和之后的屏幕 這就是我想要實(shí)現(xiàn)的目標(biāo)這是我的代碼的一部分,有什么建議嗎?JS//CREATE THE BAR CHARTvar bars = d3  .select("#bars")  .selectAll("rect")  .data(data);bars  .enter()  .append("rect")  .attr("x", function(d) {    return xScale(d.year);  })  .attr("y", function(d) {    return yScale(d.cost);  })  .attr("width", xScale.bandwidth)  .attr("height", function(d) {    return height - yScale(d.cost);  })  .style("fill", function(d) {    return colorScale(d.cost);  });//EVENT ON CLICKd3.select("#bars")  .selectAll("rect")  .on("click", function() {    //reset the previus bar selected    d3.select("#bars")      .select(".selected")      .classed("selected", false)      .style("fill", function(d) {        return colorScale(d.cost);  //reset the original color      });    //set the current bar as selected    d3.select(this).classed("selected", true);  });CSS.selected {  fill: red;  stroke: green;  stroke-width: 3;}
查看完整描述

1 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

一般來說,屬性樣式會(huì)被使用選擇器的 CSS 樣式覆蓋,而 CSS 樣式又會(huì)被內(nèi)聯(lián) CSS 樣式覆蓋。

  • .attr("fill", "blue")設(shè)置屬性fill="blue"

  • .selected { fill: "red"; }應(yīng)用風(fēng)格fill: red;;

  • .style("fill", "green")設(shè)置屬性style="fill: green;",內(nèi)聯(lián)樣式。

如果將所有這些應(yīng)用于同一元素,則最后一個(gè)優(yōu)先。通過取消選擇先前選擇的欄,您可以賦予它.style("fill",從而覆蓋任何潛在的未來樣式。我建議檢查您是否真的需要應(yīng)用這些樣式,.attr()如果需要就使用。

我在下面添加了一個(gè)小展示柜。

let settings = {

  attr: false,

  class: false,

  style: false,

};

draw();


function draw() {

  d3.select("rect")

    .attr("fill", settings.attr ? "red" : null)

    .attr("class", settings.class ? "blue" : "")

    .style("fill", settings.style ? "green" : null);

}


d3.selectAll("[type='checkbox']")

  .on("change", function() {

    settings[this.getAttribute("id")] = this.checked;

    draw();

  });

.blue {

  fill: blue;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

<div>

  <input type="checkbox" id="attr"/><label for="attr">attr</label>

  <input type="checkbox" id="class"/><label for="class">class</label>

  <input type="checkbox" id="style"/><label for="style">style</label>

</div>

<svg>

  <rect width="30" height="30"></rect>

</svg>


查看完整回答
反對(duì) 回復(fù) 2023-09-07
  • 1 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)