Public
Edited
Mar 18, 2024
Insert cell
Insert cell
Insert cell
function g2(options) {
const chart = new G2.Chart(); // Init Chart.
chart.options(options); // Set options.
chart.render(); // Render chart by options.
invalidation.then(() => chart.destroy()); // Destory chart when cell dispose.
return chart.getContainer(); // Return and mount container.
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
g2({
type: "point",
data,
encode: {
x: "sport",
y: "height",
color: "gender"
}
})
Insert cell
Insert cell
g2({
type: "interval",
data,
encode: {
x: "sport"
},
transform: [{ type: "groupX", y: "count" }],
scale: { y: { nice: true } }
})
Insert cell
Insert cell
g2({
type: "rect",
data: data.filter((d) => {
const year = new Date(d.birthday).getFullYear();
return year !== 1900 && year < 2020;
}),
encode: {
x: (d) => new Date(d.birthday).getFullYear(),
color: (d) =>
d.gold !== 0
? "金牌"
: d.silver !== 0
? "银牌"
: d.bronze !== 0
? "铜牌"
: "无牌"
},
transform: [
{ type: "binX", y: "count" },
{ type: "stackY", orderBy: "value" }
],
scale: {
color: {
domain: ["金牌", "银牌", "铜牌", "无牌"],
range: ["#F6BD16", "#5D7092", "#CE8032", "#eee"]
},
y: { nice: true }
},
style: { inset: 0.5 }
})
Insert cell
Insert cell
g2({
type: "view",
data,
children: [
{
type: "link",
encode: {
x: 1,
y: (d) => (d.gold + d.silver + d.bronze === 0 ? 0 : 1),
x1: 0,
y1: 0,
series: "nation"
},
transform: [{ type: "group", channels: "series", x: "sum", y: "sum" }],
style: { stroke: "#eee" }
},
{
type: "point",
encode: {
x: 1,
y: (d) => (d.gold + d.silver + d.bronze === 0 ? 0 : 1),
series: "nation"
},
transform: [{ type: "group", channels: "series", x: "sum", y: "sum" }],
scale: {
x: { nice: true },
y: { nice: true }
},
axis: {
x: { title: "number of participants" },
y: { title: "number of winners" }
},
labels: [
{
text: "nation",
dy: 8,
fontSize: 10,
transform: [{ type: "overlapHide" }]
}
]
}
]
})
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more