Chart
Bars, a line, an area or a donut drawn to scale, with a tooltip, a legend, keys through the points and its data as a table.
Previews are drawn in the theme you made.
Edit itBars #
January to June
| Category | Desktop | Mobile |
|---|---|---|
| January | 186 | 80 |
| February | 305 | 200 |
| March | 237 | 120 |
| April | 73 | 190 |
| May | 209 | 130 |
| June | 214 | 140 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
// The kind is a value you import: a page that draws bars ships bars. The lit
// point is the chart's own Model, folded with `Chart.update`.
export default example({
title: 'Bars',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'bars',
label: 'Visitors',
description: 'January to June',
kind: Chart.bars,
...visitors,
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Stacked bars #
| Category | Desktop | Mobile |
|---|---|---|
| January | 186 | 80 |
| February | 305 | 200 |
| March | 237 | 120 |
| April | 73 | 190 |
| May | 209 | 130 |
| June | 214 | 140 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
// `stack` puts the series on one another per category; the axis spans the stack.
export default example({
title: 'Stacked bars',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'stacked',
label: 'Visitors by device',
kind: Chart.bars,
stack: true,
...visitors,
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Step line, markers #
| Category | Desktop | Mobile |
|---|---|---|
| January | 186 | 80 |
| February | 305 | 200 |
| March | 237 | 120 |
| April | 73 | 190 |
| May | 209 | 130 |
| June | 214 | 140 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
// `curve: 'step'` runs level to the next category before it rises or falls,
// and `markers: 'always'` puts a dot on every point, not only the lit one.
export default example({
title: 'Step line, markers',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'line',
label: 'Visitors',
kind: Chart.line,
curve: 'step',
markers: 'always',
...visitors,
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Stacked area #
| Category | Desktop | Mobile |
|---|---|---|
| January | 186 | 80 |
| February | 305 | 200 |
| March | 237 | 120 |
| April | 73 | 190 |
| May | 209 | 130 |
| June | 214 | 140 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
// A stacked area: each series fills down to the one beneath it.
export default example({
title: 'Stacked area',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'area',
label: 'Visitors',
kind: Chart.area,
stack: true,
...visitors,
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Donut #
| Value | |
|---|---|
| Chrome | 275 |
| Safari | 200 |
| Firefox | 187 |
| Edge | 173 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state } from '../../snippets/chartState.js'
const browsers = [
{ name: 'Chrome', value: 275 },
{ name: 'Safari', value: 200 },
{ name: 'Firefox', value: 187 },
{ name: 'Edge', value: 173 },
]
// The hole shows the total, or the slice the pointer or the keys are on.
export default example({
title: 'Donut',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'donut',
label: 'Visitors by browser',
kind: Chart.donut,
slices: browsers,
totalLabel: 'Visitors',
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Pie #
| Value | |
|---|---|
| Chrome | 275 |
| Safari | 200 |
| Firefox | 187 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state } from '../../snippets/chartState.js'
// `hole: 0` is a pie: the slices meet at the centre, and no total is drawn.
export default example({
title: 'Pie',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'pie',
label: 'Visitors by browser',
kind: Chart.donut,
hole: 0,
slices: [
{ name: 'Chrome', value: 275 },
{ name: 'Safari', value: 200 },
{ name: 'Firefox', value: 187 },
],
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Its data, shown #
| Category | Desktop | Mobile |
|---|---|---|
| January | 186 | 80 |
| February | 305 | 200 |
| March | 237 | 120 |
| April | 73 | 190 |
| May | 209 | 130 |
| June | 214 | 140 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
// The drawing is hidden from assistive technology, which reads the same data
// from a table. `table: 'shown'` draws it for everyone.
export default example({
title: 'Its data, shown',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'shown',
label: 'Visitors',
kind: Chart.bars,
...visitors,
table: 'shown',
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
A series keeps its color #
| Category | Mobile |
|---|---|
| January | 80 |
| February | 200 |
| March | 120 |
| April | 190 |
| May | 130 |
| June | 140 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
// Mobile names its color, so hiding Desktop from `series` does not repaint it.
export default example({
title: 'A series keeps its color',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'colors',
label: 'Mobile visitors',
kind: Chart.bars,
categories: visitors.categories,
series: [{ name: 'Mobile', values: [80, 200, 120, 190, 130, 140], color: 2 }],
legend: true,
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
A kind of your own #
| Month | Visitors |
|---|---|
| January | 186 |
| February | 305 |
| March | 237 |
| April | 73 |
| May | 209 |
| June | 214 |
import { Chart, ChartSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { Message, state, visitors } from '../../snippets/chartState.js'
interface Lollipops {
readonly categories: ReadonlyArray<string>
readonly values: ReadonlyArray<number>
}
// A kind of your own is an object. `entries`, `points` and `head` say what
// the legend, the status line and the data table read, so the chart supplies
// them; `draw` returns the plot. `pointer(at)` lights a point under the mouse.
const lollipop: Chart.ChartKind<Lollipops> = {
name: 'lollipop',
entries: () => [{ name: 'Visitors', color: '1' }],
points: data =>
data.categories.map((name, at) => ({
name,
rows: [{ name: 'Visitors', value: data.values[at] ?? 0, color: '1' }],
})),
head: () => ['Month', 'Visitors'],
draw: (data, { h, builders, plot, pointer }) => {
const top = Math.max(...data.values)
return [
h.div(builders.plot.attrs(plot), [
h.svg(
builders.canvas.attrs([h.AriaHidden(true)]),
data.values.flatMap((value, at) => {
const x = `${((at + 0.5) / data.values.length) * 100}%`
const y = `${100 - (value / top) * 85}%`
return [
h.line(builders.grid.attrs([h.X1(x), h.X2(x), h.Y1('100%'), h.Y2(y)]), []),
h.circle(
builders.dot.attrs([
h.Cx(x),
h.Cy(y),
h.R('7'),
h.DataAttribute('color', '1'),
pointer(at),
]),
[],
),
]
}),
),
]),
]
},
}
export default example({
title: 'A kind of your own',
slots: ChartSlots,
recipe: Recipes.Chart,
state,
view: ({ style, model, onMessage, h }) =>
Chart.view(
{
id: 'lollipop',
label: 'Desktop visitors',
kind: lollipop,
categories: visitors.categories,
values: visitors.series[0]?.values ?? [],
model: model.chart,
onMessage: message => onMessage(Message.GotChart({ message })),
style,
},
h,
),
})In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.
Usage
Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.