簡體   English   中英

餅圖的顏色模式

[英]Color pattern for pie chart

我想在餅圖中總共使用三種顏色。 我的問題是我不希望連續出現相同的2種顏色。
這是我的簡化代碼:

var colors = ["R", "G", "B"];

for(var i = 2; i < 30; i++) {
    if(i % 2 == 0) {
        for(var x = 0; x < i; x++) document.write(colors[x % 2]);
    }else if(i % 3 == 0) { 
        for(var x = 0; x < i; x++) document.write(colors[x % 3]);
    }else {
        if(i % 3 == 1) {
            for(var x = 0; x < i; x++) document.write("<span style='color:red'>" + colors[x % 3] + "</span>");
        }else {
            for(var x = 0; x < i; x++) document.write(colors[x % 3]);
        }
    }
    document.write("<br>");
}

這是帶有相同代碼的JS小提琴 ,紅色文本是出現問題的地方。 該行以R開頭,以R結束,這使它們在餅圖中連續出現兩次。

如何避免同一顏色連續出現兩次?

對於有問題的情況,只對最后一個分段重復所有前兩個顏色。 對於最后一段,請使用第三種顏色。 像這樣:

for(var x = 0; x < i-1; x++) document.write(colors[x % 2]);
document.write(colors[2]);

這有幫助嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM