You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importReact,{useRef,useEffect,useState}from'react';import{ForceGraph2D}from'react-force-graph';importhtml2canvasfrom'html2canvas';constMyGraph=()=>{const[graphData,setGraphData]=useState({nodes: [{id: 'A'},{id: 'B'},{id: 'C'},// ... more nodes],links: [{source: 'A',target: 'B'},{source: 'B',target: 'C'},// ... more links],});constgraphRef=useRef(null);consthandleDownloadImage=async()=>{try{constgraphElement=graphRef.current.container();constcanvas=awaithtml2canvas(graphElement);constdataURL=canvas.toDataURL('image/png');constlink=document.createElement('a');link.href=dataURL;link.download='graph.png';link.click();}catch(error){console.error('Error downloading graph image:',error);}};return(<div><buttononClick={handleDownloadImage}>Download Image</button><ForceGraph2Dref={graphRef}width={600}height={400}graphData={graphData}/></div>);};exportdefaultMyGraph;
Or better on the server side
constpuppeteer=require('puppeteer');asyncfunctiongenerateGraphImage(){constbrowser=awaitpuppeteer.launch();constpage=awaitbrowser.newPage();// Navigate to a URL where your React app is rendered (e.g., a local development server)awaitpage.goto('http://localhost:3000/my-graph-page');// Wait for the graph to be rendered (you might need to use page.$ or page.$$ to target specific elements)// Capture the graph as an imageconstgraphElement=awaitpage.$('#graph-container');// Replace with the actual selectorawaitgraphElement.screenshot({path: 'graph.png'});awaitbrowser.close();}generateGraphImage();
The text was updated successfully, but these errors were encountered:
Or better on the server side
The text was updated successfully, but these errors were encountered: