This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,151 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
removeRole, | ||
addRole, | ||
} from '../roles/roles'; | ||
|
||
const RX = /\/(qDimensions|qMeasures)\/(\d+)/; | ||
const RXA = /\/(qDimensions|qMeasures)\/(\d+)\/(qAttributeDimensions|qAttributeExpressions)\/(\d+)/; | ||
|
||
/** | ||
* @param {object} properties | ||
* @param {object} byDimensionConfig | ||
* @param {'index'|'expression'|'libraryId'} byDimensionConfig.type | ||
* @param {string|integer} byDimensionConfig.typeValue | ||
* @param {boolean} byDimensionConfig.persistent | ||
* @param {string} byDimensionConfig.scheme | ||
* @param {string|QAE.StringExpression} byDimensionConfig.label | ||
*/ | ||
export function setByDimension(properties, byDimensionConfig) { | ||
// remove existing | ||
removeRole(properties.qHyperCubeDef, 'color'); | ||
|
||
const dimensions = properties.qHyperCubeDef.qDimensions; | ||
|
||
const defaultTargetPath = `/qDimensions/${dimensions.length - 1}`; | ||
|
||
const config = byDimensionConfig || { | ||
type: 'index', | ||
typeValue: dimensions.length - 1, | ||
}; | ||
|
||
let targetPath; | ||
if (config.type === 'index') { | ||
targetPath = `/qDimensions/${config.typeValue}`; | ||
} else { | ||
targetPath = `${defaultTargetPath}/qAttributeDimensions/0`; | ||
} | ||
|
||
const m = RXA.exec(targetPath); | ||
if (m) { | ||
const def = config.type === 'libraryId' ? { | ||
qLibraryId: config.typeValue, | ||
libraryId: config.typeValue, // add custom property since qLibraryId is not returned in attr dimension/measure in layout | ||
} : { | ||
qDef: config.typeValue, | ||
}; | ||
properties.qHyperCubeDef[m[1]][+m[2]][m[3]].splice(+m[4], 0, { | ||
...def, | ||
qAttribute: true, | ||
qSortBy: { qSortByAscii: 1 }, | ||
roles: [{ role: 'color' }], | ||
}); | ||
} else { | ||
// add role only | ||
const mx = RX.exec(targetPath); | ||
if (mx) { | ||
addRole(properties.qHyperCubeDef[mx[1]][+mx[2]], 'color'); | ||
} else { | ||
console.error('hmm?'); | ||
} | ||
} | ||
|
||
properties.color.byDimension = { | ||
persistent: false, | ||
scheme: '', | ||
...(properties.color.byDimension || {}), | ||
...(config || {}), | ||
}; | ||
} | ||
|
||
export function getByDimensionSettings({ | ||
layout, | ||
theme, | ||
definition, | ||
fieldPath, | ||
}) { | ||
if (definition.qError || (definition.qSize && definition.qSize.qcy === 0)) { | ||
return { | ||
invalid: true, | ||
...theme.dataColors(), | ||
}; | ||
} | ||
const pals = theme.palettes('qualitative'); | ||
const c = (layout.color && layout.color.byDimension) || {}; | ||
return { | ||
mode: 'field', | ||
field: fieldPath, | ||
fieldType: 'dimension', | ||
persistent: c.persistent, | ||
|
||
type: 'categorical', | ||
|
||
// references values in a theme | ||
palette: pals.filter(p => p.key === c.scheme)[0] || pals[0], | ||
...theme.dataColors(), | ||
|
||
// for tooltips and legend | ||
label: c.type === 'expression' ? c.label || definition.qFallbackTitle : definition.qFallbackTitle, | ||
}; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
removeRole, | ||
findFields, | ||
} from '../roles/roles'; | ||
|
||
import { | ||
setByDimension, | ||
getByDimensionSettings, | ||
} from './byDimension'; | ||
|
||
const DIMENSION_RX = /(qDimensions|qDimensionInfo)\/\d+(\/(qAttributeDimensions|qAttrDimInfo)\/\d+)?$/; | ||
const MEASURE_RX = /(qMeasures|qMeasureInfo)\/\d+(\/(qAttributeExpressions|qAttrExprInfo)\/\d+)?$/; | ||
|
||
function getFieldType(path) { | ||
if (DIMENSION_RX.test(path)) { | ||
return 'dimension'; | ||
} | ||
if (MEASURE_RX.test(path)) { | ||
return 'measure'; | ||
} | ||
return undefined; | ||
} | ||
|
||
function setByAuto(properties) { | ||
properties.color.mode = 'auto'; | ||
} | ||
|
||
export default function coloring({ | ||
properties, | ||
layout, | ||
theme, | ||
}) { | ||
return { | ||
colorBy({ | ||
mode, | ||
modeConfig, | ||
}) { | ||
// reset | ||
removeRole(properties.qHyperCubeDef, 'color'); | ||
|
||
if (!properties.color) { | ||
properties.color = {}; | ||
} | ||
|
||
if (mode === 'auto') { | ||
setByAuto(properties); | ||
delete properties.color.byDimension; | ||
} else if (mode === 'byDimension') { | ||
setByDimension(properties, modeConfig); | ||
} | ||
}, | ||
getSettings() { | ||
const hc = layout ? layout.qHyperCube : properties.qHyperCubeDef; | ||
const colorProps = layout ? layout.color : properties.color; | ||
|
||
const colorByField = findFields(f => f.roles && f.roles.filter(r => r.role === 'color').length > 0, hc)[0]; | ||
let fieldPath = colorByField ? colorByField.path.replace(/^\//, '') : 'qDimensionInfo/1'; | ||
let definition = colorByField ? colorByField.definition : null; | ||
|
||
if (!colorByField || colorProps.mode === 'auto') { | ||
fieldPath = 'qDimensionInfo/1'; | ||
definition = layout ? layout.qHyperCube.qDimensionInfo[1] : properties.qHyperCubeDef.qDimensions[0]; | ||
} | ||
|
||
const fieldType = getFieldType(fieldPath); | ||
|
||
if (fieldType === 'dimension') { | ||
return getByDimensionSettings({ | ||
layout, | ||
theme, | ||
definition, | ||
fieldPath, | ||
}); | ||
} | ||
|
||
return { | ||
invalid: true, | ||
}; | ||
}, | ||
getLegendSettings() { | ||
return { | ||
dock: 'right', | ||
}; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.