Public
Edited
May 24, 2024
2 stars
Insert cell
Insert cell
Insert cell
{
const chart = new G2.Chart({
width: 800,
height: 600
});

function isInfinity(v) {
return v === Infinity || v === -Infinity;
}

chart.data(DATA.filter((d) => !isInfinity(d.x) && !isInfinity(d.y)));

chart
.line()
.encode("x", "x")
.encode("y", "y")
.encode("shape", "smooth")
.style("stroke", "#" + Math.floor(Math.random() * 16777215).toString(16))
.style("lineWidth", 2)
.scale("x", { nice: true })
.scale("y", { nice: true })
.axis("x", { title: false })
.axis("y", { title: false })
.tooltip({
title: null,
items: [
{ name: "x", field: "x", valueFormatter: (y) => y.toFixed(2) },
{ name: "y", field: "y", valueFormatter: (y) => y.toFixed(2) }
]
});

chart
.lineY()
.data([0])
.style("stroke", "grey")
.style("lineDash", [3, 3])
.style("arrow", true);

chart
.lineX()
.data([0])
.style("stroke", "grey")
.style("lineDash", [3, 3])
.style("arrow", true);

await chart.render();

return chart.getContainer();
}
Insert cell
DATA = generateData(formula, -5, 5, 500)
Insert cell
function generateData(formula, start, end, count) {
const xArr = generateX(start, end, count);

return xArr.map((x) => {
return { x, y: getY(formula, x) };
});
}
Insert cell
function getY(formula, x) {
return eval(formula);
}
Insert cell
function generateX(start, end, count) {
return [
...new Array(count)
.fill(0)
.map((_, i) => start + ((end - start) / count) * i)
];
}
Insert cell
Three3D = require("https://unpkg.com/@antv/g2-extension-3d")
Insert cell
GWebGL = require("https://unpkg.com/@antv/g-webgl")
Insert cell
G2 = require("https://unpkg.com/@antv/g2")
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