queryverse / VegaLite.jl

Julia bindings to Vega-Lite
Other
268 stars 39 forks source link

How to store `config` in variable for reuse across plots? #442

Open rtedwards opened 1 year ago

rtedwards commented 1 year ago

I have a decent size config for customizing my charts. Currently I'm copy-pasting the config into each plot. Is there a way to store the config to a variable for easy reuse?

I've tried to store config as a dictionary and directly how it's defined within vplot without success. This is very likely already possibly, I'm just missing something.

Current

fitted_normalized_combined = fitted_normalized_combined_df |>
    @vlplot(
        :line,
        transform=[
            {density="data", bandwidth=0.25, groupby=["distribution"], counts=true, steps=50}
        ],
        title="Fitted Normal Distributions Densities from Other Distributions",
        x={
            "value:q", 
            title="Distribution",
        },
        y={"density:q", title="Density"},
        params=[{
            name= "moust-event",
            select= {type= "point", fields= ["distribution"]},
            bind= "legend"
        }],
        color={
            "distribution:n",
            legend={orient="top-right"},
            scale={range=colorscheme}
        },
        opacity={
          condition={param="moust-event", value= 1},
          value=0.25
        },
        padding=20,
        tooltip={:distribution},
        height=300,
        width=600,
        config={  # need to paste into every new chart
            background="#202124",
            view={stroke=:transparent},
            axis={
                labelFont=FONT,
                titleFont=FONT,
                titleColor="white",
                labelColor="white",
                tickColor="white",
                gridColor="grey",
                domainColor="white",
            },
            legend={
                labelFont=FONT,
                titleFont=FONT,
                labelColor="white",
                titleColor="white",
            },
            title={
                font=FONT,
                subtitleFont=FONT,
                color="white",
            },
            mark={
                font=FONT,
            },
            header={
                labelFont=FONT,
                titleFont=FONT,
                labelColor="white",
                titleColor="white",
            },
        },
    )

What I'd like to do

# would like to store into a variable
config=Dict(
    "background"=>"#202124",
    "view"=>Dict("stroke"=>:transparent),
    "axis"=>Dict(
        "labelFont"=>FONT,
        "titleFont"=>FONT,
        "titleColor"=>"white",
        "labelColor"=>"white",
        "tickColor"=>"white",
        "gridColor"=>"grey",
        "domainColor"=>"white",
    ),
    "legend"=>Dict(
        "labelFont"=>FONT,
        "titleFont"=>FONT,
        "labelColor"=>"white",
        "titleColor"=>"white",
    ),
    "title"=>Dict(
        "font"=>FONT,
        "subtitleFont"=>FONT,
        "color"=>"white",
    ),
    "mark"=>Dict(
        "font"=>FONT,
    ),
    "header"=>Dict(
        "labelFont"=>FONT,
        "titleFont"=>FONT,
        "labelColor"=>"white",
        "titleColor"=>"white",
    ),
)

fitted_normalized_combined = fitted_normalized_combined_df |>
    @vlplot(
        :line,
        transform=[
            {density="data", bandwidth=0.25, groupby=["distribution"], counts=true, steps=50}
        ],
        title="Fitted Normal Distributions Densities from Other Distributions",
        x={
            "value:q", 
            title="Distribution",
        },
        y={"density:q", title="Density"},
        params=[{
            name= "moust-event",
            select= {type= "point", fields= ["distribution"]},
            bind= "legend"
        }],
        color={
            "distribution:n",
            legend={orient="top-right"},
            scale={range=colorscheme}
        },
        opacity={
          condition={param="moust-event", value= 1},
          value=0.25
        },
        padding=20,
        tooltip={:distribution},
        height=300,
        width=600,
        config=config # pass variable
    )