All files / app graph.tsx

94.87% Statements 74/78
58.92% Branches 33/56
95.23% Functions 20/21
96.07% Lines 49/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169                        4x   54x 54x 179x 177x 90x 120x 120x       78x 78x 210x 288x 228x 228x 132x 228x 132x 132x 78x   18x   228x 228x 132x 68x 67x 68x 71x 128x   78x 288x 144x 144x 144x 144x 11830x 144x 13930x 13930x   144x 132x 132x 13930x 13930x 13930x   132x 143x   132x                                                                                                                                                                                         12x         288x    
import {use, init, getInstanceByDom} from 'echarts/core'
import {TooltipComponent, LegendComponent} from 'echarts/components'
import {GraphChart} from 'echarts/charts'
import {CanvasRenderer} from 'echarts/renderers'
import {GraphGLChart} from 'echarts-gl/charts'
import type {Edge, Node} from './analysisResults'
import {useContext, useEffect, useRef} from 'react'
import {Box} from '@mui/material'
import type {PlotOptions} from './analysisMenu'
import {EditorTermSetter} from './termEditor'
import {InfoDrawerSetter} from './infoDrawer'
 
type slimChartType = {_chartsViews: {_layouting: boolean; _layoutTimeout: number}[]; dispose: () => void}
function cancelViewSteppers(chart: slimChartType | null) {
  Eif (chart && chart._chartsViews) {
    chart._chartsViews.forEach(view => {
  E    if (view._layouting) {
        view._layouting = false
        clearTimeout(view._layoutTimeout)
      }
    })
  }
}
export default function Graph({allNodes, edges, options}: {allNodes: Node[]; edges: Edge[]; options: PlotOptions}) {
  const termEditor = useContext(EditorTermSetter)
  const updateInfoDrawerState = useContext(InfoDrawerSetter)
  const container = useRef<HTMLDivElement>(null)
  useEffect(() => {
    use([TooltipComponent, GraphChart, GraphGLChart, CanvasRenderer, LegendComponent])
    const chart = container.current ? init(container.current, 'dark', {renderer: 'canvas'}) : null
    const resize = () => chart && chart.resize()
    Eif (chart)
    E  chart.on('click', params => {
        Eif (params.dataType === 'node' || params.componentSubType === 'graphGL') {
          termEditor(params.name, true)
          updateInfoDrawerState({type: 'add', state: {type: 'term', value: params.name}})
        }
      })
    window.addEventListener('resize', resize)
    return () => {
      Eif (chart) {
      E  cancelViewSteppers(chart as unknown as slimChartType)
        chart.dispose()
      }
      window.removeEventListener('resize', resize)
    }
  }, [options.layout, termEditor, updateInfoDrawerState])
  useEffect(() => {
    Eif (container.current) {
    E  const chart = getInstanceByDom(container.current)
      Eif (chart) {
      E  cancelViewSteppers(chart as unknown as slimChartType)
        let nodes = allNodes
        if (options.hide_zeros) nodes = nodes.filter(node => !!node.value)
        nodes = nodes.map(node => {
          node.label = {show: node.value >= options.label_threshold}
          return node
        })
        if (edges.length) {
          const presentCats: {[index: string]: boolean} = {}
          nodes.forEach(node => {
            if ('string' !== typeof node.category) node.category = node.category[0]
            presentCats[node.category] = true
            node.symbolSize = options.size_by_value ? 7 + node.prop * 18 : 10
          })
          const categories = Object.keys(presentCats).map(cat => {
            return {name: cat}
          })
          chart.setOption(
            {
              legend: {
                align: 'right',
                right: 'right',
                orient: 'vertical',
                type: 'plain',
                pageButtonGap: 10,
              },
              tooltip: {
                confine: true,
                formatter: (item: {marker: string; name: string; value: number; data: {host?: string}}) => {
                  return (
                    item.marker +
                    (item.data.host ? '<i>(' + item.data.host + ')</i> ' : '') +
                    item.name +
                    ': <strong>' +
                    item.value.toFixed(3) +
                    '</strong>'
                  )
                },
              },
              backgroundColor: '#000',
              series: [
                options.layout === 'forceAtlas2'
                  ? {
                      type: 'graphGL',
                      nodes,
                      edges,
                      categories: categories,
                      roam: true,
                      label: {
                        show: true,
                        position: 'top',
                        color: '#fff',
                      },
                      lineStyle: {
                        color: 'source',
                      },
                      emphasis: {
                        focus: 'adjacency',
                        lineStyle: {
                          color: '#fff',
                          opacity: 1,
                          width: 10,
                        },
                      },
                    }
                  : {
                      type: 'graph',
                      layout: options.layout,
                      nodes,
                      edges,
                      categories: categories,
                      roam: true,
                      label: {
                        show: true,
                        position: 'top',
                      },
                      emphasis: {
                        focus: 'adjacency',
                        itemStyle: {opacity: 1},
                        label: {opacity: 1},
                        lineStyle: {
                          width: 10,
                          opacity: 1,
                        },
                      },
                      blur: {
                        itemStyle: {opacity: 0.3},
                        lineStyle: {opacity: 0.3},
                        label: {opacity: 0.3},
                      },
                      autoCurveness: true,
                      draggable: true,
                      force: {
                        repulsion: options.repulsion,
                        gravity: options.gravity,
                        edgeLength: options.edge_length,
                      },
                      circular: {
                        rotateLabel: true,
                      },
                      lineStyle: {
                        color: 'source',
                      },
                    },
              ],
            },
            false,
            options.layout !== 'forceAtlas2'
          )
        } else {
          chart.clear()
        }
      }
    }
  }, [allNodes, edges, options, termEditor, updateInfoDrawerState])
  return <Box ref={container} sx={{width: '100%', height: '100%', minHeight: '10px'}} />
}