Lessons/SVG Switches/Lesson 9

Minimal teal knob

Plain grey track with a solid teal knob. Only the knob position changes.

State logic:
Only the knob position changes.
Animation:
Knob cx slides 29 → 91.
Switch previewDemo
vSwitch = 0

This preview is not connected to a live Qlik app. In Qlik, a separate button action changes vSwitch between 0 and 1.

The Qlik expression

Paste into an object that displays a measure as an image.

Qlik expression
='data:image/svg+xml;utf8,' &
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 60" width="100%" height="100%" preserveAspectRatio="xMidYMid meet">' &
  '<desc>' & Num(Now(1)*86400000,'0') & '</desc>' &
  '<rect x="10" y="12" width="100" height="36" rx="18" fill="%23eceef0"/>' &
  '<circle cx="' & If($(vSwitch)=1,'91','29') & '" cy="30" r="16" fill="%2314b99f" stroke="%2310a88f" stroke-width="1.5">' &
    '<animate attributeName="cx" from="' & If($(vSwitch)=1,'29','91') & '" to="' & If($(vSwitch)=1,'91','29') & '" dur="0.2s" fill="freeze"/>' &
  '</circle>' &
'</svg>'

How the expression works

  1. 1
    <svg ... viewBox="0 0 120 60">

    The <svg> tag opens the image and viewBox="0 0 120 60" defines the 120 × 60 drawing area.

  2. 2
    <rect ... fill="%23eceef0"/>

    The track is plain and static — its fill has no If() and no <animate>, so it looks identical in both states.

  3. 3
    <circle ... fill="%2314b99f">

    The knob is the only moving part: a solid teal circle with a slightly darker outline.

  4. 4
    If($(vSwitch)=1,'91','29')

    The knob's cx reads your Qlik variable: 91 (right) when vSwitch is 1, 29 (left) when it is 0. Nothing else in the image changes.

  5. 5
    <animate attributeName="cx" ...>

    <animate> slides the knob between 29 and 91 over 0.2s on every click, so it glides instead of jumping.

  6. 6
    <desc>Num(Now(1)*86400000,'0')

    The hidden <desc> holds a fresh timestamp on every recalculation, so Qlik treats each click as a new image and the slide replays in both directions.

Qlik setup

vSwitch must be 0 (OFF) or 1 (ON).

1. Create the variable

Create vSwitch in Variables. If it has no initial value, set it to 0.

Variable
Name:        vSwitch
Definition:  0

2. Add a button action

Set variable value → vSwitch, with this value.

Button action value
=1-$(vSwitch)

3. Display the SVG expression as an image next to or beneath the button. The hidden timestamp makes the image fresh on every recalculation so any animation can replay in both directions.

Troubleshooting

The switch does not change state

Check that vSwitch starts at 0 and the button action sets vSwitch to =1-$(vSwitch). The SVG image itself does not change the Qlik variable.

The animation does not replay

Keep the hidden timestamp line in the copied expression and ensure the image recalculates when vSwitch changes. SVG animation support depends on your Qlik rendering context.