Tags and plots
Two more walk-throughs, carrying on from Getting
started. First, 3D in plain HTML using the <phoria-view> tag, then plotting
maths functions - surfaces that move, contour maps, shapes and fields. As before, every step is a
complete program running beside its code, which you can copy as one HTML file or open in CodePen.
Allow about fifteen minutes.
Part 4: Plotting functions
Maths Is Beautiful! As well as rows of data, charts can plot maths functions. You give the chart a function,
fn, and the ranges to sample it over, and it does the rest - you get the axes, colours,
tooltips and smooth transitions like any other chart.
1. A surface from a function
Here fn(x, z) works out a height for each point across x and
z. The surface is coloured by height unless you tell it otherwise, and you can hover over
it to read off a point.
How finely the function is sampled is set by samples. 48 is the default unless you
change it - go higher for a sharper surface, or lower if you need it to be faster.
2. A parameter and a slider
Anything in params is passed to your function along with the point. When you change
them with plot.update({ params }), the surface smoothly changes to its new shape. Giving
y a fixed range keeps the axis still, so you see the surface itself move rather than the
axis rescaling to fit it.
3. Moving with time
With time: true, your function is also given the chart's clock as t, in
seconds, and the surface is worked out again every frame. Only the surface changes - the axes, scales
and legend stay as they are, which is why y has a fixed range. Here two ripples spread out
and meet: where two crests meet they add together, and where a crest meets a trough they cancel
out.
4. Contour lines
Contour lines join up points of equal height, like the ones on a walker's map. Give
contours the number of lines you want, or the exact heights to draw them at, and
place puts them on the surface, on the floor underneath it, or both.
5. Shapes that fold back
A height on its own can't make a shape like a ring, but a parametric surface can. Its
fn(u, v) returns a whole point, [x, y, z], for each pair of u
and v values. A curve works the same way with a single parameter,
t. Both use equal units on every axis so a torus stays nice and round, and giving both
charts the same x, y and z ranges puts them in the same box,
so the knot sits neatly on the ring.
6. A field of arrows
mark: 'vector' draws an arrow at each sample, pointing the way your function says,
with its length and colour showing how strong it is there. frame: 'origin' swaps the
chart's usual walls for axes through zero with arrowheads, like you'd draw on a blackboard.