Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

index.tsx 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { Graph } from '@antv/g6';
  2. import { useSize } from 'ahooks';
  3. import { useEffect, useRef } from 'react';
  4. import { graphData } from './constant';
  5. import styles from './index.less';
  6. import { Converter } from './util';
  7. const converter = new Converter();
  8. const nextData = converter.buildNodesAndCombos(
  9. graphData.nodes,
  10. graphData.edges,
  11. );
  12. console.log('🚀 ~ nextData:', nextData);
  13. const finalData = { ...graphData, ...nextData };
  14. const ForceGraph = () => {
  15. const containerRef = useRef<HTMLDivElement>(null);
  16. const size = useSize(containerRef);
  17. const render = () => {
  18. const graph = new Graph({
  19. container: containerRef.current!,
  20. autoFit: 'view',
  21. behaviors: ['drag-element', 'drag-canvas', 'zoom-canvas'],
  22. plugins: [
  23. {
  24. type: 'tooltip',
  25. getContent: (e, items) => {
  26. if (items.every((x) => x?.description)) {
  27. let result = ``;
  28. items.forEach((item) => {
  29. if (item?.description) {
  30. result += `<p>${item?.description}</p>`;
  31. }
  32. });
  33. return result;
  34. }
  35. return undefined;
  36. },
  37. },
  38. ],
  39. layout: {
  40. type: 'combo-combined',
  41. comboPadding: 2,
  42. },
  43. node: {
  44. style: {
  45. size: 20,
  46. labelText: (d) => d.id,
  47. labelPadding: 20,
  48. // labelOffsetX: 20,
  49. labelOffsetY: 5,
  50. },
  51. palette: {
  52. type: 'group',
  53. field: (d) => d.combo,
  54. },
  55. },
  56. edge: {
  57. style: (model) => {
  58. const { size, color } = model.data;
  59. return {
  60. stroke: color || '#99ADD1',
  61. lineWidth: size || 1,
  62. };
  63. },
  64. },
  65. // data: graphData,
  66. });
  67. graph.setData(finalData);
  68. graph.render();
  69. };
  70. useEffect(() => {
  71. render();
  72. }, []);
  73. return <div ref={containerRef} className={styles.container} />;
  74. };
  75. export default ForceGraph;