Public
Edited
Oct 9
1 fork
3 stars
Insert cell
Insert cell
Insert cell
function g2(options) {
const chart = new G2.Chart();
chart.options(options);
chart.render();
invalidation.then((chart) => chart.render());
return chart.getContainer();
}
Insert cell
Insert cell
Insert cell
// A basic mark.
g2({
type: "point", // Point mark, a template to generate circles.
data: athletes, // The source of template.
encode: {
// Bind column of data to visual property.
x: "weight",
y: "height",
color: "sex"
}
})
Insert cell
Insert cell
// Different type of channels.
g2({
type: "interval",
data: profits,
paddingLeft: 50,
encode: {
x: "month",
y: ["end", "start"], // An array channels, sort for y and y1
// y: "end",
// y1: "start",
color: (d) => {
// A transform channel: derive new column data.
return d.month === "Total"
? "Total"
: d.profit > 0
? "Increase"
: "Decrease";
},
size: 30 // A constant channel.
},
axis: {
y: { labelFormatter: "~s" }
}
})
Insert cell
Insert cell
// Multiple marks in a view.
g2({
type: "view",
children: [
// To specify multiple marks with view.children.
{
type: "line", // A line mark.
data: values,
encode: { x: "year", y: "value" }
},
{
type: "point", // A point mark.
data: values,
encode: { x: "year", y: "value" },
style: { fill: "white" }
}
]
})
Insert cell
// A plot with composite and static marks.
g2({
type: "view",
children: [
{
type: "forceGraph", // A composite or high-order mark.
data: {
value: miserables
}
},
{
type: "text", // A static(non-data-driven) mark.
style: {
// Specify non-data-driven attributes.
x: 600, // Relative position.
y: "100%", // Absolute position.
text: "Force Graph",
textAnchor: "end",
fontSize: 20,
fontWeight: "bold"
}
}
]
})
Insert cell
Insert cell
g2({
type: "line",
data: {
type: "fetch", // An online data source.
value:
"https://gw.alipayobjects.com/os/bmw-prod/551d80c6-a6be-4f3c-a82a-abd739e12977.csv"
},
encode: {
x: "date",
y: "close"
}
})
Insert cell
// Inline data with transform.
g2({
type: "geoPath",
coordinate: { type: "albersUsa" },
data: {
value: topojson.feature(us, us.objects.counties).features,
// Data transform.
transform: [
{
type: "join",
join: unemployment,
on: ["id", "id"],
select: ["rate"]
}
]
},
encode: { color: "rate" },
scale: { color: { unknown: "#fff" } }
})
Insert cell
Insert cell
g2({
type: "area",
data: unemployment2,
paddingLeft: 60,
encode: {
x: (d) => new Date(d.date),
y: "unemployed",
color: "industry",
shape: "smooth"
},
transform: [{ type: "stackY" }] // A transform to avoid overlap in data space.
})
Insert cell
g2({
type: "point",
data: countries,
encode: {
shape: "point",
color: "Continent"
},
transform: [{ type: "pack" }] // A transform to avoid overlap in screen space.
})
Insert cell
g2({
type: "rect",
data: athletes,
encode: {
x: "weight",
y: "height",
color: "sex"
},
transform: [
// A transform to aggregate data.
{
type: "bin",
opacity: "count"
}
],
legend: { opacity: false }
})
Insert cell
g2({
type: "rect",
data: athletes,
encode: {
x: "weight",
y: "height",
color: "sex"
},
transform: [
// Multiple transform.
{ type: "binX", y: "count" },
{ type: "stackY", orderBy: "series" }
],
style: {
insetLeft: 1
}
})
Insert cell
Insert cell
g2({
type: "point",
data: countries,
encode: {
x: "GDP",
y: "LifeExpectancy",
color: "Continent",
size: "Population",
shape: "point"
},
scale: {
color: { range: ["red", "yellow", "blue", "black"] },
size: { type: "log", range: [4, 20] } // Specify scale type and range.
},
legend: { size: false },
style: { fillOpacity: 0.3, lineWidth: 1 }
})
Insert cell
Insert cell
g2({
type: "interval",
height: 640,
data: {
type: "fetch",
value:
"https://gw.alipayobjects.com/os/bmw-prod/79fd9317-d2af-4bc4-90fa-9d07357398fd.csv"
},
legend: false,
encode: { y: "value", color: "name" },
transform: [{ type: "stackY" }],
scale: {
color: {
palette: "spectral",
offset: (t) => t * 0.8 + 0.1
}
},
coordinate: { type: "theta" }, // Specify coordinates.
style: { stroke: "white" },
animate: { enterType: "waveIn", enterDuration: 1000 },
labels: [
{ text: "name", radius: 0.8, fontSize: 10, fontWeight: "bold" },
{
text: (d, i, data) => (i < data.length - 3 ? d.value : ""),
radius: 0.8,
fontSize: 9,
dy: 12
}
]
})
Insert cell
{
const position = [
"economy (mpg)",
"cylinders",
"displacement (cc)",
"power (hp)",
"weight (lb)",
"0-60 mph (s)",
"year"
];
return g2({
type: "line",
width: 800,
data: {
type: "fetch",
value: "https://assets.antv.antgroup.com/g2/cars3.json"
},
coordinate: { type: "parallel" }, // Specify series of coordinates.
encode: { position, color: "cylinders" },
style: {
strokeWidth: 1.5,
strokeOpacity: 0.4
},
scale: { color: { palette: "brBG", offset: (t) => 1 - t } },
axis: Object.fromEntries(
Array.from({ length: position.length }, (_, i) => [
`position${i === 0 ? "" : i}`,
{
zIndex: 1,
titlePosition: "right",
line: true,
labelStroke: "#fff",
labelStrokeWidth: 5,
labelFontSize: 10,
labelStrokeLineJoin: "round",
titleStroke: "#fff",
titleFontSize: 10,
titleStrokeWidth: 5,
titleStrokeLineJoin: "round",
titleTransform: "translate(-50%, 0) rotate(-90)",
lineStroke: "black",
tickStroke: "black",
lineStrokeWidth: 1
}
])
)
});
}
Insert cell
Insert cell
// A space composition example.
g2({
type: "spaceFlex",
width: 900,
ratio: [1, 2],
direction: "col",
data: {
type: "fetch",
value: "https://assets.antv.antgroup.com/g2/seattle-weather.json"
},
children: [
{
type: "interval",
paddingBottom: 0,
paddingRight: 300,
encode: {
x: (d) => new Date(d.date).getUTCDate(),
y: "temp_max",
color: "steelblue"
},
transform: [{ type: "groupX", y: "max" }],
axis: { x: false }
},
{
type: "spaceFlex",
paddingBottom: 60,
ratio: [2, 1],
children: [
{
type: "cell",
paddingBottom: 60,
paddingRight: 0,
encode: {
x: (d) => new Date(d.date).getUTCDate(),
y: (d) => new Date(d.date).getUTCMonth(),
color: "temp_max"
},
transform: [{ type: "group", color: "max" }],
scale: { color: { palette: "gnBu" } },
style: { inset: 0.5 },
axis: { x: { title: "Date" }, y: { title: "Month" } },
legend: { color: false }
},
{
type: "interval",
encode: {
x: (d) => new Date(d.date).getUTCMonth(),
y: "temp_max",
color: "steelblue"
},
transform: [{ type: "groupX", y: "max" }],
coordinate: { transform: [{ type: "transpose" }] },
axis: { x: false }
}
]
}
]
})
Insert cell
// A facet composition example.
g2({
type: "facetRect",
data: countries,
paddingLeft: 40,
paddingBottom: 40,
width,
height: 360,
encode: {
x: "Continent"
},
children: [
{
type: "point",
inset: 15,
encode: {
x: "GDP",
y: "LifeExpectancy",
size: "Population",
shape: "point"
},
scale: {
size: { type: "log", range: [4, 20] } // Specify scale type and range.
},
style: {
fillOpacity: 0.3,
lineWidth: 1
}
}
]
})
Insert cell
// A nested facet composition.
g2({
type: "facetRect",
height: 600,
paddingLeft: 85,
paddingBottom: 60,
shareSize: true,
data: {
type: "fetch",
value: "https://assets.antv.antgroup.com/g2/titanic.json",
transform: [
{ type: "sortBy", fields: ["survived", "sex"] },
{
type: "map",
callback: ({ survived, ...d }) => ({
...d,
survived: survived + ""
})
}
]
},
encode: { y: "pclass" },
children: [
{
type: "facetRect",
shareSize: true,
encode: { x: "survived" },
axis: {
y: false,
x: {
labelFormatter: (d) => (d === "1" ? "Yes" : "No"),
position: "bottom"
}
},
children: [
{
type: "facetRect",
shareSize: true,
encode: { y: "sex" },
axis: { x: false, y: { position: "left" } },
children: [
{
type: "point",
encode: { color: "survived", shape: "point", size: 3 },
transform: [{ type: "pack" }],
legend: {
color: { labelFormatter: (d) => (d === "1" ? "Yes" : "No") }
},
tooltip: { title: "", items: ["pclass", "survived", "sex"] }
}
]
}
]
}
]
})
Insert cell
Insert cell
Insert cell
g2({
type: "interval",
width: 720,
data: events,
coordinate: { transform: [{ type: "transpose" }] },
scale: {
enterDuration: {
zero: true,
range: [0, 10000]
}
},
encode: {
x: "name",
y: ["endTime", "startTime"],
color: "name",
enterDuration: (d) => d.endTime - d.startTime,
enterDelay: "startTime"
}
})
Insert cell
g2({
type: "interval",
data: fruits,
encode: {
x: "year",
y: "value",
color: "type",
series: "type",
enterDuration: 1000
},
transform: [{ type: "stackEnter", groupBy: ["x", "color"] }]
})
Insert cell
g2({
type: "timingKeyframe",
duration: 1000,
direction: "reverse",
iterationCount: 2,
children: [
{
type: "interval",
transform: [{ type: "groupX", y: "mean" }],
data: people,
encode: {
x: "gender",
y: "weight",
color: "gender",
key: "gender"
}
},
{
type: "point",
data: people,
encode: {
x: "height",
y: "weight",
color: "gender",
shape: "point",
groupKey: "gender"
}
}
]
})
Insert cell
Insert cell
g2({
type: "point",
data: {
type: "fetch",
value: "https://gw.alipayobjects.com/os/antvdemo/assets/data/scatter.json"
},
encode: { x: "weight", y: "height", color: "gender", shape: "point" },
style: {
fillOpacity: 0.7,
transform: "scale(1, 1)",
transformOrigin: "center center"
},
state: {
inactive: { fill: "black", fillOpacity: 0.5, transform: "scale(0.5, 0.5)" }
},
interaction: { brushXHighlight: {} }
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
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