vegalite({
$schema: "https://vega.github.io/schema/vega-lite/v2.0.json",
height: 533,
width: 300,
data: { url: "https://vega.github.io/vega-datasets/data/barley.json" },
description: "Slope graph à la Edward Tufte",
transform: [
{
aggregate: [{ op: "sum", field: "yield", as: "yield" }],
groupby: ["year", "site"]
},
{ as: "first_year", calculate: "min(datum.year, 1931)" },
{ as: "last_year", calculate: "max(datum.year, 1932)" }
],
layer: [
{
mark: "line",
description: "lines linking the `yield` values",
encoding: {
x: {
field: "year",
type: "ordinal",
axis: {
orient: "top",
zindex: 0,
title: "",
grid: true,
domain: false,
ticks: false,
labels: true,
labelAngle: 0
}
},
y: {
field: "yield",
type: "quantitative",
axis: null,
scale: { zero: false }
},
detail: { field: "site", type: "nominal" },
color: { value: "lightgray" },
size: { value: 1.5 }
}
},
{
mark: "circle",
description: "white circles to contain the `yield` values",
encoding: {
x: {
field: "year",
type: "ordinal",
axis: {
orient: "top",
zindex: 0,
title: "",
grid: true,
domain: false,
ticks: false,
labels: true
}
},
y: { field: "yield", type: "quantitative", axis: null },
size: { value: 400 },
color: { value: "white" },
opacity: { value: 1 }
}
},
{
mark: "text",
description: "`yield` values",
encoding: {
x: {
field: "year",
type: "ordinal",
axis: {
orient: "top",
zindex: 0,
title: "",
grid: true,
domain: false,
ticks: false,
labels: true
}
},
y: { field: "yield", type: "quantitative", axis: null },
detail: { field: "site", type: "nominal" },
text: { field: "yield", type: "quantitative" },
color: { value: "black" },
size: { value: 10 }
}
},
{
mark: { type: "text", style: "last_name" },
transform: [{ filter: "datum.year === datum.last_year" }],
description: "last year site label",
encoding: {
x: {
field: "year",
type: "ordinal",
axis: {
orient: "top",
zindex: 0,
title: "",
grid: true,
domain: false,
ticks: false,
labels: true
}
},
y: { field: "yield", type: "quantitative", axis: null },
text: { field: "site", type: "nominal" },
size: { value: 10 },
color: { value: "black" }
}
},
{
mark: { type: "text", style: "first_name" },
transform: [{ filter: "datum.year === datum.first_year" }],
description: "first year site label",
encoding: {
x: {
field: "year",
type: "ordinal",
axis: {
orient: "top",
zindex: 0,
title: "",
grid: true,
domain: false,
ticks: false,
labels: true
}
},
y: { field: "yield", type: "quantitative", axis: null },
text: { field: "site", type: "nominal" },
size: { value: 10 },
color: { value: "black" }
}
}
],
config: {
view: { strokeWidth: 0 },
numberFormat: ".0f",
axis: {
gridWidth: 0,
labelColor: "black",
labelFontSize: 11,
labelAngle: 0
},
text: { align: "center", baseline: "middle" },
style: {
last_name: { dx: 15, align: "left", baseline: "middle" },
first_name: { dx: -15, align: "right", baseline: "middle" }
}
}
})