d3.stackOrderDescending Not Sorting Properly

Viewed 21

I am trying to make a stacked bar chart using d3 by sorting the following CSV data from greatest to least:
https://raw.githubusercontent.com/garciarjralejandro/Set-Pieces/main/sequence3.csv


I attempted to use d3.stackOrderDescending, and while it does sort, it doesn't sort the data properly (i.e. the data isn't in descending order). How can I make it so that my bars stack correctly from greatest to least?

Thank you for your time, I greatly appreciate it.

Plunkr below:
https://plnkr.co/edit/tGpbkdjEdtw1Wino

Here is the code:

const margin = {top: 10, right: 30, bottom: 20, left: 50},
    width = 460 - margin.left - margin.right,
    height = 400 - margin.top - margin.bottom;


const svg = d3.select("#stackedBarChart")
  .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})`);


d3.csv("https://raw.githubusercontent.com/garciarjralejandro/Set-Pieces/main/sequence3.csv").then( function(data) {


  const subgroups = data.columns.slice(1)

  const groups = data.map(d => d.group)


  const x = d3.scaleBand()
      .domain(groups)
      .range([0, width])
      .padding([0.2])
  svg.append("g")
    .attr("transform", `translate(0, ${height})`)
    .call(d3.axisBottom(x).tickSizeOuter(0));

  const y = d3.scaleLinear()
    .domain([0, 160])
    .range([ height, 0 ]);
  svg.append("g")
    .call(d3.axisLeft(y));

  const color = d3.scaleOrdinal()
    .domain(subgroups)
    .range(['#C7EFCF','#FE5F55','#EEF5DB'])

  const stackedData = d3.stack()
    .order(d3.stackOrderDescending)
    .keys(subgroups)
    (data)



  // ----------------
  // Tooltip
  // ----------------
  const tooltip = d3.select("#stackedBarChart")
    .append("div")
    .style("opacity", 0)
    .attr("class", "tooltip")
    .style("background-color", "white")
    .style("border", "solid")
    .style("border-width", "1px")
    .style("border-radius", "5px")
    .style("padding", "10px")
    .style("width", "150px")

  const mouseover = function(event, d) {
    const subgroupName = d3.select(this.parentNode).datum().key;
    const subgroupValue = d.data[subgroupName];
    tooltip
        .html("Filter " + subgroupName + "<br>" + "Value: " + subgroupValue)
        .style("opacity", 1)

  }
  const mousemove = function(event, d) {
    tooltip.style("transform","translateY(-55%)")
           .style("left",(event.x)/2+"px")
           .style("top",(event.y)/2-30+"px")
  }
  const mouseleave = function(event, d) {
    tooltip
      .style("opacity", 0)
  }

  const colors = ["#9fc1ee", "#ee9f9f"]


  svg.append("g")
    .selectAll("g")
    .data(stackedData)
    .join("g")
      .attr("fill", d => color(d.key))
      .selectAll("rect")
      .data(d => d)
      .join("rect")
        .attr("x", d =>  x(d.data.group))
        .attr("y", d => y(d[1]))
        .attr("height", d => y(d[0]) - y(d[1]))
        .attr("width",x.bandwidth())
        .attr("stroke", "grey")
        .attr("fill", function(d, i) {
            return colors[i];
        })
      .on("mouseover", mouseover)
      .on("mousemove", mousemove)
      .on("mouseleave", mouseleave)

})
0 Answers
Related