HUX布斯
如果您想在条形长度较小的情况下稍微改变条形颜色,您可以使用fill-opacity并保持fill相同!这样,如果值较浅,则颜色会不太明显且较浅。opacity只需创建一个带有 range 的新比例即可[0.3, 1]。我选择 0.3 是因为 0 不透明度意味着条形图不可见,而您通常不希望这样。我添加了一个单独的值d.height来表示条形的整个可见高度,该高度与 start 分开(但相当于d.Min + d.All + d.Max)。现在,只需将该属性应用于每个条形即可完成。您可以选择将范围设置为[0, 1]而不用于d3.extent域 - 这可能会导致类似的结果,尽管您可以通过思想实验发现一些差异。现在该fill-opacity属性已在每个柱上设置。因此同一堆栈中的条具有相同的fill-opacity值。但请注意,这完全是可选的,您也可以应用不同的值。// 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"]);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>编辑:知道您想按组对条形进行着色,我将使用相同的逻辑,但进行一些调整:首先,我转而z处理fill-opacity(我仍然用它来强调不同的群体),并使用新的group颜色序数比例。这个规模的关键就是预先存在的领域d.Group。// 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([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 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([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>