-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
0d0e730
commit 95be4fd
Showing
5 changed files
with
154 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
177 changes: 106 additions & 71 deletions
177
src/components/module-dependency-graph/ModuleDependencyGraph.vue
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 |
---|---|---|
@@ -1,100 +1,135 @@ | ||
<template> | ||
<canvas id="chart" /> | ||
<div class="cy-wrapper"> | ||
<div id="cy"></div> | ||
</div> | ||
</template> | ||
|
||
<style> | ||
.cy-wrapper { | ||
width: 100%; | ||
height: 100%; | ||
position: absolute; | ||
} | ||
#cy { | ||
width: 100%; | ||
height: 100%; | ||
position: relative; | ||
margin: auto; | ||
background: #ececec; | ||
} | ||
</style> | ||
|
||
<script lang="ts"> | ||
import { Chart, LinearScale, PointElement, type ChartOptions, type ChartTypeRegistry, type ChartConfiguration } from 'chart.js/auto'; | ||
import { defineComponent } from 'vue'; | ||
import { ForceDirectedGraphController, EdgeLine } from 'chartjs-chart-graph'; | ||
import ChartDataLabels from 'chartjs-plugin-datalabels'; | ||
import type { Module } from '../../helpers/types'; | ||
type Node = { id: string, displayName: string }; | ||
type Link = { source: string, target: string }; | ||
Chart.register(ForceDirectedGraphController, EdgeLine, LinearScale, PointElement, ChartDataLabels); | ||
// todo: labels (should not hide arrow) | ||
// todo: no animation (faster render) | ||
// todo: keep things closer together (way too speard apart) | ||
const chartOptions: ChartOptions<'forceDirectedGraph' | ChartDataLabels> = { | ||
tree: { orientation: 'radial' }, | ||
layout: { padding: 20 }, | ||
plugins: { | ||
datalabels: { | ||
display: (_: ChartDataLabels.Context) => { | ||
return true; | ||
}, | ||
align: (context: ChartDataLabels.Context) => { | ||
const index = context.dataIndex; | ||
const value = context.dataset.data[index] as { angle: number }; | ||
return (-value.angle / Math.PI) * 180; | ||
}, | ||
rotation: (context: ChartDataLabels.Context) => { | ||
const index = context.dataIndex; | ||
const value = context.dataset.data[index] as { angle: number }; | ||
return (-value.angle / Math.PI) * 180; | ||
}, | ||
backgroundColor: 'white', | ||
formatter: (value: { displayName: string }) => { | ||
return value.displayName; | ||
}, | ||
}, | ||
}, | ||
}; | ||
import cytoscape from 'cytoscape'; | ||
import { getCategoryColorForModule } from '../../helpers/color-helper'; | ||
export default defineComponent({ | ||
name: 'ModuleDependencyGraph', | ||
props: { | ||
modules: { | ||
modulesToDisplay: { | ||
type: Array<Module>, | ||
required: true, | ||
}, | ||
selectedModules: { | ||
type: Array<Module>, | ||
required: true, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
chart: null as any | null, | ||
}; | ||
cy: null as any | null, | ||
} | ||
}, | ||
mounted() { | ||
this.create(); | ||
}, | ||
watch: { | ||
modules: { | ||
modulesToDisplay: { | ||
deep: true, | ||
immediate: false, | ||
handler(newValue: Module[]) { | ||
const nodes = [...newValue.map(module => ({ id: module.id, displayName: module.name }))]; | ||
let links = [...newValue.flatMap(module => module.recommendedModuleIds.map(recommendedId => ({ source: recommendedId, target: module.id })) ?? [])]; | ||
links = links.filter(link => nodes.some(node => node.id === link.source) && nodes.some(node => node.id === link.target)); | ||
console.log({ nodes, links }); | ||
this.createChart(nodes, links); | ||
handler() { | ||
this.create(); | ||
}, | ||
}, | ||
}, | ||
methods: { | ||
createChart(nodes: Node[], links: Link[]) { | ||
if (this.chart) { | ||
this.chart.destroy(); | ||
} | ||
const canvas = (document.getElementById('chart') as HTMLCanvasElement).getContext('2d') as CanvasRenderingContext2D; | ||
const config: ChartConfiguration<'forceDirectedGraph' | ChartDataLabels> = { | ||
type: ForceDirectedGraphController.id, | ||
buildData(): { nodes: { data: Node }[], edges: { data: Edge }[] } { | ||
const nodes: { data: Node }[] = [...this.modulesToDisplay.map(module => ({ | ||
data: { | ||
labels: nodes.map((d) => d.id), | ||
datasets: [{ | ||
pointBackgroundColor: 'steelblue', | ||
pointRadius: 5, | ||
directed: true, | ||
data: nodes, | ||
edges: links, | ||
}] | ||
}, | ||
options: chartOptions, | ||
plugins: [ChartDataLabels], | ||
}; | ||
this.chart = new Chart(canvas, config); | ||
id: module.id, | ||
label: module.name, | ||
categoryColor: getCategoryColorForModule(module), | ||
} | ||
}))]; | ||
let edges = [...this.modulesToDisplay.flatMap(module => | ||
module.recommendedModuleIds.map(recId => | ||
({ data: { source: recId, target: module.id } }) | ||
) ?? [])]; | ||
edges = edges.filter(edge => | ||
edge.data.source && | ||
edge.data.target && | ||
nodes.some(node => node.data.id === edge.data.source) && | ||
nodes.some(node => node.data.id === edge.data.target)); | ||
console.log({ nodes, edges }); | ||
return { nodes, edges }; | ||
}, | ||
create() { | ||
const { nodes, edges } = this.buildData(); | ||
this.cy = cytoscape({ | ||
container: document.getElementById('cy'), | ||
elements: { | ||
nodes, | ||
edges | ||
}, | ||
zoomingEnabled: false, | ||
style: [ | ||
{ | ||
selector: "node[label]", | ||
style: { | ||
"label": "data(label)", | ||
} | ||
}, | ||
{ | ||
selector: "edge[label]", | ||
style: { | ||
"label": "data(label)", | ||
"width": 3 | ||
} | ||
}, | ||
{ | ||
selector: "edge", | ||
style: { | ||
"width": 1, | ||
"target-arrow-shape": "triangle", | ||
"curve-style": "straight", | ||
} | ||
}, | ||
{ | ||
selector: 'node[categoryColor]', | ||
style: { | ||
"background-color": "data(categoryColor)" | ||
} | ||
} | ||
], | ||
}); | ||
} | ||
}, | ||
}); | ||
type Node = { | ||
id: string, | ||
label: string, | ||
categoryColor: string, | ||
}; | ||
type Edge = { | ||
source: string, | ||
target: string, | ||
}; | ||
</script> |
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