Skip to content

The graph ​

The parameters of the graph say what the drawing shows: the part of the plane, the size of a unit, the axes, the grid, and the defaults of the figures. They are the parameters field of PiDraw, and what refreshLayout reads. They are separated by commas, and all of them are optional.

pidraw
A(1,2)->label
B(-2,-1)->label
ParameterWhat it sets
x, y, boxthe part of the plane that is drawn
unitX, unitYthe length of a unit
unit, scalethe unit and the scale of the lengths that the labels write
axis, axisX, axisY, axisXname, axisYnamethe axes
grid, gridX, gridYthe grid
default.label, default.tex, default.pointswhat the figures have by default

The window ​

ParameterDescription
x=min:max, y=min:maxthe part of the plane that is drawn, in units (-8:8 by default). A bound can be written with pi: x=-2pi:2pi
x=6, y=4for x=-6:6, y=-4:4
box=5for x=-5:5,y=-5:5
box=8;6for x=-8:8,y=-6:6
box=-3:7the same interval for both axes

x and y are more precise than box: they set what box leaves.

pidraw
f(x)=sin(x)->red
g(x)=cos(x)->blue
pidraw
A(5,4)->label
B(-5,-3)->label
pidraw
A(6,2)->label

When the origin is outside the window (x=2:10), the axis that would be outside is drawn at the nearest edge, with its numbers toward the inside. The ticks are still the ones of the plane (2, 3, 4...). This is done for orthogonal axes only.

pidraw
A(5,1)->label

The size of a unit ​

ParameterDescription
unitX=2, unitY=3the length of a unit of each axis, in centimeters (1 by default). unitX alone is the unit of both axes, unitY alone leaves unitX at 1

The drawing has its real size: dx × unitX centimeters wide and dy × unitY centimeters high, where dx and dy are the lengths of the two intervals. It does not depend on the page. It is shown at that size, or reduced to the width of the page when it is wider. The window can change without a unit, the width of a stroke or the size of a point changing. In the TikZ export, a unit is exactly unitX centimeters.

js
const draw = new PiDraw(container, {parameters: 'x=-5:5,y=-2:2'})

// 1 cm is about 37.9 pixels (a pixel is 0.75 point of TeX): the window is 10 cm wide and 4 cm high
Math.round(draw.graph.config.width) // 379
Math.round(draw.graph.config.height) // 152
pidraw
A(2,1)->label

The unit of the lengths ​

ParameterDescription
unit=mmthe symbol written after the lengths and the areas of the labels: pm, nm, µm, mm, cm, dm, m, dam, hm, km, or u for a unit that is not a real length. None by default: the lengths are written without a unit
scale=10, scale=1/10how long the lengths of the labels are compared with the units of the graph: a length is multiplied by scale, an area by its square (1 by default)

They change nothing of the drawing, of the axes or of the coordinates of the points: they are for the values that a label shows (@length, @radius, @perimeter, @area). A unit does not give a scale, and a scale does not give a unit: scale=1/10 alone corrects a circle drawn with a radius of 10 units, and unit=mm alone writes the lengths as they are, in millimeters. An angle always has its degree sign, and a slope or the coordinates of a point have no unit.

The area under a curve (fill, riemann) has no unit: the vertical axis of a function is not always a length.

pidraw
A(0,0)
B(4,3)
s=seg A,B->label=@
c=circ A,2->label=@radius
js
const draw = new PiDraw(container, {
    code: 'O(0,0)\nc=circ O,2->label=@radius\nA(1,1)\nB(4,1)\nC(1,4)\na=arc B,A,C->label=@',
    parameters: 'unit=mm,scale=10'
})
const {c, a} = draw.graph.figures

c.label.displayName // 20 mm
a.label.displayName // 90°

With scale=10, a unit of the graph is worth 10 millimeters: the lengths are multiplied by 10 and the areas by 100.

pidraw
A(0,0)
B(4,0)
C(4,3)
s=seg A,C->label=@
p=poly A,B,C->label=@area,fill=orange/0.3

Axes ​

The axes of the graph are called Ox and Oy. They are lines like the others, shown or not, and any figure can be built on them (P=inter d,Ox, H=proj A,Oy, p=perp Ox,A, c=circ O,Ox, b=bis Ox,d). These two names are reserved: a figure cannot have them.

