Giter Club home page Giter Club logo

Comments (3)

Fil avatar Fil commented on May 22, 2024

I've tried to address this issue, but I'm not sure what is considered to be broken.

Here's my current test https://observablehq.com/d/d36850d4bd824097

data = [{l: "A", n: 1}, {l: "B", n: 3},{l: "C", n: 2}];

Plot.plot({
  facet: { data, y: "l" },
  marks: [
    Plot.ruleY(data, {y: "n", stroke: ["red", "blue", "orange"]})
  ]
})

With no faceting, this creates three rules, with the three colors.

Once we activate faceting, each facet has 1 rule, but only the "red" stroke is ever used. I don't think it's wrong ("red" goes to the first line of the facet, "blue" to the second, etc. And if I want to join the stroke array with the data I should do it before feeding them to plot (?).)

But this probably only shows that I didn't understand the issue :) You probably have a different example in mind?

from plot.

mbostock avatar mbostock commented on May 22, 2024

Aside: the stroke values here are abstract, not literal colors, so I changed your test to use ["foo", "bar", "baz"] for clarity. These are then assigned colors through the color scale, whose range defaults to tableau10 for ordinal values.

The behavior that I was imagining should be equivalent to the data having an additional stroke property:

data = [
  {l: "A", n: 1, stroke: "foo"}, 
  {l: "B", n: 3, stroke: "bar"},
  {l: "C", n: 2, stroke: "baz"}
]

And then referencing that property:

Plot.plot({
  facet: {data, y: "l"},
  marks: [
    Plot.ruleY(data, {y: "n", stroke: "stroke"})
  ]
})

The intent of specifying channel values as an array is to support columnar data such as this:

flatdata = ({
  i: [0, 1, 2],
  l: ["A", "B", "C"],
  n: [1, 2, 3],
  stroke: ["foo", "bar", "baz"]
})

This works fine without faceting, e.g.:

Plot.plot({
  marks: [
    Plot.ruleY(flatdata.i, {y: flatdata.n, stroke: flatdata.stroke})
  ]
})

But it is not consistent with row-oriented data with faceting. This should be equivalent to the above example using the stroke field:

Plot.plot({
  facet: {data: flatdata.i, y: flatdata.l},
  marks: [
    Plot.ruleY(flatdata.i, {y: flatdata.n, stroke: flatdata.stroke})
  ]
})

from plot.

mbostock avatar mbostock commented on May 22, 2024

The reason the function definition of stroke works, while the array definition does not, is that mark.initialize is called repeatedly for each facet’s data:

plot/src/marks/facet.js

Lines 29 to 31 in 4bb8198

const markData = mark.data === data ? facetData : mark.data;
const named = Object.create(null);
const {index, channels} = mark.initialize(markData);

However, if the channel’s value is already an array, it is returned as-is here, and thus all facets share the same channel values, rather than faceting them:

plot/src/mark.js

Lines 36 to 42 in 4bb8198

function Channel(data, {scale, type, value}) {
let label;
if (typeof value === "string") label = value, value = Array.from(data, field(value));
else if (typeof value === "function") label = value.label, value = Array.from(data, value);
else if (typeof value.length !== "number") value = Array.from(value);
return {scale, type, value, label};
}

My guess is that we need to do something like pass the facetIndex to mark.initialize, and when present and the channel is specified as an array, the Channel constructor should take the corresponding values (Array.from(index, i => values[i])) from the channel definition for each facet.

from plot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.