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

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

D3 堆疊條形圖,每個堆疊由不同組設(shè)置不同的顏色,

D3 堆疊條形圖,每個堆疊由不同組設(shè)置不同的顏色,

森林海 2023-08-24 17:56:16
問題我試圖在 D3 (v5) 中獲得一個堆疊條形圖,為不同的組提供單獨的彩色條形(我可以做到,圖 1),每個堆疊具有不同的顏色(取決于圖 2)。我找不到一種方法來獲得堆棧著色(即我希望組顏色的不同色調(diào)隨不同的堆棧高度而變化)圖 3 中的示例(除非我希望不同的組具有不同的顏色,即不重復(fù))因為他們在這里)。在我提供的代碼示例中,有兩組數(shù)據(jù)。一個簡單的設(shè)置,可以幫助處理數(shù)據(jù):Animal,Group,A,B,C,DDog,Domestic,10,10,20,5Cat,Domestic,20,5,10,10Mouse,Pest,75,5,35,0 Lion,Africa,5,5,30,25Elephant,Africa,15,15,20,20Whale,Marine,35,20,10,45Shark,Marine,45,55,0, 60Fish,Marine,20, 5,30,10我實際上正在嘗試使用一個更大的集合。這是bl.ocks.org我正在嘗試開發(fā)的代碼:// set the dimensions and margins of the graphconst margin = {    top: 90,    right: 20,    bottom: 30,    left: 40  },  width = 960 - margin.left - margin.right,  height = 960 - margin.top - margin.bottom;const svg = d3.select("#my_dataviz")  .append("svg")  .attr("width", width + margin.left + margin.right)  .attr("height", height + margin.top + margin.bottom)  .append("g")  .attr("transform",    "translate(" + margin.left + "," + margin.top + ")");const y = d3.scaleBand()  .range([0, height])  .padding(0.1);const x = d3.scaleLinear()  .range([0, width]);const z = d3.scaleOrdinal()  .range(["none", "lightsteelblue", "steelblue", "darksteelblue"]);d3.csv("https://gist.githubusercontent.com/JimMaltby/844ca313589e488b249b86ead0d621a9/raw/f328ad6291ffd3c9767a2dbdba5ce8ade59a5dfa/TimeBarDummyFormat.csv", d3.autoType, (d, i, columns) => {      var i = 3;      var t = 0;      for (; i < columns.length; ++i)        t += d[columns[i]] = +d[columns[i]];      d.total = t;      return d;    }  )JSFiddle: https://jsfiddle.net/yq7bkvdL/我嘗試過的我覺得我只是錯過了一些簡單的東西,但我是一個編碼菜鳥,我的編碼非常初級,所以我無法解決它。我想我要么把填充attr放在錯誤的地方。或者是我不明白如何在 的嵌套/分層數(shù)據(jù)中選擇鍵d3.stack。我嘗試了各種方法,但都沒有成功:1.顏色數(shù)組 我嘗試編寫一個函數(shù)來創(chuàng)建顏色數(shù)組,通過迭代(使用forEach)“鍵”和“組”值/名稱并將它們連接起來創(chuàng)建一個可以與d3 縮放(順序)以選擇正確的顏色。例如,對于第一個數(shù)據(jù)集,它將創(chuàng)建一個數(shù)組ColoursID [DomesticA, DomesticB, DomesticC, DomesticD,PestA, PestB...],然后與中的顏色相匹配ColourS ["grey", "lightgreen", "green", "darkgreen", "yellow", ...]下面是這樣做的嘗試,以及注釋掉的各種其他探索。
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經(jīng)驗 獲得超6個贊

如果您想在條形長度較小的情況下稍微改變條形顏色,您可以使用fill-opacity并保持fill相同!這樣,如果值較淺,則顏色會不太明顯且較淺。

opacity只需創(chuàng)建一個帶有 range 的新比例即可[0.3, 1]。我選擇 0.3 是因為 0 不透明度意味著條形圖不可見,而您通常不希望這樣。我添加了一個單獨的值d.height來表示條形的整個可見高度,該高度與 start 分開(但相當于d.Min + d.All + d.Max)。現(xiàn)在,只需將該屬性應(yīng)用于每個條形即可完成。

