Styling in depth

VisQuill leaves the look to CSS. Your code says what something is, through class names; a stylesheet says how it looks. This chapter is about making that split work for you.

How class names are built

Every name in a style string gets a prefix: the one from VisQuill.create, followed by the prefixes of all layers the visual is in, from the outside in (Styles has the short version):

const canvas = VisQuill.create(div, "vqg-").canvas
const chart = canvas.layer("chart-")

chart.visuals.circle("bar selected")        // class="vqg-chart-bar vqg-chart-selected"
chart.visuals.circle(".highlight point")    // class="highlight vqg-chart-point"
chart.visuals.circle("bar @style fill: red") // class="vqg-chart-bar", style="fill: red"

A name with a leading dot is taken as it is, for classes the page defines anyway. A layer created with layer() instead of layer("name-") adds nothing.

One drawing, several looks

Because the prefix comes from the layer, the same drawing code produces differently named classes in different layers. Write the drawing once, with plain names, and give each layer its own look:

The same function draws both charts. Only the stylesheet tells them apart; point at the bars.
// One drawing function, no colors in it: only class names
const chart = (group: RvgGroup) => {
    const base = group.visuals.segment("base", [0, 0], [240, 0])
    ;[90, 150, 60, 120].forEach((height, i) => {
        const bar = group.visuals.rectangle("bar")
        Attach.rectangleToSegment(bar, height, base, { paddingLeft: 10 + i * 60, paddingRight: 190 - i * 60 })
    })
}

// Two layers with their own prefix: vqg-warm-bar and vqg-cool-bar
const warm = canvas.layer("warm-")
const cool = canvas.layer("cool-")
chart(warm)
chart(cool)
.vqg-warm-bar       { fill: #C2994C; transition: fill 150ms; }
.vqg-warm-bar:hover { fill: #A57404; }
.vqg-cool-bar       { fill: none; stroke: #3B372F; stroke-width: 2; }
.vqg-cool-base      { stroke-dasharray: 4 3; }

The same trick themes whole graphics: two graphics on one page with different prefixes in VisQuill.create can share all code and still look different.

What VisQuill writes, and what is left to you

VisQuill writes these into the SVG, and changes them whenever the geometry changes:

  • the geometry: x1/y1/x2/y2, cx/cy/r, points, d, x/y/width/height,
  • transform of groups, points, handles and turned text,
  • class and inline style from the style string,
  • display: none for a hidden visual (visible.value = false).

Leave those to VisQuill. Everything else is yours: colors, strokes, dashes, fonts, opacity, cursors, :hover, and CSS transitions of colors and opacity. For motion, use animations: CSS can’t transition what VisQuill writes as attributes.

One useful exception: CSS r sets the radius of points and handles, .vqg-handle { r: 14px; }, including the area you can grab a handle by. VisQuill’s own radius value doesn’t change, so nothing else is affected.

Inline styles

Classes can’t express a color that is computed from data. That’s what @style is for:

Drag the handle; its position sets the hue of the circle.
const dot = canvas.visuals.circle("")
Circles.circleAt([320, 130], 80, dot)
const handle = canvas.handles.disk("handle", [320, 250])

// A color computed from a value: an inline style, written whole each time
Reactive.do([handle], () => {
    const hue = Math.round(handle.x / 640 * 360)
    dot.applyStyleDirective(`@style fill: hsl(${hue}, 55%, 55%)`)
})

@style sets the whole inline style: a second @style replaces the first, it doesn’t add to it. Write all inline properties each time, and keep everything that is not computed in the stylesheet.