useData
The hook for returning data for use in visualisations

Overview

The useData hook returns data from the engine for either KPIs or visualisations. For dimensions and measures to build charts, use the cols input and for expressions to build KPIs use the metrics input.

Basic Example

The below example returns data to the console for a number of Qlik dimensions and expressions.
1
2
const cols = [{
3
qField: "[OrderDateMonth]",
4
qLabel: "Order Date",
5
},
6
{
7
qField: "[Country",
8
qLabel: "Body Location",
9
},
10
{
11
qField: "=Sum(Price*Quantity)",
12
qLabel: "price",
13
qFillStyle: "orange",
14
}]
15
16
const {
17
dataSet,
18
} = useData({
19
cols,
20
qTitle,
21
});
22
23
console.log(dataSet)
24
25
// ...
Copied!

KPI Example

See below for an example of how to get data from the engine to build a KPI. Here we are creating storing the output from two Qlik expressions into a variable called metrics. You can pass in expressions or variables to the hook.
1
// .....
2
3
const qMetrics = [{
4
qName: "KPI 1",
5
qExpr: "=$(vQuantity)",
6
qType: "qStringExpression"
7
},
8
{
9
qName: "KPI 2",
10
qExpr: "=Sum(Price*Quantity)",
11
qType: "qStringExpression"
12
}]
13
14
const {
15
metrics,
16
} = useData({
17
qMetrics,
18
});
19
20
console.log('metric: ',metrics)
21
22
//...
Copied!

Master Items Example

If you are using Master Items from your Qlik Sense app, your cols config will be looking slightly different. See below for an example
1
import React from 'react'
2
import { useData } from '@motor-js/engine'
3
4
const MasterItemsExample = () => {
5
6
const cols = [
7
{
8
qField: "[OrderDateMonth]",
9
qLabel: "Order Date",
10
},
11
{
12
qField: "",
13
qLibraryId: "dWHamW",
14
qType: "measure",
15
}];
16
17
const { dataSet, headerGroup } = useData({
18
cols,
19
});
20
21
//....
Copied!

Data Options

The following options can be passed into the hook
  • cols: Array
    • Optional
    • The columns in your Qlik application you wish to extract data for
    • Can be an array of strings or an array of objects
    • See Cols Options for the object definition
  • qPage: Object
    • Optional
    • Must be an object in the following structure: { qTop: num, qLeft: num, qWidth: num, qHeight: num }
    • Size of the extracted data from the engine
  • qTitle: string
    • Title of your visualisation
  • qSubtitle: string
    • Subtitle
  • qMetrics: array<object>
    • An array of Metric objects
    • Expressions to calculate KPI values
    • See Metric Options for the object definition

Cols Options

  • qField: <string> | <array>
    • Required
    • Dimension or Measure name from your Qlik Sense data model
    • Pass an array to use a drill down group
  • qLibraryId: <string>
    • Use to refer to a Master Item created in your app
    • Must refer to the id of your Master Item
  • qType: <"dimension" | "measure">
    • Use along with qLibraryId, if you are using Master Items from your Qlik Sense data model
    • Required to label your qLibraryId as a dimension or measure
  • qLabel: <string>
    • Label of your dimension / measure. This will be available in the headerGroup object to use as your table's title
  • qGrouping: <oneOf>
    • N or GRP_NX_NONE
    • H or GRP_NX_HIEARCHY
    • C or GRP_NX_COLLECTION
    • Use H for a drill down group
  • qNumType: <oneOf>
    • U or UNKNOWN
    • A or ASCII
    • I or INTEGER
    • R or REAL
    • F or FIX
    • M or MONEY
    • D or DATE
    • T or TIME
    • TS or TIMESTAMP
    • IV or INTERVAL
    • To set the number type of the returned text
  • qNumFmt: <string>
    • Format of number e.g. '$#,##0'
  • qFillStyle: <string>
    • Fill colour of your measure to be used in your chart

Metric Options

  • qName: string
    • Name of the metric
  • qExpr: string
    • Qlik expression to calculate a single value
  • qType: oneOf
    • qStringExpression
    • qValueExpression
    • Whether the expression should return a string or numeric value

Data Properties

The following properties are returned from the hook
  • DataSet: Array
    • An array of data objects, contains the data returned from the engine
    • The structure of this object can change depending on your cols input
    • See Data Properties for more details
  • DataKeys: Array
    • An array of data keys, contains the dimension names, which your measure is calculated across or the name of the measure. This depends on the number of dimensions and measures you are passing into cols.
    • See Data Keys Properties for more details
  • DataList: Array
    • See Data List Properties for more details
  • select: Function(column, [elemNumber], toggle)
    • A function to apply selections against the Qlik engine
    • You need to pass:
      • Column to apply selection against Needs to be an array of the element numbers you wish to select
      • An array of element numbers to select
      • Whether to toggle selections, optional parameter, default set to false
    • Element numbers are equivalent to the elemNumber in the dataKey object, in dataSet
  • beginSelections: Function
    • A function to begin the selections mode, equivalent to this Qlik method
  • endSelections: Function
    • A function to end the selections mode, equivalent to this Qlik method
  • selections: Array
    • An array of selected element numbers
  • clearSelections: Function
    • Clears all selections for the particular dimension
  • metrics: Object
    • An object containing the output from Metric options (qMetrics array)

DataSet Properties

  • qElemNumber: Number
    • Element number of the data point relating to the primary dimension
  • label: String
    • Label of the data point
  • [dimensionOneName]: String
    • Primary dimension values
  • [dimensionTwoName]: String
    • Second dimension values. Will be present if two dimensions are passed into the cols object
  • [dimensionTwoName]-qElemNumber: Number
    • Element number associated to each of the second dimension data points
    • Will be present if two dimensions are passed into the cols object
  • [measureName]: String | Number
    • Measure value, will be present if only 1 dimension is passed into the cols object
  • key: Number
    • Unique key for each object in the DataSet array

DataKey Properties

  • [keyName]: string
    • Relates to a data point in each Data Object
    • Could be a measure name or list of dimension values depending on your cols definition

Data List Properties

Contains an array of dataKeyobjects. Please see Data Key Properties for more details
Last modified 9mo ago