ParameterDescription
axisshows the axes Ox and Oy, with an arrow at their end, 20 pixels from the edges. Without it they exist but are not drawn
axisX, axisYshows only this axis
axis=1, axis=0.5, axis=pi, axis=pi/3graduates the axes: a tick and a number every step units, from 0. With a step in π, the x axis writes fractions of π (π/2, 3π/2, -π) and the y axis keeps a step of 1. axisX=... and axisY=... set one axis
axisXname=x, axisYname=ythe name written at the end of the axis, on the side without numbers. The axis is shown
pidraw
A(1,1)->label
pidraw
A(1,1)->label
pidraw
f(x)=sin(x)->red
pidraw
A(1,1)

The numbers of the x axis are under the axis, the ones of the y axis on its left, without the 0 (it is where the axes cross). Ticks that are less than 4 pixels apart are not drawn, and when they are less than 36 pixels apart, a number every two, three... is written, so that they do not touch. No tick is drawn against the arrow. The numbers and the name have the color of the axis.

The options of an axis ​

An entry of the graph that draws something (axis, axisX, axisY, axisXname, axisYname, grid, gridX, gridY) can have options after ->, the same as the ones of a figure: color, width, dashes... The commas that follow are the ones of its options, up to a double comma ,, (or to the end):

pidraw
A(1,1)

Here axis has the options red and thick, grid=2 has gray, and unitX=0.9 is an entry. The options of axis are given to both axes, the ones of axisX or axisY are added to its own for one axis.

Four options are for the graduation and the extent of an axis: ticks, numbers, padding and range. They are the ones of the graduated line axis, and so are name and side.

pidraw
A(2,1)
pidraw
A(10,1)
pidraw
A(2,1)

range limits the part of an axis that is drawn, in units from its zero: range=-2:3 draws the axis from -2 to 3, and range=3 is -3:3. The arrow is at the end of the range. The axis is also cut by the window and by padding. The range is written after ->, and the ,, ends the options, so that the next entry (axisX here) is not one of them:

pidraw
A(2,1)

Grid ​

ParameterDescription
gridthe grid: one line per unit on each axis
grid=0.5a step of 0.5 on both axes
grid=pi, grid=2pi, grid=pi/3a step of π/2, 2π/2, π/3 on the x axis (the denominator is the parameter, 2 by default), and of 1 on the y axis
gridX=2, gridY=0.5the step of one axis. The other one keeps the value of grid, or 1

Lines of a grid that would be less than 3 pixels apart are not drawn: a grid that is too fine for the scale.

pidraw
A(1,1)
pidraw
A(1,1)

The option of the grid is subgrid: each step is cut in 2 parts (subgrid), or in n (subgrid=4, an integer from 2 to 20), with lines of the same color, twice as light.

pidraw
f(x)=sin(x)->red

Defaults of the figures ​

ParameterDescription
default.labelthe points have a label: their name
default.texthe labels of the points are written in TeX
default.points=nothe points are not drawn
default.points=o, s, *, o/12the shape of the points (circle, square, crosshair) and their size

A label or a shape that a point asks for itself wins.

pidraw
A(1,1)
B(3,2)
C(5,1)->o=10
pidraw
A(1,1)
B(3,2)
C(5,1)->label=C_1
pidraw
A(1,1)
B(3,2)
s=seg A,B
pidraw
A(1,1)
B(3,2)

Mistakes ​

A parameter that does not exist, or that does not exist any more, and a value that is not of the expected form, are an error. The graph then keeps the parameters it had, and the error is in layoutError. The message says what replaces a name that was removed.

js
const draw = new PiDraw(container, {parameters: 'ppu=3'})

draw.layoutError.message // The parameter "ppu" does not exist any more: use unitX and unitY, the length of a unit in cm (unitX=1.5)
draw.refreshLayout('label').message // The parameter "label" does not exist any more: use default.label
draw.refreshLayout('x=abc').message // Cannot read the parameter "x": "abc" is not a number, or an expression of numbers like 2pi
draw.refreshLayout('x=-4:4,axis') // null
draw.layoutError // null

Released under the MIT License.