How to make a 2D Interactive Solar System in Qlik Sense
Robert Svebeck
Driving Responsible AI Implementation in Region Stockholm / Karolinska University Hospital
Here is just a super quick guide on how to create a simple animated and interactive 2D Solar System using Qlik Standard Map Object.
This may not be particularly useful for your every day life as a BI developer.... but I hope this inspires you to to see beyond what is possible in Standard Qlik Sense.
In this example I am using Qlik Sense November Release.
Script
The only default setting you need to define is to set . as decimal separator:
SET DecimalSep='.';
Then we need some data - using INLINE load to keep things simple. Feel free to add more columns. And Yes... I know the sun is not a planet...
Objects: LOAD * INLINE [ PlanetNo, Planet, Radius, Velocity, OrbitRadius, Color 0,sun, 35, 0, 0, #ffe000 1,mercury, 5, 6, 65, #02a3e4 2,venus, 10, 5, 90, #e09343 3,earth, 15, 4, 125, #52c3e0 4,mars, 20, 3.5, 175, #ff0000 5,jupiter, 25, 3, 225, #af47e0 6,saturn, 20, 2.25, 275, #e0c05c 7,uranus, 15, 2, 325, #a6c7e0 8,neptune, 25, 1.5, 375, #dd4466 ];
- Radius = Size of planet
- Velocity = How fast the planet rotates around the sun (hence Sun = 0)
- OrbitRadius = How far away the planet rotates around the Sun (hence Sun = 0)
- Color = Just the color of the planet.
After this, we create a new empty table called rotation. We will fill it with data soon.
rotation:load * INLINE [PlanetNo];
Now we start the first for loop. Calculating the rotation for one planet at a time. We start with planet no 0 and finish with planet no 8
for vPlanetNo = 0 to 8
We define vPlanetPosition with a random value between 0 and 360. This will generate a random starting position for each planet:
Let vPlanetPosition = round(Rand()*360,1);
Then we need to fetch the planets rotation parameters (velocity and orbit radius) from the previously created Objects table, and save them as new variables:
Let vVelocity = peek('Velocity',vPlanetNo,'Objects'); Let vOrbitRadius = peek('OrbitRadius',vPlanetNo,'Objects');
Now we start to calculate each rotation frame (or animation frame) by adding another for loop. In this case, we will create 90 rotation frames.
for vRotationNo = 1 to 90
Now we can calculate where the planet should be plotted:
Let vPlanetPosition = vPlanetPosition + vVelocity/100; Let vX = cos(vPlanetPosition) * vOrbitRadius; Let vY = sin(vPlanetPosition) * vOrbitRadius;
As you see, just a simple cos for x and sin for y, multiplied with the planet radius.
The vVelocity/100 is just to make each animation step smaller. The higher value here, the bigger step between two frames. If you want to make a super smooth animation, make each step much smaller and increase the number of rotations from 90 to 900... you will reach a point where perhaps you see that you have too much data.
Then we save this position in our rotation table.
Concatenate(rotation) LOAD $(vRotationNo) & '|' & $(vPlanetNo) as RotationKey, $(vRotationNo) as RotationNo, $(vPlanetNo) as PlanetNo, $(vX) as long, $(vY) as lat autogenerate(1);
The field RotationKey will be used later to visualize each planets "expected route".
The fields long and lat will be used as location for the map object.
We are done. Just end the two for loops:
next next
Reload your app. Your data model should look like this;
Sheet design.
Follow these steps:
- Create a map object
- In Map Settings: Set Base map = "None"
- In Map Settings: Set Projection = "User defined (meters)"
- In Layers: Add Point Layer
- Point Layer: Data: Dimension = Planet
- Point Layer: Location: lat as Latitude, long as Longitude
- Point Layer: Size & Shape: Size by Radius
- Point Layer: Colors: Deselect "Auto"
- Point Layer: Colors: "By expression": Expression = Color
Exit the layer, and create another layer:
- Add another Point Layer
- Point Layer: Data: Dimension = RotationKey
- Point Layer: Location: only({1} lat) as Latitude, only({1} long) as Longitude (The set only({1}...) part enables this map layer to show all animation steps at once)
- Point Layer: Size & Shape: Set as small bubble size as possible. (We want this layer to show the total circle path for each planet, so the smaller points the better)
- Point Layer: Colors: Set transparency to middle, outline color to 100% transparency. (Again, the path should be barely visible)
Exit this point layer and go back to Map Settings
- In Map Settings: De-select "Auto Zoom" and select "Set default view". (This way we look at the solar system from the same viewpoint all the time, regardless of where the planets are. Avoiding auto zoom.)
Create a new animation object (available in the Qlik Dashboard Bundle)
- Dimension: RotationNo
- Animator Options: Time Between Steps: 300
You are done. Press Play!
Further developments that you can do to check if you have understood what is going on: Add the Earth moon. It should rotate around the Earth and not the Sun, but apart from that the code to add a moon is just the same math.
Thanks for reading, contact me here on LinkedIn, or on Twitter if you want to have a copy of this app.
In my series on dataonthe.rocks - I currently write about how to make 2D things turn 3D, and vice versa. Maybe I will see you there?
Robert Svebeck
Hello... May you please share the application with me.? Thanks, Yanela (South Africa)?
Data Engineering, Data Governance and Analytics
3 年this is fun!
Qlik Partner Ambassador | MBA Gest?o de Projetos | Os melhores insights com Qlik e Alfabetiza??o de Dados
3 年Haha so original and funtastic!