Skip to content

Commit d51616d

Browse files
author
Harry Li
committed
added graph recenter button
1 parent 93b70bf commit d51616d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#graph-container {
2+
height: 400px;
3+
4+
position: relative;
5+
6+
.recenter-button {
7+
position: absolute;
8+
top: 0.5rem;
9+
left: 0.5rem;
10+
}
11+
}

src/components/QueryVisualization/QueryGraph/QueryGraph.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
// Copyright (c) 2024 Massachusetts Institute of Technology
22
// SPDX-License-Identifier: MIT
33

4-
import {useMemo} from "react";
4+
import {useMemo, useRef} from "react";
55

66
import Graphin, {Components, LegendChildrenProps} from "@antv/graphin";
77
import '@antv/graphin/dist/index.css';
8-
import { Title } from '@mantine/core';
8+
import { ActionIcon, Button, Title } from '@mantine/core';
99

1010
import { useAppSelector } from "redux/store.ts";
1111

1212
import { parseSparqlQuery } from "utils/parseSparqlQuery.ts";
1313
import { transformTripleQueryToGraphin } from "utils/transformTripleDataToGraphin.ts";
1414
import { useQueryGetIDTableEntitiesFromQuery } from "utils/knowledgeBase/getEntityData.ts";
15+
import { IconFocus } from "@tabler/icons-react";
16+
17+
import styles from "./QueryGraph.module.scss"
1518

1619
const { Legend } = Components;
1720

@@ -26,19 +29,32 @@ export const QueryGraph = () => {
2629
return transformTripleQueryToGraphin(semanticTriples, idTableEntities||undefined);
2730
}, [queryValue, idTableEntities]);
2831

32+
const graphRef = useRef<Graphin>(null)
33+
const center = () => {
34+
if(graphRef.current) {
35+
const graph = graphRef.current.graph;
36+
graph.fitView(); // Re-centers and fits graph to view
37+
}
38+
}
39+
40+
2941
if(queryGraphData.nodes.length===0) return null
3042

3143
return (
3244
<div>
3345
<Title style={{color:"white", marginLeft: 13, marginBottom: 7, marginTop: 7, padding: 1}} order={4}>Query Structure Graph</Title>
34-
<div style={{height: '400px'}}>
35-
<Graphin data={queryGraphData} layout={{type: 'dagre', rankdir: 'LR'}} style={{minHeight: "unset"}}>
46+
<div id={styles["graph-container"]}>
47+
<Graphin data={queryGraphData} ref={graphRef} layout={{type: 'dagre', rankdir: 'LR'}} style={{minHeight: "unset"}}>
3648
<Legend bindType="node" sortKey="data.type">
3749
{(renderProps: LegendChildrenProps) => {
3850
return <Legend.Node {...renderProps} />;
3951
}}
4052
</Legend>
4153
</Graphin>
54+
55+
<ActionIcon className={styles["recenter-button"]} size="sm" variant="filled" aria-label="Center" onClick={() => center()}>
56+
<IconFocus/>
57+
</ActionIcon>
4258
</div>
4359
</div>
4460
)

0 commit comments

Comments
 (0)