Extra/SVG Switches/Lesson 07

Switch 7: Blue active-state switch

A classic switch: a white circular knob on a fully rounded track. The key idea is the active state — the track colour itself changes with vSwitch, so ON is obvious at a glance.

Try the blue switchDemo
vSwitch = 0

This preview demonstrates the appearance only and is not connected to a live Qlik app. In Qlik, a separate button action must change vSwitch between 0 and 1.

The Qlik expression

Paste into a measure displayed as an image.

Qlik expression
='data:image/svg+xml;utf8,' &
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 90 50" width="100%" height="100%" preserveAspectRatio="xMidYMid meet">' &
  '<rect x="8" y="4" width="72" height="42" rx="21" fill="' & If($(vSwitch)=1,'%234052c5','%23e7eaf2') & '" stroke="%23d7dae1" stroke-width="2">' &
    '<animate attributeName="fill" from="' & If($(vSwitch)=1,'%23e7eaf2','%234052c5') & '" to="' & If($(vSwitch)=1,'%234052c5','%23e7eaf2') & '" dur="0.2s" fill="freeze"/>' &
  '</rect>' &
  '<circle cx="' & If($(vSwitch)=1,'59','26') & '" cy="25" r="17" fill="%23ffffff" stroke="%23c5c7cb" stroke-width="2.5">' &
    '<animate attributeName="cx" from="' & If($(vSwitch)=1,'26','59') & '" to="' & If($(vSwitch)=1,'59','26') & '" dur="0.2s" fill="freeze"/>' &
  '</circle>' &
'</svg>'

How the expression works

  1. 1

    Data URI and viewBox

    'data:image/svg+xml;utf8,' &
    '<svg ... viewBox="0 0 90 50" ...>'

    Inline SVG on a 90×50 grid that scales to fit the Qlik object.

  2. 2

    Rounded track

    <rect x="8" y="4" width="72" height="42" rx="21" ...>

    rx=21 is half the height (42), giving fully round ends.

  3. 3

    Conditional track colour

    If($(vSwitch)=1,'%234052c5','%23e7eaf2')

    Blue when vSwitch is 1, pale grey when 0. # is written as %23 so the data URI stays valid.

  4. 4

    Colour animation

    <animate attributeName="fill" from="..." to="..." dur="0.2s" fill="freeze"/>

    Fades the track from its previous colour to the new one. The from value is the opposite state, the to value is the current state.

  5. 5

    Circular knob

    <circle ... cy="25" r="17" fill="%23ffffff" stroke="%23c5c7cb">

    A white circle (r=17) centred vertically on the track.

  6. 6

    Conditional cx

    cx="' & If($(vSwitch)=1,'59','26') & '"

    59 puts the knob on the right (ON), 26 on the left (OFF).

  7. 7

    Optional animation

    <animate attributeName="cx" ... dur="0.2s" fill="freeze"/>

    Slides the knob from the old position to the new one whenever Qlik re-renders the image.

Qlik setup

Done in your Qlik app, separate from the preview above. vSwitch must be 0 (OFF) or 1 (ON).

1. Create the variable

In edit mode, open Variables and create vSwitch starting at 0.

Variable
Name:        vSwitch
Definition:  0

2. Add a button action

Button → Set variable value → vSwitch, with this value.

Button action value
=1-$(vSwitch)

3. Place the expression in an object that renders images (e.g. a Text & image measure shown as an image), next to or under the button.

Troubleshooting

The track colour doesn't change+

Both the fill attribute and the animate from/to values must use If($(vSwitch)=1, ...). If only one does, the colour may snap back.

The image is broken or blank+

Check every colour uses %23 instead of #, quotes are balanced, and the measure is shown as an image.

The switch never changes+

The SVG only draws the state. Confirm vSwitch is 0 or 1 and that a separate Qlik button action toggles it with =1-$(vSwitch).