您可以選擇將范圍設(shè)置為[0, 1]而不用于d3.extent域 - 這可能會導(dǎo)致類似的結(jié)果,盡管您可以通過思想實驗發(fā)現(xiàn)一些差異。

現(xiàn)在該fill-opacity屬性已在每個柱上設(shè)置。因此同一堆棧中的條具有相同的fill-opacity值。但請注意,這完全是可選的,您也可以應(yīng)用不同的值。

// set the dimensions and margins of the graph

const margin = {

    top: 90,

    right: 20,

    bottom: 30,

    left: 40

  },

  width = 960 - margin.left - margin.right,

  height = 960 - margin.top - margin.bottom;


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

  .append("svg")

  .attr("width", width + margin.left + margin.right)

  .attr("height", height + margin.top + margin.bottom)

  .append("g")

  .attr("transform",

    "translate(" + margin.left + "," + margin.top + ")");


const y = d3.scaleBand()

  .range([0, height])

  .padding(0.1);

const x = d3.scaleLinear()

  .range([0, width]);

const z = d3.scaleOrdinal()

  .range(["none", "lightsteelblue", "steelblue", "darksteelblue"]);

const opacity = d3.scaleLinear()

  .range([0.3, 1]);


d3.csv("https://gist.githubusercontent.com/JimMaltby/844ca313589e488b249b86ead0d621a9/raw/f328ad6291ffd3c9767a2dbdba5ce8ade59a5dfa/TimeBarDummyFormat.csv", d3.autoType, (d, i, columns) => {

      var i = 3;

      var t = 0;

      for (; i < columns.length; ++i)

        t += d[columns[i]] = +d[columns[i]];

      d.total = t;

      d.height = d.total - d.Start;

      return d;

    }


  ).then(function(data) {

    const keys = data.columns.slice(3); // takes the column names, ignoring the first 3 items = ["EarlyMin","EarlyAll", "LateAll", "LateMax"]


    // List of groups = species here = value of the first column called group -> I show them on the X axis

    const Groups = d3.map(data, d => d.Group);

    y.domain(data.map(d => d.Ser));

    x.domain([2000, d3.max(data, d => d.total)]).nice();

    z.domain(keys);

    opacity.domain(d3.extent(data, d => d.height));

    console.log(opacity.domain());


    svg.append("g")

      .selectAll("g")

      .data(d3.stack().keys(keys)(data))

      .enter().append("g")

      .attr("fill", d => z(d.key))

      .selectAll("rect")

      .data(d => d)

      .enter()

      .append("rect")

      .attr("y", d => y(d.data.Ser))

      .attr("x", d => x(d[0]))

      .attr("height", y.bandwidth())

      .attr("width", d => x(d[1]) - x(d[0]))

      .attr("fill-opacity", d => opacity(d.data.height));


    svg.append("g")

      .attr("transform", "translate(0,0)")

      .call(d3.axisTop(x));


    svg.append("g")

      .call(d3.axisLeft(y));

  });

.bar {

  fill: rgb(70, 131, 180);

}

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

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

編輯:知道您想按組對條形進行著色,我將使用相同的邏輯,但進行一些調(diào)整:

首先,我轉(zhuǎn)而z處理fill-opacity(我仍然用它來強調(diào)不同的群體),并使用新的group顏色序數(shù)比例。這個規(guī)模的關(guān)鍵就是預(yù)先存在的領(lǐng)域d.Group

// set the dimensions and margins of the graph

const margin = {

    top: 90,

    right: 20,

    bottom: 30,

    left: 40

  },

  width = 960 - margin.left - margin.right,

  height = 960 - margin.top - margin.bottom;


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

  .append("svg")

  .attr("width", width + margin.left + margin.right)

  .attr("height", height + margin.top + margin.bottom)

  .append("g")

  .attr("transform",

    "translate(" + margin.left + "," + margin.top + ")");


const y = d3.scaleBand()

  .range([0, height])

  .padding(0.1);

const x = d3.scaleLinear()

  .range([0, width]);

const z = d3.scaleOrdinal()

  .range([0.25, 0.5, 0.75, 1]);

const group = d3.scaleOrdinal()

  .range(["darkgreen", "darkred", "steelblue", "purple"]);


