Skip to content

Setting colours with Plot.line #685

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

You must be logged in to vote

For multiple lines, the recommended pattern is to use tidy data where you have a single dataset that represents all of your series, together. For example, you might have a CSV file like this:

category,date,value
apples,2021-01-01,49
apples,2021-01-02,2
apples,2021-01-03,25
…
oranges,2021-01-01,17
oranges,2021-01-02,36
…

You can create a fake dataset like so:

data = ["apples", "oranges", "bananas"].flatMap(category => {
  let value = 0;
  return d3.utcDays(new Date("2021-01-01"), new Date("2022-01-01")).map(date => {
    value += Math.round(Math.random() * 100 - 50); // random walk
    return {category, date, value};
  });
})

You can then render this like so:

Plot.plot({
  marks: [
    Plot

Replies: 1 comment 1 reply

Comment options

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

Answer selected by Wolsten
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