Lessons/SVG Switches/Lesson 4

Filled capsule

Taller filled capsule with a white knob. Capsule fill swaps grey → teal.

State logic:
Capsule fill swaps grey → teal.
Animation:
Knob cx slides; capsule fill fades.
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="10" width="100" height="40" rx="20" fill="' & If($(vSwitch)=1,'%2314b99f','%23dfe3e2') & '">' &
    '<animate attributeName="fill" from="' & If($(vSwitch)=1,'%23dfe3e2','%2314b99f') & '" to="' & If($(vSwitch)=1,'%2314b99f','%23dfe3e2') & '" dur="0.2s" fill="freeze"/>' &
  '</rect>' &
  '<circle cx="' & If($(vSwitch)=1,'91','29') & '" cy="30" r="15" fill="%23ffffff" stroke="%23d6dedb" 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; viewBox="0 0 120 60" sets a 120 × 60 drawing area containing the whole switch.

  2. 2
    <rect ... width="100" height="40" rx="20">

    The capsule track is taller here (height 40) and rx="20" — half the height — makes it a fully rounded capsule.

  3. 3
    If($(vSwitch)=1,'%2314b99f','%23dfe3e2')

    The capsule's fill reads your Qlik variable: teal when vSwitch is 1, grey when it is 0. Colours use %23 because # would break the data-URI address.

  4. 4
    <circle ... fill="%23ffffff">

    The <circle> is the white knob that sits on top of the capsule.

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

    Two <animate> tags fire on each click: the knob's cx slides 29 → 91 and back while the capsule's fill fades between its two colours.

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

    The hidden <desc> holds a fresh timestamp on every recalculation, so Qlik reloads the image each click and the animation 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.