d3.csv("https://gist.githubusercontent.com/JimMaltby/844ca313589e488b249b86ead0d621a9/raw/f328ad6291ffd3c9767a2dbdba5ce8ade59a5dfa/TimeBarDummyFormat.csv", d3.autoType, (d, i, columns) => {

      var i = 3;

      var t = 0;

      for (; i < columns.length; ++i)

        t += d[columns[i]] = +d[columns[i]];

      d.total = t;

      return d;

    }


  ).then(function(data) {

    const keys = data.columns.slice(3); // takes the column names, ignoring the first 3 items = ["EarlyMin","EarlyAll", "LateAll", "LateMax"]


    y.domain(data.map(d => d.Ser));

    x.domain([2000, d3.max(data, d => d.total)]).nice();

    z.domain(keys);

    group.domain(data.map(d => d.Group));


    svg.append("g")

      .selectAll("g")

      .data(d3.stack().keys(keys)(data))

      .enter().append("g")

      .attr("fill-opacity", d => z(d.key))

      .selectAll("rect")

      .data(d => d)

      .enter()

      .append("rect")

      .attr("y", d => y(d.data.Ser))

      .attr("x", d => x(d[0]))

      .attr("height", y.bandwidth())

      .attr("width", d => x(d[1]) - x(d[0]))

      .attr("fill", d => group(d.data.Group));

      


    svg.append("g")

      .attr("transform", "translate(0,0)")

      .call(d3.axisTop(x));


    svg.append("g")

      .call(d3.axisLeft(y));

  });

.bar {

  fill: rgb(70, 131, 180);

}

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

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

編輯2:如果你想自己指定顏色,我會使用顏色鍵映射:


// set the dimensions and margins of the graph

const margin = {

    top: 90,

    right: 20,

    bottom: 30,

    left: 40

  },

  width = 960 - margin.left - margin.right,

  height = 960 - margin.top - margin.bottom;


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

  .append("svg")

  .attr("width", width + margin.left + margin.right)

  .attr("height", height + margin.top + margin.bottom)

  .append("g")

  .attr("transform",

    "translate(" + margin.left + "," + margin.top + ")");


const y = d3.scaleBand()

  .range([0, height])

  .padding(0.1);

const x = d3.scaleLinear()

  .range([0, width]);

const z = d3.scaleOrdinal()

  .range([0.25, 0.5, 0.75, 1]);

const group = d3.scaleOrdinal()

  .range([

    { Start: "none", Min: "lightgreen", All: "green", Max: "darkgreen" },

    { Start: "none", Min: "indianred", All: "red", Max: "darkred" },

    { Start: "none", Min: "lightsteelblue", All: "steelblue", Max: "darksteelblue" }

  ]);


d3.csv("https://gist.githubusercontent.com/JimMaltby/844ca313589e488b249b86ead0d621a9/raw/f328ad6291ffd3c9767a2dbdba5ce8ade59a5dfa/TimeBarDummyFormat.csv", d3.autoType, (d, i, columns) => {

      var i = 3;

      var t = 0;

      for (; i < columns.length; ++i)

        t += d[columns[i]] = +d[columns[i]];

      d.total = t;

      return d;

    }


  ).then(function(data) {

    const keys = data.columns.slice(3); // takes the column names, ignoring the first 3 items = ["EarlyMin","EarlyAll", "LateAll", "LateMax"]


    y.domain(data.map(d => d.Ser));

    x.domain([2000, d3.max(data, d => d.total)]).nice();

    z.domain(keys);

    group.domain(data.map(d => d.Group));


    svg.append("g")

      .selectAll("g")

      .data(d3.stack().keys(keys)(data))

      .enter().append("g")

      .each(function(e) {

        d3.select(this)

          .selectAll("rect")

          .data(d => d)

          .enter()

          .append("rect")

          .attr("y", d => y(d.data.Ser))

          .attr("x", d => x(d[0]))

          .attr("height", y.bandwidth())

          .attr("width", d => x(d[1]) - x(d[0]))

          .attr("fill", d => group(d.data.Group)[e.key]);

      });

      


    svg.append("g")

      .attr("transform", "translate(0,0)")

      .call(d3.axisTop(x));


    svg.append("g")

      .call(d3.axisLeft(y));

  });

.bar {

  fill: rgb(70, 131, 180);

}

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

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


查看完整回答
反對 回復(fù) 2023-08-24
  • 1 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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