Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Themes #630

Open
mkfreeman opened this issue Dec 22, 2021 · 16 comments
Open

Themes #630

mkfreeman opened this issue Dec 22, 2021 · 16 comments
Labels
enhancement New feature or request

Comments

@mkfreeman
Copy link
Contributor

To support different styles, we could create a set of plot themes (e.g., dark, light, etc.). As a comparison, the ggthemes package has default styles to emulate popular styles like the Wallstreet Journal, FiveThirtyEight, etc.

Perhaps a simple approach would be to define a set of styles in Plot.themes that could be set as the style property in a Plot:

Plot.plot({
    style: Plot.themes.dark
})

Where Plot.themes.dark has a set of key/value pairs describing the style: {background: "black", color: "white",...}

@mkfreeman mkfreeman added the enhancement New feature or request label Dec 22, 2021
@Fil
Copy link
Contributor

Fil commented Dec 22, 2021

related: #6 #7 #114 #253 #397 #662

@awiederkehr
Copy link

Would love to help work on this however I can!

@Fil Fil mentioned this issue Nov 25, 2022
9 tasks
@mbostock
Copy link
Member

The new axis mark #1197 will help with this, for example you could have a ggplot2 theme #1197 (comment).

@Fil
Copy link
Contributor

Fil commented Mar 28, 2023

@Fil
Copy link
Contributor

Fil commented Mar 29, 2023

Here's a handy response from Mike on the user forum:
https://talk.observablehq.com/t/set-global-plot-defaults-for-observable-plot/7797/3

@martindaniel4
Copy link

As a Plot user, that feature would be very useful! Here is also another R package that was relevant to style graphs from a former Airbnb DS - https://github.com/ricardo-bion/ggtech

@Fil
Copy link
Contributor

Fil commented Aug 2, 2023

Textures #903

@tophtucker
Copy link
Contributor

tophtucker commented Aug 12, 2023

I like Mike’s nice style: Plot.themes.dark suggestion. And it could be prototyped as an ongoing notebook.

It gets trickier when you want to set a default theme across many plots. One can also imagine wanting to set default categorical/sequential/diverging color schemes, etc.

Maybe there could be something like

Plot.defaults({
  style: Plot.themes.dark,
  color: {scheme: {sequential: "blues"}}
})

which would return a new instance of Plot with those defaults? There’d be various difficulties. What’s that object syntax? (Note that I abused the color scheme option.) To name your version Plot in a notebook, would you have to do something like Plot = (await require("@observablehq/plot")).defaults({style: Plot.themes.dark}), or could you take advantage of the built-in Plot?

@mbostock
Copy link
Member

Yes, exactly Toph. On Observable, my hope is that you can configure Plot for your workspace and it automatically applies the theme to notebooks in your workspace without you having to edit them. And likewise in vanilla JavaScript, you could call export default Plot.defaults(…) or whatever and import that instead of Plot.

@Fil
Copy link
Contributor

Fil commented Aug 22, 2023

Projections! (suggested in #1111)

@mbostock
Copy link
Member

Projections!

What about projections, @Fil? Not sure how this is related to themes.

@Fil
Copy link
Contributor

Fil commented Aug 22, 2023

In that comment I wanted a mechanism to add a few named projections to my "themed" or personalized Plot, similarly to the way we'll be able to add (or modify) color themes. Once we have a theming mechanism in place it will seem natural to want to extend it (progressively) to other hard-coded lists of named option values. Even more so if it's called "defaults" and not "theme".

@christophe-g
Copy link

Stumbled across this while trying to adjust my observable colors to my themes.

What about adding a new scheme for categorical color space along the following, using css variables:

export default [
  'var(--plot-color-0)',
  'var(--plot-color-1)',
  'var(--plot-color-2)',
  'var(--plot-color-3)',
  'var(--plot-color-4)',
  'var(--plot-color-5)',
  'var(--plot-color-6)',
  'var(--plot-color-7)',
  'var(--plot-color-8)',
  'var(--plot-color-9)',
]

It would then be the consumer responsibility to set relevant colors - we could provide a helper so that it uses observable10 scheme by default. Handling dark, and customized theme is then pretty easy.

Happy to provide a PR if that makes sense

@Fil
Copy link
Contributor

Fil commented Jul 12, 2024

@christophe-g I like the idea of this virtual color scheme, but unfortunately I think this can't be the default because for the raster mark we need to convert the css colors into materialized RGB colors.

@christophe-g
Copy link

christophe-g commented Jul 12, 2024

Merci Fil,

for the raster mark we need to convert the css colors into materialized RGB colors

I guess you are referring to

({r, g, b} = rgb(fi));

I think this type of virtual color scheme should only be applied/available for categorical data, not for continuous scales. I am thinking of this approach as a generalization of observable10 scheme for instance.

In theory, when color processing is required (like compute rgb from a real color and not a css variable), it should be possible to infer the value of the css variable in a given context though:

// Get the root element - the problem is that this is not going to work well with shadow-dom, 
// you would need the root of the web-component for which css variables are defined. 
const root = document.documentElement;
const style = getComputedStyle(root);
const color0 = style.getPropertyValue('--plot-color-0');

@Fil
Copy link
Contributor

Fil commented Jul 15, 2024

This would work in many cases, but it's tricky to do generically. It's possible to use categorical colors in the raster mark, and we can't know in advance where in the DOM the chart might be appended. (Also, the materialized values for a variable might change with time, for example when entering dark mode.)

But outside of this caveat, it seems quite useful to have as a categorical palette.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants