Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an image capture of the Graph view #347

Open
gkorland opened this issue Jan 2, 2025 · 0 comments · Fixed by #356
Open

Add an image capture of the Graph view #347

gkorland opened this issue Jan 2, 2025 · 0 comments · Fixed by #356
Assignees
Labels
enhancement New feature or request small

Comments

@gkorland
Copy link
Contributor

gkorland commented Jan 2, 2025

import React, { useRef, useEffect, useState } from 'react';
import { ForceGraph2D } from 'react-force-graph';
import html2canvas from 'html2canvas'; 

const MyGraph = () => {
  const [graphData, setGraphData] = useState({
    nodes: [
      { id: 'A' },
      { id: 'B' },
      { id: 'C' },
      // ... more nodes
    ],
    links: [
      { source: 'A', target: 'B' },
      { source: 'B', target: 'C' },
      // ... more links
    ],
  });
  const graphRef = useRef(null);

  const handleDownloadImage = async () => {
    try {
      const graphElement = graphRef.current.container(); 
      const canvas = await html2canvas(graphElement);
      const dataURL = canvas.toDataURL('image/png'); 

      const link = document.createElement('a');
      link.href = dataURL;
      link.download = 'graph.png'; 
      link.click();
    } catch (error) {
      console.error('Error downloading graph image:', error);
    }
  };

  return (
    <div>
      <button onClick={handleDownloadImage}>Download Image</button>
      <ForceGraph2D
        ref={graphRef}
        width={600}
        height={400}
        graphData={graphData}
      />
    </div>
  );
};

export default MyGraph;

Or better on the server side

const puppeteer = require('puppeteer');

async function generateGraphImage() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Navigate to a URL where your React app is rendered (e.g., a local development server)
  await page.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 image
  const graphElement = await page.$('#graph-container'); // Replace with the actual selector
  await graphElement.screenshot({ path: 'graph.png' });

  await browser.close();
}

generateGraphImage();
@gkorland gkorland added the enhancement New feature or request label Jan 2, 2025
@Anchel123 Anchel123 self-assigned this Jan 19, 2025
@barakb barakb added the small label Jan 19, 2025
@Anchel123 Anchel123 linked a pull request Jan 21, 2025 that will close this issue
Anchel123 added a commit that referenced this issue Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request small
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants