Creating dynamic charts in Qlik Sense®

To increase the flexibility of a single chart object, you can set it up so that the dimension used is based on what the user wants to see. This is a much more efficient use of space for single sheets and makes the whole experience much more dynamic.

Getting ready

For the purpose of this recipe, we will make use of the sales information for different fruits as defined in the script:

  1. Create a new Qlik Sense application and call it QS_DynamicCharts.
  2. Load the following data into the data load editor:
    Transactions:
    Load 
     Mod(IterNo(),26)+1 AS Period,
     Pick(Ceil(3*Rand()),'Standard','Premium','Discount') AS ProductType,
     Pick(Ceil(6*Rand()),'Apple','Orange','Cherry','Plum','Fig', 'Pear') AS Category,
     Pick(Ceil(3*Rand()),'Heavy','Medium','Light') AS Weight,
     Pick(Ceil(2*Rand()),'2013','2014') AS Year, 
     Round(1000*Rand()*Rand()*Rand()) AS Sales
    Autogenerate 20
    While Rand()<=0.5 or IterNo()=1;
    
    SET vDimension = 'GetFieldSelections(Dimensions)';
    
    Dimensions:
    LOAD * INLINE [
        Dimensions
        Weight
        ProductType
        Category
        Period
    ];

How to do it…

  1. From the App overview, create a new sheet and enter the Edit mode.
  2. Add the field Dimensions onto the main content pane.
  3. Drag a bar chart object onto the content pane.
  4. Add the following expression as a measure:
    Sum(Sales)
  5. Add a following calculated dimension by clicking on the How to do it… button:
    = [' & Pick(Match($(vDimension),'Weight','ProductType', 'Category'),Weight,ProductType,Category) & ']'
  6. Enter one click of the space bar as the label to make it appear as if there is no dimension label.
  7. Exit the editor mode and select a value in the Dimensions field.
  8. The final product should resemble the following screenshot:
    How to do it…

How it works…

In the script loaded at the beginning of the recipe, we set a variable called vDimension. The GetFieldSelections() function will return the values selected in the field we specify inside the brackets GetFieldSelections (Dimensions). The Dimensions field is simply a hardcoded list of specific fields in the Data model. The code we wrote in the dimension field of the chart uses this variable value to set the dimension dynamically to whatever value the user picks in the list box we created.

There's More….

If you are running Qlik Sense 2.1.1, then you can now enter expressions in the chart title area. If so, enter the following code:

='Showing Sales by: ' & $(vDimension)

This displays the selected dimension in the chart title dynamically. If you are running an earlier version of Qlik Sense, simply leave the label blank. After this, I would suggest creating a textbox explaining the chart, which will display whatever the value is, in the associated list box. You can also use the preceding expression in the explanation textbox.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset