Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
git clone https://github.com/qlik-demo-team/qlik-picasso-tutorial.git
cd qlik-picasso-tutorial
npm i
npm run dev
// qDocPromise.js
const enigma = require('enigma.js');
const schema = require('enigma.js/schemas/12.20.0.json');
const SenseUtilities = require('enigma.js/sense-utilities');
const config = {
host: '', // <== Qlik server domain here
secure: true,
port: 443,
prefix: '',
appId: '', // <== app ID here
};
const url = SenseUtilities.buildUrl(config);
const qDocPromise = enigma.create({ schema, url }).open().then(global => global.openDoc(config.appId));
export default qDocPromise;
// hypercube.js
const hypercube = {
qInfo: { qId: 'Consumer Sales', qType: 'data' },
qHyperCubeDef: {
qDimensions: [
{ qDef: { qFieldDefs: ['[Product Group Desc]']}, qFieldLabels: ['Product Group'] }
],
qMeasures: [
{ qDef: { qDef: '=SUM([Sales Margin Amount])/SUM([Sales Amount])', qLabel: 'Margin' }},
],
qInitialDataFetch: [{
qTop: 0, qLeft: 0, qWidth: 10, qHeight: 1000,
}],
qInterColumnSortOrder: [],
qSuppressZero: true,
qSuppressMissing: true,
}
}
export default hypercube;
// listObject.js
const listObject = {
title: "A list object",
description: "Description of the list object",
qInfo: { qId: 'Consumer Sales List Object', qType: 'List Object'},
qListObjectDef: {
qStateName: "$",
qDef: { qFieldDefs: ['[Product Group Desc]'], qFieldLabels: ["Product Description"], qSortCriterias: [{qSortByLoadOrder: 1}]},
qInitialDataFetch: [{
qTop: 0, qLeft: 0, qWidth: 10, qHeight: 1000,
}],
},
}
export default listObject;
import qDocPromise from './qDocPromise';
import hypercube from './hypercube';
import listObject from './listObject';
qDocPromise.then((doc) => {
console.log(doc);
doc.createSessionObject(hypercube).then((hc) => {
hc.getLayout().then((layout) => {
console.log(layout)
})
})
doc.createSessionObject(listObject).then((lo) => {
lo.getLayout().then((layout) => {
console.log(layout)
})
})
})
// Home.js
import React, { useRef } from 'react'; // NEW CODE
import qDocPromise from './qDocPromise';
import hypercube from './hypercube';
import listObject from './listObject';
// >>>>> NEW CODE
import picasso from 'picasso.js';
import picassoQ from 'picasso-plugin-q';
picasso.use(picassoQ);
import settings from './settings'; // new file
// <<<<<<<<<<
const Home = () => {
const elementNode = useRef(null); // NEW CODE
qDocPromise.then((doc) => {
console.log(doc);
doc.createSessionObject(hypercube).then((hc) => {
hc.getLayout().then((layout) => {
console.log(layout)
})
})
doc.createSessionObject(listObject).then((lo) => {
lo.getLayout().then((layout) => {
console.log(layout)
})
})
})
// NEW CODE
return (
<div className="parent">
<div style={{ padding: '20px'}} >
<div ref={elementNode} style={{ height: '400px', width: '600px', minWidth: 'min(600px, 90vw)', position: 'relative'}}></div>
</div>
</div>
)
}
export default Home;
// settings.js (new file)
const settings = {
scales: {
y: {
data: { field: 'Margin' },
invert: true,
include: [0]
},
c: {
data: { field: 'Margin' },
type: 'color'
},
t: { data: { extract: { field: '[Product Group Desc]' } }, padding: 0.3 },
},
components: [{
type: 'axis',
dock: 'left',
scale: 'y'
},{
type: 'axis',
dock: 'bottom',
scale: 't'
},{
key: 'bars',
type: 'box',
data: {
extract: {
field: '[Product Group Desc]',
props: {
start: 0,
end: { field: 'Margin' }
}
}
},
settings: {
major: { scale: 't' },
minor: { scale: 'y' },
box: {
fill: { scale: 'c', ref: 'end' }
}
}
}]
}
export default settings;
doc.createSessionObject(hypercube).then((hc) => {
hc.getLayout().then((layout) => {
let _pic = picasso.chart({
element: elementNode.current,
data: [{
type: 'q',
key: 'qHyperCube',
data: layout.qHyperCube
}],
settings
})
})
})
// Dropdown.js
import React from 'react';
const Dropdown = ({ options, makeSelection }) => {
console.log(options);
const handleChange = (val) => {
const currentSelected = options.filter(o => o.qState === 'S').map(o => Number(o.qElemNumber))
currentSelected.push(Number(val));
makeSelection(currentSelected);
}
return (
<select onChange={(e) => handleChange(e.target.value)}>
{options.map((opt, i) => <option key={i} value={opt.qElemNumber}>{opt.qText} - {opt.qState}</option>)}
</select>
)
}
export default Dropdown;
// Home.js
import React, { useRef, useState, useEffect } from 'react';
import Dropdown from './Dropdown';
import qDocPromise from './qDocPromise';
import hypercube from './hypercube';
import listObject from './listObject';
import picasso from 'picasso.js';
import picassoQ from 'picasso-plugin-q';
picasso.use(picassoQ);
import settings from './settings';
const Home = () => {
const elementNode = useRef(null);
const [qListObject, setQListObject] = useState(null);
const [options, setOptions] = useState([])
const updateOptions = (layout) => {
const lOptions = layout.qListObject.qDataPages[0].qMatrix.map((arr) => {
return arr[0];
})
setOptions(lOptions);
}
useEffect(() => {
qDocPromise.then((doc) => {
console.log(doc);
doc.createSessionObject(hypercube).then((hc) => {
hc.getLayout().then((layout) => {
let _pic = picasso.chart({
element: elementNode.current,
data: [{
type: 'q',
key: 'qHyperCube',
data: layout.qHyperCube
}],
settings
})
})
})
// List Object
doc.createSessionObject(listObject).then((lo) => {
setQListObject(lo);
lo.getLayout().then((layout) => {
updateOptions(layout);
})
lo.on('changed', () => {
lo.getLayout().then((layout) => {
updateOptions(layout);
})
})
})
})
}, [])
const makeSelection = (arr) => {
qListObject.selectListObjectValues('/qListObjectDef', arr, false, false)
}
return (
<div className="parent">
<Dropdown options={options} makeSelection={makeSelection} />
<div style={{ padding: '20px'}} >
<div ref={elementNode} style={{ height: '400px', width: '600px', minWidth: 'min(600px, 90vw)', position: 'relative'}}></div>
</div>
</div>
)
}
export default Home;
qDocPromise.then((doc) => {
let _pic;
doc.createSessionObject(hypercube).then((hc) => {
hc.getLayout().then((layout) => {
_pic = picasso.chart({
element: elementNode.current,
data: [{
type: 'q',
key: 'qHyperCube',
data: layout.qHyperCube
}],
settings
})
})
hc.on('changed', () => {
hc.getLayout().then((layout) => {
_pic.update({
data: [{
type: 'q',
key: 'qHyperCube',
data: layout.qHyperCube
}],
})
})
})
})
// more code
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.