Skip to content

Custom sort order for stacked bar chart #2207

Answered by mbostock
spandl asked this question in Q&A
Discussion options

You must be logged in to vote

You want to use the order option to the stack transform (rather than the sort option). If you want to sort the segments in ascending order based on the z channel (which defaults to fill), that’s:

Plot.plot({
  marginLeft: 400,
  color: {
    domain: ["1", "2", "3", "4", "5"],
    range: ["grey", "orange", "red", "pink", "black"],
    legend: true
  },
  marks: [
    Plot.barX(dataset, {
      y: data.accessors.y,
      x: data.accessors.x,
      fill: data.accessors.z,
      order: "z",
      sort: {
        y: "-x"
      }
    })
  ]
})

If you want to specify a different order of z values, then you could specify the order option as a list of values in the desired order, e.g., ["1", "2",…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@spandl
Comment options

Comment options

You must be logged in to vote
2 replies
@spandl
Comment options

@mbostock
Comment options

Answer selected by mbostock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants