Lessons/SVG Switches/Lesson 8

Arrow slider

Filled track with an arrow and a white knob. Track turns teal and the arrow turns white.

State logic:
Track turns teal and the arrow turns white.
Animation:
Knob cx slides; track 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="11" width="100" height="38" rx="19" fill="' & If($(vSwitch)=1,'%2314b99f','%23e8e9ec') & '">' &
    '<animate attributeName="fill" from="' & If($(vSwitch)=1,'%23e8e9ec','%2314b99f') & '" to="' & If($(vSwitch)=1,'%2314b99f','%23e8e9ec') & '" dur="0.2s" fill="freeze"/>' &
  '</rect>' &
  '<path d="M35 30h31m-8-8l8 8-8 8" fill="none" stroke="' & If($(vSwitch)=1,'%23ffffff','%237b8480') & '" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>' &
  '<circle cx="' & If($(vSwitch)=1,'91','29') & '" cy="30" r="14" fill="%23ffffff" stroke="%23d8dedb" 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 the 120 × 60 drawing area containing the whole switch.

  2. 2
    <rect ... rx="19" ...>

    The track is a pill (rx="19") whose fill swaps grey → teal with the state, driven by the If() inside it.

  3. 3
    <path d="M35 30h31m-8-8l8 8-8 8" ...>

    The <path> draws the arrow. Its stroke reads your Qlik variable: white on teal when vSwitch is 1, grey when OFF. Colours use %23 because # would break the data-URI address.

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

    The <circle> is the white knob that slides along the track.

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

    Two <animate> tags run per click: the knob's cx slides 29 → 91 and back while the track's fill fades between its two colours.

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

    The hidden <desc> gets a fresh timestamp each recalculation, so Qlik reloads the image on every click and the animation replays both ways.

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.