'use strict' ; import type CorePoint from '../Core/Series/Point' ; import type CorePointOptions from '../Core/Series/PointOptions' ; import type CoreSeries from '../Core/Series/Series' ; import type ColorType from '../Core/Color/ColorType' ; import Color from '../Core/Color/Color.js' ; import U from '../Core/Utilities.js' ; const { extend , isArray , isNumber , isObject , merge , pick } = U ; private function getColor ( node , options ) { const index = options . index , mapOptionsToLevel = options . mapOptionsToLevel , parentColor = options . parentColor , parentColorIndex = options . parentColorIndex , series = options . series , colors = options . colors , siblings = options . siblings , points = series . points , chartOptionsChart = series . chart . options . chart ; let getColorByPoint , point , level , colorByPoint , colorIndexByPoint , color , colorIndex ; private const variateColor = ( color ) : ColorType => { const colorVariation = level && level . colorVariation ; if ( colorVariation && colorVariation . key === 'brightness' && index && siblings ) { return Color . parse ( color ) . brighten ( colorVariation . to * ( index / siblings ) ) . get ( ) ; } return color ; } ; if ( node ) { point = points [ node . i ] ; level = mapOptionsToLevel [ node . level ] || { } ; getColorByPoint = point && level . colorByPoint ; if ( getColorByPoint ) { colorIndexByPoint = ( point . index as any ) % ( colors ? colors . length : ( chartOptionsChart . colorCount as any ) ) ; colorByPoint = colors && colors [ colorIndexByPoint ] ; } if ( ! series . chart . styledMode ) { color = pick ( point && point . options . color , level && level . color , colorByPoint , parentColor && variateColor ( parentColor ) , series . color ) ; } colorIndex = pick ( point && point . options . colorIndex , level && level . colorIndex , colorIndexByPoint , parentColorIndex , options . colorIndex ) ; } return { color : color , colorIndex : colorIndex } ; } private param { Object } params return { Highcharts . Dictionary < object > | null } function getLevelOptions < T extends TreeUtilities . Series > ( params ) { let result = null , defaults , converted , i , from , to , levels ; if ( isObject ( params ) ) { result = { } ; from = isNumber ( params . from ) ? params . from : 1 ; levels = params . levels ; converted = { } as any ; defaults = isObject ( params . defaults ) ? params . defaults : { } ; if ( isArray ( levels ) ) { converted = levels . reduce ( ( obj , item ) : any => { let level , levelIsConstant , options ; if ( isObject ( item ) && isNumber ( item . level ) ) { options = merge ( { } , item ) ; levelIsConstant = pick ( options . levelIsConstant , defaults . levelIsConstant ) ; delete options . levelIsConstant ; delete options . level ; level = item . level + ( levelIsConstant ? 0 : from - 1 ) ; if ( isObject ( obj [ level ] ) ) { merge ( true , obj [ level ] , options ) ; } else { obj [ level ] = options ; } } return obj ; } , { } ) ; } to = isNumber ( params . to ) ? params . to : 1 ; for ( i = 0 ; i <= to ; i ++ ) { result [ i ] = merge ( { } , defaults , isObject ( converted [ i ] ) ? converted [ i ] : { } ) ; } } return result ; } private todo todo function setTreeValues < T extends TreeUtilities . Series > ( tree , options ) { const before = options . before , idRoot = options . idRoot , mapIdToNode = options . mapIdToNode , nodeRoot = mapIdToNode [ idRoot ] , levelIsConstant = ( options . levelIsConstant !== false ) , points = options . points , point = points [ tree . i ] , optionsPoint = point && point . options || { } , children = [ ] ; let childrenTotal = 0 ; tree . levelDynamic = tree . level - ( levelIsConstant ? 0 : nodeRoot . level ) ; tree . name = pick ( point && point . name , '' ) ; tree . visible = ( idRoot === tree . id || options . visible === true ) ; if ( typeof before === 'function' ) { tree = before ( tree , options ) ; } tree . children . forEach ( ( child , i ) : void => { const newOptions = extend < TreeUtilities . SetTreeValuesOptions < T > > ( { } as any , options ) ; extend ( newOptions , { index : i , siblings : tree . children . length , visible : tree . visible } ) ; child = setTreeValues ( child , newOptions ) ; children . push ( child ) ; if ( child . visible ) { childrenTotal += child . val ; } } ) ; const value = pick ( optionsPoint . value , childrenTotal ) ; tree . visible = value >= 0 && ( childrenTotal > 0 || tree . visible ) ; tree . children = children ; tree . childrenTotal = childrenTotal ; tree . isLeaf = tree . visible && ! childrenTotal ; tree . val = value ; return tree ; } private param { Object } series return { string } function updateRootId ( series ) { let rootId , options ; if ( isObject ( series ) ) { options = isObject ( series . options ) ? series . options : { } ; rootId = pick ( series . rootNode , options . rootId , '' ) ; if ( isObject ( series . userOptions ) ) { series . userOptions . rootId = rootId ; } series . rootNode = rootId ; } return rootId ; } namespace TreeUtilities { export interface ColorObject { color : ColorType ; colorIndex : number ; } export interface GetColorOptions { colorIndex ? : number ; colors ? : Array < ColorType > ; index ? : number ; mapOptionsToLevel ? : any ; parentColor ? : ColorType ; parentColorIndex ? : number ; series : CoreSeries ; siblings ? : number ; } export interface NodeObject { children : Array < NodeObject > ; childrenTotal ? : number ; i : number ; id : string ; isLeaf ? : boolean ; levelDynamic ? : number ; level : number ; name ? : string ; val : number ; visible : boolean ; } export interface Point extends CorePoint { options : PointOptions ; } export interface PointOptions extends CorePointOptions { value ? : ( number | null ) ; } export interface Series extends CoreSeries { mapOptionsToLevel : any ; points : Array < Point > ; tree : NodeObject ; } export interface SetTreeValuesBeforeCallbackFunction < T extends Series > { ( node , options ) : T [ 'tree' ] ; } export interface SetTreeValuesOptions < T extends Series > { before ? : SetTreeValuesBeforeCallbackFunction < T > ; idRoot : string ; index ? : number ; levelIsConstant ? : boolean ; mapIdToNode : Record < string , NodeObject > ; points : T [ 'points' ] ; series : T ; siblings ? : number ; visible ? : boolean ; } } const TreeUtilities = { getColor , getLevelOptions , setTreeValues , updateRootId } ; export default TreeUtilities ;