Elicit.js

Image

Pictures work as they do in CSS. image() draws one picture per row at x, y, from its src channel, like an <img>. On bar, rect, area, point, ellipse, shape, waffle and dotStack, the image channel is the shape's background image: it paints inside the shape in place of the fill. Both channels take their settings as params: fit, position, and on image, repeat. For a picture behind the whole chart, use guides.image.

API reference

image(options)

Import plot from elicitjs. One picture per row, like an <img>, centred at x, y in a square of side 2 * size.

image({ src, channels, edits, id }) → Mark
image({ channels: { x, y, src: { field: "photo", fit: "cover", position: "top" } } })
Options
OptionType · defaultDescription
channels
Channelsdefault{}
x, y, src, size, angle plus the standard style channels.
src
string | (d) => string
The picture's URL for every row. Shorthand for channels.src = { value }.
size
number | ChannelSpecdefault16
Half the square's side, in px.
stroke, opacity, …
style
A stroke outlines the square; opacity fades the picture.
Channels
ChannelTypeDescription
x, y
linear | band | point
The centre. A missing channel parks it at the centre of that dimension.
src
raw
The picture's URL: a column of URLs ({ field }), one URL ({ value }) or a function of the row ({ fn }). Read as-is, never scaled. Takes fit (default 'contain') and position as params.
angle
linear
Rotation in math degrees about the centre.
Returns / emits

A mark emitting one picture per row that has a URL.

image channel (on marks)

A shape's background image. bar, rect, area, point, ellipse, shape, waffle and dotStack paint it inside each shape in place of the fill. Its settings are params of the channel.

barY({ image: "photo.jpg", channels })
barY({ channels: { x, y, image: { value: "icon.svg", fit: "contain", position: "bottom", repeat: "repeat-y" } } })
Options
OptionType · defaultDescription
image
string | (d) => string
The picture's URL. Shorthand for channels.image = { value }.
fit
'cover' | 'contain' | 'fill' | 'none' | 'scale-down'default'cover' on image · 'contain' on src
How the picture is sized in its box, as CSS object-fit.
position
stringdefault'center'
Where the picture sits in its box, as CSS object-position: keywords ('top', 'bottom left') and percentages ('25% 75%').
repeat
'no-repeat' | 'repeat' | 'repeat-x' | 'repeat-y'default'no-repeat'
Whether the picture repeats from where position puts it, as CSS background-repeat.

One picture per row

image() centres each picture on its row's x and y, in a square of side 2 * size (default size: 16, a 32px square). Give one URL with the src shorthand, or a column of URLs with channels: { src: { field } }. A row with no URL draws nothing.

The picture moves, picks and outlines like any other mark, so move() places it.

Place each forecast

Each row names its icon in src; move() writes both coordinates.

const chart = Elicit({
width: 420, height: 300,
margins: { top: 16, right: 20, bottom: 40, left: 48 },
axes: { x: { title: "how likely (%)" }, y: { title: "how disruptive" } },
data: [
{ day: "Mon", icon: "/images/sun.svg", chance: 70, impact: 1 },
],
schema: {
day: { type: "categorical" },
icon: { type: "categorical" },
chance: { type: "quantitative", domain: [0, 100] },
impact: { type: "quantitative", domain: [0, 10] },
},
marks: [
image({
size: 18,
channels: {
x: { field: "chance" },
y: { field: "impact" },
// One URL per row. `fit` and `position` work as on an <img>.
src: { field: "icon", fit: "contain" },
},
edits: [ move({ channels: ["x", "y"] }) ],
}),
],
});
mount(chart.dom());
Try: Drag each icon to how likely and how disruptive you think that day's weather is.

Pictures inside marks

Set the image channel on a mark with a shape and the picture replaces its fill. The mark keeps its stroke, its edits and its geometry. fillOpacity and opacity fade the picture. A hover or selection effect that sets fill shows its colour in place of the picture.

repeat repeats the picture from where position puts it, as background-repeat does. With fit: 'contain', position: 'bottom' and repeat: 'repeat-y', each picture is as wide as the bar and they stack up from its base, so a taller bar shows more copies.

On a waffle, each filled cell shows the picture and empty cells stay the track. With one cell per person, the chart is a pictogram.

People in a bar

image: { value, fit: "contain", position: "bottom", repeat: "repeat-y" }

const chart = Elicit({
width: 380, height: 300,
margins: { top: 14, right: 14, bottom: 26, left: 34 },
data: [
{ group: "18–34", people: 40 }, { group: "35–54", people: 65 },
{ group: "55+", people: 25 },
],
schema: {
group: { type: "categorical", domain: ["18–34", "35–54", "55+"] },
people: { type: "quantitative", domain: [0, 100] },
},
scales: { x: { type: "band", padding: 0.55 } },
marks: [
barY({
channels: {
x: { field: "group" },
y: { field: "people", edit: move() },
// As CSS: each picture as wide as the bar, stacked up from its base.
image: {
value: "/images/person.svg",
fit: "contain", position: "bottom", repeat: "repeat-y",
},
},
}),
],
});
mount(chart.dom());
Try: Drag a bar up or down.

A pictogram you fill

100 cells, one per person; edit.waffle.fill() sets how many are filled.

const chart = Elicit({
width: 260, height: 300,
margins: { top: 16, right: 12, bottom: 24, left: 12 },
data: [{ cat: "of 100 people", value: 30 }],
schema: {
cat: { type: "categorical", domain: ["of 100 people"] },
value: { type: "quantitative", domain: [0, 100] },
},
scales: { x: { type: "band" } },
marks: [
waffleY({
multiple: 10,
channels: {
x: { field: "cat" },
count: { field: "value", edit: edit.waffle.fill() },
// Each filled cell shows the picture; empty cells stay the track.
image: { value: "/images/person.svg", fit: "contain" },
},
}),
],
});
mount(chart.dom());
Try: Drag up or down the grid to set how many people out of 100.

Fit, position and repeat

The settings are params of the channel, next to its field or value: image: { field: 'photo', fit: 'contain', position: 'top' }. A mark's box is its shape's bounding box. An image()'s box is its square.

  • fit sizes the picture, as object-fit. 'cover' fills the box and crops what spills over; 'contain' shows all of it; 'fill' stretches it to the box; 'none' keeps the picture's own size; 'scale-down' is the smaller of 'none' and 'contain'. The default is 'cover' on image and 'contain' on src.
  • position places the picture in what room is left, as object-position and background-position. It takes keywords ('center', 'top', 'bottom left') and percentages ('25% 75%'). The default is 'center'.
  • repeat repeats the picture across the box, as background-repeat: 'no-repeat' (the default), 'repeat', 'repeat-x' or 'repeat-y'. It applies to image only, since an <img> does not repeat.

fit: 'none' and 'scale-down' read the picture's own width and height. Give an SVG explicit width and height attributes, or these use its default size.

arc, line, text and the other marks draw no image channel and log a warning when given one.