How to Export a Graph as an Image

Questions & Answers

Summary

Successfully exporting a graph as an image depends on the proper configuration of the graph's view.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

When exporting a graph as an image, the generated result can be influenced in many ways. Anything ranging from zoom level, custom image dimensions, or different clipping areas, to image background is possible.

All of the aforementioned configurations are done on the view containing the actual graph. This view is an instance of type Graph2DView, and class ViewPortConfigurator can be used to conveniently configure it.

Exporting a graph as an image needs the yFiles Viewer package distribution.

The sample code fragment below shows several methods that together can be used to properly configure a dedicated view for image export of a graph.
What the sample code does:

void exportGraphToImageFileFormat(Graph2D graph, ImageOutputHandler ioh, 
                                  String outFile)
{
  Graph2DView originalView = replaceCurrentWithExportView(graph, ioh);
  configureExportView((Graph2DView)graph.getCurrentView());

  exportGraphAsImage(graph, ioh, outFile);
  restoreOriginalView(graph, originalView);
}

Graph2DView replaceCurrentWithExportView(Graph2D graph, ImageOutputHandler ioh)
{
  // Save the currently active view. 
  Graph2DView originalView = (Graph2DView)graph.getCurrentView();

  // Create a new Graph2DView instance with the graph. This will be the 
  // dedicated view for image export. 
  Graph2DView exportView = ioh.createDefaultGraph2DView(graph);
  // Use the Graph2DRenderer instance of the currently active view. 
  exportView.setGraph2DRenderer(originalView.getGraph2DRenderer());

  // Replace the currently active view containing the graph with the "export" 
  // view. 
  graph.setCurrentView(exportView);

  return originalView;
}

void configureExportView(Graph2DView exportView)
{
  ViewPortConfigurator vpc = new ViewPortConfigurator();

  // Register the graph to be exported with the configurator instance. 
  // Depending on the other settings (see below) the graph will be used to 
  // determine the image size, for example. 
  vpc.setGraph2D(exportView.getGraph2D());

  // The graph's bounding box should determine the size of the image. 
  vpc.setSizeType(ViewPortConfigurator.SIZE_USE_ORIGINAL);
  // The complete graph should be exported, hence set the clipping type 
  // accordingly. 
  vpc.setClipType(ViewPortConfigurator.CLIP_GRAPH);

  // Configure the export view using mainly default values, i.e., zoom level 
  // 100%, and 15 pixel margin around the graph's bounding box. 
  vpc.configure(exportView);
}

void exportGraphAsImage(Graph2D graph, IOHandler ioh, String outFile)
{
  try
  {
    // Writing out the graph using the given IOHandler. 
    ioh.write(graph, outFile);
  }
  catch (IOException ioEx)
  {
    // Something went wrong. Complain. 
    printErrorMessage("Cannot export graph as image file '" + outFile + "'.");
  }
}

void restoreOriginalView(Graph2D graph, Graph2DView originalView)
{
  // Remove the "export" view from the graph. 
  graph.removeView(graph.getCurrentView());
  // Reset the current view to the originally active view. 
  graph.setCurrentView(originalView);
}
The sample code uses the default zoom level of 1.0 (i.e., 100%). To set another value for the zoom level the method ViewPortConfigurator.setScalingFactor(double) can be used.

As an example for image export, the yFiles source code demo demo.io.GraphFormatConverter shows how to convert a graph specified in GraphML/Zipped GraphML, YGF, or GML format to a GIF, JPG, SVG or GraphML output.

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Input and Output > Exporting a Graph's Visual Representation
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
image - export - output - view - configuration - ViewPortConfigurator - Graph2DView - JPG - GIF