Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(site):Add legend component demo
- Loading branch information
1 parent
3f45237
commit 2fb07a2
Showing
7 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
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
71 changes: 71 additions & 0 deletions
71
packages/graphin-site/examples/components/legend/demo/Filter.jsx
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* eslint-disable no-undef */ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Graphin, { Utils } from '@antv/graphin'; | ||
|
||
import '@antv/graphin/dist/index.css'; // 引入Graphin CSS | ||
import { Legend } from '@antv/graphin-components'; | ||
import '@antv/graphin-components/dist/index.css'; // Graphin 组件 CSS | ||
|
||
const source = Utils.mock(6) | ||
.circle() | ||
.graphin(); | ||
|
||
source.nodes.forEach((node, index) => { | ||
const isCompany = index % 3 === 0; | ||
node.style = { | ||
...node.style, | ||
fontFamily: 'graphin', | ||
icon: isCompany ? 'company' : 'user', | ||
primaryColor: isCompany ? '#873bf4' : '#f79e26', | ||
}; | ||
node.data.type = isCompany ? 'company' : 'person'; | ||
}); | ||
|
||
const App = () => { | ||
const [data, setData] = React.useState(source); | ||
|
||
const legendOptions = [ | ||
{ | ||
label: '公司', | ||
value: 'company', | ||
color: '#873bf4', | ||
}, | ||
{ | ||
label: '人群', | ||
value: 'person', | ||
color: '#f79e26', | ||
}, | ||
]; | ||
|
||
const handleLegend = (_checked, options, LegendProps) => { | ||
// Filter 逻辑 | ||
const optionsMap = options.reduce((acc, curr) => { | ||
acc[curr.value] = curr; | ||
return acc; | ||
}, {}); | ||
|
||
const filterNodes = source.nodes.filter(node => { | ||
return optionsMap[node.data.type].checked; | ||
}); | ||
const ids = filterNodes.map(c => c.id); | ||
const filterEdges = source.edges.filter(edge => { | ||
return ids.indexOf(edge.source) !== -1 && ids.indexOf(edge.target) !== -1; | ||
}); | ||
setData({ | ||
edges: filterEdges, | ||
nodes: filterNodes, | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Graphin data={data} layout={{ name: 'concentric' }}> | ||
<Legend options={legendOptions} onChange={handleLegend} /> | ||
</Graphin> | ||
</div> | ||
); | ||
}; | ||
const rootElement = document.getElementById('container'); | ||
ReactDOM.render(<App />, rootElement); |
67 changes: 67 additions & 0 deletions
67
packages/graphin-site/examples/components/legend/demo/Highlight.jsx
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-disable no-undef */ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Graphin, { Utils } from '@antv/graphin'; | ||
import '@antv/graphin/dist/index.css'; // 引入Graphin CSS | ||
import { Legend } from '@antv/graphin-components'; | ||
import '@antv/graphin-components/dist/index.css'; // Graphin 组件 CSS | ||
|
||
const { nodes, edges } = Utils.mock(6) | ||
.circle() | ||
.graphin(); | ||
console.log(nodes, edges); | ||
nodes.forEach((node, index) => { | ||
const isCompany = index % 3 === 0; | ||
|
||
node.style = { | ||
...node.style, | ||
fontFamily: 'graphin', | ||
icon: isCompany ? 'company' : 'user', | ||
primaryColor: isCompany ? '#873bf4' : '#f79e26', | ||
}; | ||
node.data.type = isCompany ? 'company' : 'person'; | ||
}); | ||
|
||
const App = () => { | ||
const legendOptions = [ | ||
{ | ||
label: '公司', | ||
value: 'company', | ||
color: '#873bf4', | ||
}, | ||
{ | ||
label: '人群', | ||
value: 'person', | ||
color: '#f79e26', | ||
}, | ||
]; | ||
|
||
const handleLegend = (checked, options, LegendProps) => { | ||
const { apis } = LegendProps; | ||
// Highlight 逻辑 | ||
const optionsMap = options.reduce((acc, curr) => { | ||
acc[curr.value] = curr; | ||
return acc; | ||
}, {}); | ||
|
||
const filterNodes = nodes.filter(node => { | ||
return optionsMap[node.data.type].checked; | ||
}); | ||
const nodeIds = filterNodes.map(c => c.id); | ||
console.log(filterNodes, nodeIds); | ||
|
||
apis.highlight(nodeIds); | ||
|
||
// Hide逻辑 | ||
}; | ||
return ( | ||
<div> | ||
<Graphin data={{ nodes, edges }} layout={{ name: 'concentric' }}> | ||
<Legend options={legendOptions} onChange={handleLegend} /> | ||
</Graphin> | ||
</div> | ||
); | ||
}; | ||
const rootElement = document.getElementById('container'); | ||
ReactDOM.render(<App />, rootElement); |
16 changes: 16 additions & 0 deletions
16
packages/graphin-site/examples/components/legend/demo/meta.json
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"title": { | ||
"zh": "中文分类", | ||
"en": "Category" | ||
}, | ||
"demos": [ | ||
{ | ||
"filename": "Highlight.jsx", | ||
"title": "" | ||
}, | ||
{ | ||
"filename": "Filter.jsx", | ||
"title": "" | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: Legend | ||
order: 1 | ||
--- | ||
|
||
Legend is a common matching component for graph analysis. It usually classifies nodes and edges for dyeing and interactive analysis. | ||
|
||
- demo-1: highlight logic | ||
- demo-2: filter logic |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
title: Legend 图例 | ||
order: 1 | ||
--- | ||
|
||
Legend 图例是一种常见的图分析配套组件,通常将节点 和 边 分类后进行染色,交互分析。 | ||
|
||
- demo-1 : highlight 逻辑 | ||
- demo-2 : filter 逻辑 |