plot1 = {
let w = 800;
let h = (2.1 * w) / 7;
let x_ticks = d3.range(-2 * Math.PI, 7, Math.PI / 2).filter((x) => x != 0);
let x_scale = d3.scaleLinear().domain([-7, 7]).range([0, w]);
let plot = Plot.plot({
width: w,
height: h,
margin: 0,
x: { domain: [-7, 7] },
y: { domain: [-2.1, 2.1] },
marks: [
Plot.axisX({
y: 0,
ticks: x_ticks,
tickFormat: (x) => {
const n = Math.round((2 * x) / Math.PI);
return String.raw`${n == 1 ? "" : n == -1 ? "-" : n}\pi`;
},
render(index, scales, values, dimensions, context, next) {
const g = d3.create("svg:g", context.document);
const { x: X, y: Y, text: T } = values;
if (T) {
g.selectAll()
.data(index)
.enter()
.append("g")
.attr(
"transform",
(i) => `translate(${X[i]},${10 + Y[i]}) scale(1.2)`
)
.append((i) => MathJax.tex2svg(T[i]).querySelector("svg"));
return g.node();
}
else return next(index, scales, values, dimensions, context);
}
}),
Plot.axisY({
x: 0,
ticks: [-2, -1, 1, 2],
tickFormat: d3.format("d")
}),
Plot.ruleX([0]),
Plot.ruleY([0]),
Plot.line(
d3.range(-7, 7, 0.01).map((x) => [x, Math.sin(x)]),
{ strokeWidth: 3 }
)
]
});
return plot;
}