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

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

如何在 d3.js 中創(chuàng)建餅圖可視化,手動(dòng)鍵入輸入并平滑過渡

如何在 d3.js 中創(chuàng)建餅圖可視化,手動(dòng)鍵入輸入并平滑過渡

一只甜甜圈 2023-07-06 17:37:18
我正在嘗試使用 D3.js 創(chuàng)建一個(gè)包含 3 個(gè)餅圖切片的餅圖。我想用鍵盤輸入值,并且更新值時(shí)(單擊按鈕時(shí))的過渡應(yīng)該平滑,這就是我使用 merge() 和 transition() 的原因。換句話說,我想做的是調(diào)整這個(gè)給定示例中的代碼:https://www.d3-graph-gallery.com/graph/pie_changeData.html不同之處在于,我希望能夠手動(dòng)輸入值,而不是將它們放在代碼中(最好總是 3 個(gè)餅圖切片)。由于某種原因,我無法真正適應(yīng)代碼。提前致謝。// set the dimensions and margins of the graphvar width = 450height = 450margin = 40// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.var radius = Math.min(width, height) / 2 - margin// append the svg object to the div called 'my_dataviz'var svg = d3.select("#my_dataviz")  .append("svg")  .attr("width", width)  .attr("height", height)  .append("g")  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");// set the color scalevar color = d3.scaleOrdinal()  .domain(["a", "b", "c", "d"])  .range(d3.schemeDark2);// A function that create / update the plot for a given variable:function update() {  var data = d3.selectAll('.fuel').nodes();  var pie = d3.pie() //we create this variable, for the values to be readeable in the console    .value(function(d) {      return d.innerHTML;    })(data);  var u = svg.selectAll("path")    .data(pie)  // Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.  u.enter()    .append('path')    .merge(u)    .transition()    .duration(5000)    .attr('d', d3.arc()      .innerRadius(0)      .outerRadius(radius)    )    .attr('fill', function(d) {      return (color(d.data.key))    })    .attr("stroke", "white")    .style("stroke-width", "2px")    .style("opacity", 1)}<meta charset="utf-8"><!-- Load d3.js --><script src="https://d3js.org/d3.v4.js"></script><!-- Color scale --><script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
查看完整描述

1 回答

?
largeQ

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

要訪問輸入框中的值,請(qǐng)使用this.value, not this.innerHtml,其中this指向相關(guān) DOM 節(jié)點(diǎn):


// set the dimensions and margins of the graph

var width = 450

height = 450

margin = 40


// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.

var radius = Math.min(width, height) / 2 - margin


// append the svg object to the div called 'my_dataviz'

var svg = d3.select("#my_dataviz")

  .append("svg")

  .attr("width", width)

  .attr("height", height)

  .append("g")

  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");


// set the color scale

var color = d3.scaleOrdinal()

  .domain(["a", "b", "c", "d"])

  .range(d3.schemeDark2);


// A function that create / update the plot for a given variable:

function update() {


  var data = [];

  d3.selectAll('.fuel').each(function() {

    data.push(+this.value || 0);

  });


  var pie = d3.pie()

    (data);


  var u = svg.selectAll("path")

    .data(pie)


  // Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.

  u.enter()

    .append('path') 

    .merge(u)

    .transition()

    .duration(5000)

    .attr('d', d3.arc()

      .innerRadius(0)

      .outerRadius(radius)

    )

    .attr('fill', function(d) {

      return (color(d.data.key))

    })

    .attr("stroke", "white")

    .style("stroke-width", "2px")

    .style("opacity", 1)

}

<meta charset="utf-8">


<!-- Load d3.js -->

<script src="https://d3js.org/d3.v4.js"></script>


<!-- Color scale -->

<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>



<!-- Create 3 cells for the input -->


<td>

  <input type="number" class="fuel" style="text-align:center">

</td>

<td>

  <input type="number" class="fuel" style="text-align:center">

</td>

<td>

  <input type="number" class="fuel" style="text-align:center">

</td>


<!-- Add  button -->

<button onclick="update()">Update</button>


<!-- Create a div where the graph will take place -->

<div id="my_dataviz"></div>


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

添加回答

舉報(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)