亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

HTML/JS 畫布甜甜圈圖如何創建重疊的圓形筆觸?

HTML/JS 畫布甜甜圈圖如何創建重疊的圓形筆觸?

海綿寶寶撒 2022-05-26 16:25:58
我的問題是我的甜甜圈圖沒有按我想要的那樣工作。我想創建一個像這樣的甜甜圈圖:但我的甜甜圈圖現在看起來像這樣:如您所見,筆畫不會在正確的方向上重疊。我想這可能是因為我開始從右到左畫筆畫。相反,它應該從左到右繪制它們,因此左側的“圓形末端”是可見的,而不是右側的圓形末端。這是我到目前為止所嘗試的://function to draw the donut chart, ctx = context, cx - cy = position, radius and arcwithdmbChart(ctx, cx, cy, radius, arcwidth) {   var tot = 0;   var accum = 0;   var PI = Math.PI;   var PI2 = PI * 2;   var offset = -PI/2;   for(var i = 0; i < this.canvasValues.length; i++) {     tot += this.canvasValues[i];   }     //Donut Sectors Color: Draw each stroke based on the value (canvasValues) and Color (canvasColors)   for(var i = 0; i < this.canvasValues.length; i++) {    ctx.lineWidth = arcwidth;    ctx.beginPath();    ctx.lineCap = "round";    ctx.arc(cx, cy, radius, offset + PI2 * (accum/tot), offset + PI2 * ((accum + this.canvasValues[i]) / tot));    ctx.strokeStyle = this.canvasColors[i];    ctx.stroke();     accum += this.canvasValues[i];   } }正如你所看到的,我得到的值是每個筆畫的長度和顏色的百分比。從頂部開始,我從頂部 -> 右側 -> 底部 -> 左側繪制每個,這就是結果。但是我怎樣才能修改它以獲得最上面的結果呢?編輯: 在@Helder Sepulveda 的幫助下,我現在這樣創建了它。我更改了很多計算,修復了更改帶來的一些錯誤?,F在唯一的問題是它沒有開始在頂部繪制。如您所見,綠色筆劃應從頂部開始:function dmbChart(ctx, cx, cy, radius, arcwidth) {   var canvasValues =  [30, 5, 15, 10, 10, 10, 10, 10];  var canvasColors = ["#10dc60", "#DDDDDD", "#0cd1e8", "#ffce00", "#7044ff", "#f04141", "#ffea00", "#ee82ee"];  ctx.lineWidth = arcwidth;  ctx.lineCap = "round";     var accum = canvasValues.reduce((a,b) => a + b, 0);    for (var i = canvasValues.length-1; i >= 0; i--) {    var radians = canvasValues[i] / 100 * 360 * Math.PI / 180    ctx.beginPath();    ctx.arc(cx, cy, radius, accum, accum - radians, true);    ctx.strokeStyle = canvasColors[i];    ctx.stroke();        accum -= radians;  }    ctx.beginPath();  ctx.arc(cx, cy, radius, accum, accum - (0.1 / 100 * 360 * Math.PI / 180), true);    ctx.strokeStyle = canvasColors[canvasColors.length - 1];  ctx.stroke();}
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

我正在做一些假設canvasValues并canvasColors展示一些有效的東西:


function dmbChart(ctx, cx, cy, radius, arcwidth) {

  var accum = 0;

  var PI = Math.PI;

  var PI2 = PI * 2;

  var offset = -PI / 2;

  var canvasValues = [10, 10, 10]

  var canvasColors = ["red", "green", "blue"]

  var tot = canvasValues.reduce((a, b) => a + b, 0)


  ctx.lineWidth = arcwidth;

  ctx.lineCap = "round";


  for (var i = 0; i < canvasValues.length; i++) {

    ctx.beginPath();

    ctx.arc(cx, cy, radius, offset + PI2 * (accum / tot), offset + PI2 * ((accum + canvasValues[i]) / tot));

    ctx.strokeStyle = canvasColors[i];

    ctx.stroke();

    accum += canvasValues[i];

  }


  ctx.beginPath();

  ctx.arc(cx, cy, radius, offset, offset);

  ctx.strokeStyle = canvasColors[0];

  ctx.stroke();

}


const canvas = document.getElementById("c");

canvas.width = canvas.height = 140;

const ctx = canvas.getContext("2d");


dmbChart(ctx, 70, 70, 50, 30)

<canvas id="c"></canvas>


這個想法是在循環結束時用第一個值(和顏色)繪制最后一個“短”弧


我還將幾行移出循環:


  ctx.lineWidth = arcwidth;

  ctx.lineCap = "round";

可以在循環之前設置一次


這是我們在顛倒方向的評論中談到的


function dmbChart(ctx, cx, cy, radius, arcwidth) {

  var PI = Math.PI;

  var PI2 = PI * 2;

  var offset = -PI / 2;

  var canvasValues = [10, 10, 10]

  var canvasColors = ["red", "green", "blue"]

  var tot = canvasValues.reduce((a,b) => a + b, 0)

  var accum = tot;

  

  ctx.lineWidth = arcwidth;

  ctx.lineCap = "round";


  for (var i = canvasValues.length-1; i >= 0; i--) {

    ctx.beginPath();

    ctx.arc(cx, cy, radius, offset + PI2 * (accum / tot), offset + PI2 * ((accum + canvasValues[i]) / tot));

    ctx.strokeStyle = canvasColors[i];

    ctx.stroke();    

    accum -= canvasValues[i];    

  }


  ctx.beginPath();

  p = offset + PI2 * ((tot + canvasValues[canvasValues.length-1]) / tot)

  ctx.arc(cx, cy, radius, p, p);  

  ctx.strokeStyle = canvasColors[canvasColors.length-1];

  ctx.stroke();

}


const canvas = document.getElementById("c");

canvas.width = canvas.height = 140;

const ctx = canvas.getContext("2d");


dmbChart(ctx, 70, 70, 50, 30)

<canvas id="c"></canvas>


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號