Public
Edited
Jan 6, 2024
1 fork
5 stars
Insert cell
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
g2({
type: "point",
data, // Bind abstract dataset for the point mark.
encode: {
x: "GDP", // X-position encodes GDP column.
y: "LifeExpectancy", // Y-position encodes LifeExpectancy column.
color: "Continent", // Color encodes column derived from Population column.
shape: "hollow" // Shape encodes constant hollowPoint.
},
style: { lineWidth: 2 } // LineWidth fixed to 2.
})
Insert cell
Insert cell
g2({
type: "point",
data,
encode: {
x: "GDP",
y: "LifeExpectancy",
color: (d) => (d.Population > 600e6 ? true : false),
shape: "hollow"
},
style: { lineWidth: 2 },
axis: { x: { labelFormatter: "~s" } }, // Format labels of axisX.
legend: { color: { labelFormatter: (d) => (d ? "High" : "Low") } }
})
Insert cell
Insert cell
g2({
type: "interval",
data,
encode: { x: "Continent" },
transform: [{ type: "groupX", y: "count" }],
axis: { x: { title: "country" } }
})
Insert cell
Insert cell
Insert cell
Insert cell
g2({
type: "view",
data,
children: [
{
type: "point",
encode: { x: "GDP", y: "LifeExpectancy", color: "Continent" },
style: { fillOpacity: 0.3, lineWidth: 1 }
},
{
type: "text",
encode: {
series: "Continent",
size: "Population",
text: "Country",
x: "GDP",
y: "LifeExpectancy"
},
// Select a shape for each series with specified selector.
transform: [{ type: "select", x: selector }],
style: {
fontSize: 14,
fill: "black",
stroke: "none",
fontWeight: "bold",
dy: -11
}
}
]
})
Insert cell
Insert cell
Insert cell
g2({
type: "point",
data,
encode: {
x: "GDP",
y: "LifeExpectancy",
color: "Continent",
size: "Population",
shape: "point"
},
scale: { size: { type: scale, range: [4, 20] } },
legend: { size: false },
style: { fillOpacity: 0.3, lineWidth: 1 }
})
Insert cell
Insert cell
Insert cell
g2({
type: "interval",
data,
encode: { x: "Continent" },
transform: [{ type: "groupX", y: "count" }],
coordinate: { transform: [{ type: "transpose" }] } // Apply coordinate transformation
})
Insert cell
Insert cell
{
const axisOptions = {
zIndex: 1,
line: true,
titleTransform: "translate(-50%, 0) rotate(-90)",
titlePosition: "right"
};
return g2({
type: "line",
paddingLeft: 40,
data,
encode: {
// Position encode multiple data columns, each columns corresponding an axis.
position: ["Continent", "LifeExpectancy", "Population", "GDP"],
color: "Continent"
},
coordinate: { type: "parallel" }, // Specify parallel coordinate transformation.
style: { strokeOpacity: 0.8 },
axis: {
position: axisOptions, // Set the first axis to top.
position1: axisOptions, // Set the second axis to top.
position2: { ...axisOptions, labelFormatter: "~s" }, // Set the third axis to the top and format it.
position3: { ...axisOptions, labelFormatter: "~s" } // Set the fourth axis to the top and format it.
}
});
}
Insert cell
Insert cell
g2({
type: "spaceFlex", // Create a flex node and add to chart.
data, // Bind data and pass it to child.
width: 840,
height: 320,
children: [
{
type: "point", // Create a view with a point mark and add it to the flex node.
encode: { x: "GDP", y: "LifeExpectancy" },
axis: { x: { labelFormatter: "~s" } }
},
{
type: "interval", // Create a view with a interval mark and add it to the flex node.
encode: { x: "Continent" },
transform: [{ type: "groupX", y: "count" }]
}
]
})
Insert cell
Insert cell
g2({
type: "facetRect",
width: 840,
height: 290,
paddingLeft: 40,
paddingBottom: 40,
data, // Data will be divided and pass.
encode: { x: "Continent" }, // Data is grouped by Continent column and space is placed horizontally.
children: [
{
type: "point",
encode: { x: "GDP", y: "LifeExpectancy" },
axis: { x: { labelFormatter: "~s" } }
}
]
})
Insert cell
Insert cell
Insert cell
Insert cell
g2({
type: "facetRect",
width: 700,
height: 280,
data,
shareSize: +shareSize,
shareData: +shareData,
encode: { x: "Continent" },
scale: { x: { domain: ["Europe", "Americas", "Asia", "Oceania"] } },
children: [
{
type: "point",
encode: {
title: "Country",
shape: "point",
tooltip: ["LifeExpectancy", "GDP", "Population"]
},
transform: [{ type: "pack" }]
}
]
})
Insert cell
Insert cell
Insert cell
g2({
type: "point",
data,
encode: {
x: "GDP",
y: "LifeExpectancy",
size: "Population",
color: "Continent",
shape: "point",
enterDuration: 1000
},
transform: [{ type: "stackEnter", groupBy: ["color"] }],
scale: { size: { type: "log", range: [4, 20] } },
legend: { size: false },
style: { fillOpacity: 0.3, lineWidth: 1 }
})
Insert cell
Insert cell
g2({
type: "timingKeyframe",
direction: "alternate",
iterationCount: "infinite",
children: [
{
type: "point",
class: "point",
paddingLeft: 50,
data,
encode: {
x: "GDP",
y: "LifeExpectancy",
color: "Continent",
key: "Country",
shape: "point",
groupKey: "Continent"
},
legend: false
},
{
type: "interval",
class: "point",
paddingLeft: 50,
data,
encode: { x: "Continent", key: "Continent", color: "Continent" },
transform: [{ type: "groupX", y: "count" }],
axis: { y: { title: "count" } },
legend: false
}
]
})
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: true }
})
Insert cell
g2({
type: "point",
data,
encode: {
x: "GDP",
y: "LifeExpectancy",
size: "Population",
color: "Continent",
shape: "point"
},
scale: { size: { type: "log", range: [4, 20] } },
legend: { size: false },
style: { fillOpacity: 0.3, lineWidth: 1 },
interaction: { fisheye: true }
})
Insert cell
Insert cell
Insert cell
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