Expressions involving parameters may be used anywhere in KGJS. For example, an attribute can show the sum of two params:
The value of $a$ is ${params.a}.
The value of $b$ is ${params.b.toFixed(1)}.
Let's add $a$ and $b$ together: $$ ${params.a} + ${params.b.toFixed(1)} = ${(params.a + params.b).toFixed(1)}$$
`" sidebar: controls: - title: Adding Parameters sliders: - {param: a, label: a} - {param: b, label: b}However, it’s better to use a calculation to pre-calculate the summed value. A calculation is a function that takes in parameters, written in the mathjs format. We can create calculations like this:
The value of $a$ is ${params.a}.
The value of $b$ is ${params.b.toFixed(1)}.
Let's add $a$ and $b$ together: $$ ${params.a} + ${params.b.toFixed(1)} = ${calcs.sumAB.toFixed(1)}$$
`" sidebar: controls: - title: Adding Parameters sliders: - {param: a, label: a} - {param: b, label: b}Note that calculations work best when all terms are surrounded by parentheses. For example, ((params.a)^(params.b))
is more robust than params.a^params.b
. Without parentheses, the expression may be evaluated incorrectly.
Calcs can exist to a depth of 5:
$b = 2a = ${calcs.b}$
$c = 2b = ${calcs.c}$
$d = 2c = ${calcs.d}$
$e = 2d = ${calcs.e}$
`" sidebar: controls: - title: Parameters sliders: - {param: a, label: a}Taking a closer look at the code structure using calculations, notice that the HTML text is surrounded by the ` symbols.