import "powerbi-visuals-tools/templates/visuals/.api/v1.11.0/PowerBI-visuals" ; import { INetworkNavigatorData , INetworkNavigatorLink , INetworkNavigatorConfiguration } from "@essex/network-navigator" ; import { INetworkNavigatorSelectableNode } from "./models" ; import { DATA_ROLES } from "./constants" ; import * as models from "powerbi-models" ; import DataView = powerbi . DataView ; import SelectionId = powerbi . visuals . ISelectionId ; import ISelectionIdBuilder = powerbi . visuals . ISelectionIdBuilder ; /**
 * Converts the powerbi data view into an internal data structure
 */ export function converter ( dataView , settings , columnToFilter ? , createIdBuilder ? ) { "use strict" ; let nodeList = [ ] ; const nodeMap = { } ; let linkList = [ ] ; const table = dataView . table ; const colMap = { } ; const metadataColMap = { } ; table . columns . forEach ( ( c , i ) => { Object . keys ( c . roles ) . forEach ( role => { colMap [ role ] = i ; metadataColMap [ role ] = c ; } ) ; } ) ; const roles = DATA_ROLES ; const sourceIdx = colMap [ roles . source . name ] ; const sourceColorIdx = colMap [ roles . sourceColor . name ] ; const sourceLabelColorIdx = colMap [ roles . sourceLabelColor . name ] ; const targetColorIdx = colMap [ roles . targetColor . name ] ; const targetLabelColorIdx = colMap [ roles . targetLabelColor . name ] ; const targetIdx = colMap [ roles . target . name ] ; const edgeValueIdx = colMap [ roles . edgeValue . name ] ; const edgeColorValueIdx = colMap [ roles . edgeColorValue . name ] ; const sourceNodeWeightIdx = colMap [ roles . sourceNodeWeight . name ] ; const targetNodeWeightIdx = colMap [ roles . targetNodeWeight . name ] ; /**
     * Creates a node with the given value if the node has not already been seen/created
     */ function getNode ( id , dvIdentity , isSource , nodeWeight , color = "gray" , labelColor , group = 0 ) { const column = table . columns [ isSource ? sourceIdx : targetIdx ] ; let node = nodeMap [ id ] ; if ( ! nodeMap [ id ] ) { const builder = createIdBuilder ( ) ; const identityColumn = dataView . metadata . columns . filter ( n => n . queryName === column . queryName ) [ 0 ] ; const filterTargetColumn = columnToFilter || identityColumn ; const target = { table : filterTargetColumn . queryName . substr ( 0 , filterTargetColumn . queryName . indexOf ( "." ) ) , column : filterTargetColumn . displayName , } ; const filter = < models . IAdvancedFilter > < any > new models . AdvancedFilter ( target , "And" , { operator : "Is" , value : id , } ) ; node = nodeMap [ id ] = { name : id , color : color || "gray" , labelColor , index : nodeList . length , filter , value : nodeWeight , neighbors : 1 , selected : false , identity : builder ? builder . withCategory ( { source : identityColumn , values : < any > null , identity : [ dvIdentity ] , } , 0 ) . createSelectionId ( ) : < any > - 1 , } ; nodeList . push ( node ) ; } return node ; } if ( sourceIdx !== undefined && targetIdx !== undefined ) { table . rows . forEach ( ( row , idx ) => { const identity = table . identity [ idx ] ; if ( row [ sourceIdx ] && row [ targetIdx ] ) { /** These need to be strings to work properly */ const sourceId = row [ sourceIdx ] + "" ; const targetId = row [ targetIdx ] + "" ; const edge = { source : getNode ( sourceId , identity , true , row [ sourceNodeWeightIdx ] as number , row [ sourceColorIdx ] as string , row [ sourceLabelColorIdx ] as string ) . index , target : getNode ( targetId , identity , false , row [ targetNodeWeightIdx ] as number , row [ targetColorIdx ] as string , row [ targetLabelColorIdx ] as string ) . index , value : row [ edgeValueIdx ] , colorValue : row [ edgeColorValueIdx ] , } as INetworkNavigatorLink ; nodeList [ edge . source ] . neighbors += 1 ; nodeList [ edge . target ] . neighbors += 1 ; linkList . push ( edge ) ; } } ) ; const maxNodes = settings . maxNodeCount ; if ( typeof maxNodes === "number" && maxNodes > 0 ) { nodeList = nodeList . slice ( 0 , maxNodes ) ; linkList = linkList . filter ( n => n . source < maxNodes && n . target < maxNodes ) ; } } return { nodes : nodeList , links : linkList , } ; } export default converter ;