Skip to content

Instantly share code, notes, and snippets.

@pietersv
Created November 6, 2023 21:50
Show Gist options
  • Save pietersv/fefd28b46f5ce24b249280f4986ecdcf to your computer and use it in GitHub Desktop.
Save pietersv/fefd28b46f5ce24b249280f4986ecdcf to your computer and use it in GitHub Desktop.
Selectively change set element to weighted/unweighted depending on currently local or global crosstab banner element
/**
* This code will set selected elements to unweighted if crosstabbed by particular banner elements.
*/
{
//TL, 2023-11-02
//This code is from the QUNIT: Car sales demo
//Add code that unweighs data when Specialty is used as a crosstab
let BANNER_THAT_SHALL_BE_UNWEIGHTED = 'quotaquestion'
//Toggle global weight off when the global crosstab == BANNER_THAT_SHALL_BE_UNWEIGHTED
function onChangeGlobalWeight() {
let globalBanner = protobi.viewModel.get('crosstab')
$.notify("Crossed by "+ globalBanner)
if (globalBanner == BANNER_THAT_SHALL_BE_UNWEIGHTED) {
protobi.viewModel.set('showWeighted', false)
}
else {
protobi.viewModel.set('showWeighted', true)
}
}
//Set local weight == 1 when the local crosstab == BANNER_THAT_SHALL_BE_UNWEIGHTED
function onChangeLocalWeight(dim) {
let col = dim.get('col')
if (!Array.isArray(col)) col = [col]
let priorWeight = dim.get('weight') || null
let defaultWeight = dim.get('_weight') || null
let currentWeight = col.includes(BANNER_THAT_SHALL_BE_UNWEIGHTED) ? 1 : (defaultWeight ?? priorWeight)
if (priorWeight != currentWeight) {
dim.set('weight', currentWeight)
console.log(`Note: setting weight to ${currentWeight} for ${dim.getKey()} because banner is '${BANNER_THAT_SHALL_BE_UNWEIGHTED}`, {priorWeight, currentWeight, col})
}
}
protobi.viewModel.on('change:crosstab', onChangeGlobalWeight)
onChangeGlobalWeight()
protobi.getDimensions().forEach(function(dim) {
onChangeLocalWeight(dim)
dim.on('change:col', function(){
onChangeLocalWeight(dim)
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment