Calculations


Expressions involving parameters may be used anywhere in KGJS. For example, an attribute can show the sum of two params:

params: - {name: a, value: 3, min: 0, max: 10} - {name: b, value: 0.5, min: 0, max: 1, round: 0.1} layout: HTMLPlusSidebarLayout: html: "`

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:

params: - {name: a, value: 3, min: 0, max: 10} - {name: b, value: 0.5, min: 0, max: 1, round: 0.1} calcs: sumAB: (params.a + params.b) layout: HTMLPlusSidebarLayout: html: "`

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:

params: - {name: a, value: 3, min: 0, max: 10} calcs: b: (2 * params.a) c: (2 * calcs.b) d: (2 * calcs.c) e: (2 * calcs.d) layout: HTMLPlusSidebarLayout: html: "`

$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.

KGJS
KineticGraphs.org
Getting Started

About the Project

Authoring Sandbox

Graphs & Layouts

Layouts

Sidebars

Graphs

Trees

3D Graphs

Graph Objects

Styling Graph Objects

Points

Lines

Curves

Contours

Shapes and Areas

Segments and Arrows

Angles and Angle Markers

User Interactions

Parameters

Calculations

Dragging

Economics

Econ Schema

Supply and Demand

Consumer Optimization

Exchange