Avoiding Low Memory Conditions when Image-Exporting Graphs
Applies to: yFiles for Java 2.9, 2.8, 2.7, 2.6, 2.5, 2.4, 2.3, 2.2, 2.1, 2.0 print article email article

Type: Tips & Tricks

Generating images from a graph needs special precautions when the graph extends a lot in both horizontal and vertical direction.

Exporting the visual representation of a graph, i.e., generating an image from a graph, is a memory intensive task. Regardless of the actual image file format used (JPG, GIF, etc.), the in-memory representation of the image that is to be exported needs at least 4 bytes (in ARGB) for each pixel. Especially when the graph extends a lot in both horizontal and vertical direction, memory consumption during the generation process can quickly reach a multi-megabyte value.

Avoiding low memory conditions during image-exporting a graph can be done in either of two ways:

  • Scaling the graph for the process using a scale factor smaller than 1.0, thus decreasing both width and height of the resulting image.
  • Creating a set of image tiles where each one shows only part of the original image and thus only needs a fraction of the memory during the generation process.

Scaling the graph

Scaling the graph before generating the image can be done using the ViewPortConfigurator.setScalingFactor(double) method when configuring the viewport, for example. For more information on how to use class ViewPortConfigurator, see also this Knowledge Base article.

Image tiling

Image tiling, i.e., generating a number of small images instead of a single big one, substantially decreases the memory footprint during the generation process.

Class TiledImageOutputHandler can be used as a thin wrapper around any of the classes for image export, e.g., GIFIOHandler, or JPGIOHandler. The respective class is then used as a delegate that is invoked from within the tiling process.

The following sample code fragment shows one way class TiledImageOutputHandler can be used.
What the sample code does:

  • Takes an ImageOutputHandler instance and two integral parameters for 'row' and 'column' that determine the number of tiles that will be generated.
  • Optionally, the generated tiles are arranged in an HTML table (using the boolean parameter 'htmlTable').
  • For any invalid parameter values for 'row' or 'column' the given ImageOutputHandler instance is simply returned.

public IOHandler getTilingWrapper(ImageOutputHandler ioh, int row, int column, 
                                  boolean htmlTable) {
  // Check for valid parameter values. 
  if (row < 1 || column < 1) {
    return ioh;
  }
  
  TiledImageOutputHandler tioh = new TiledImageOutputHandler(ioh);
  // Set the tiling accordingly. 
  tioh.setRowCount(row);
  tioh.setColumnCount(column);
  
  // Set HTML table generation accordingly. 
  tioh.setHTMLTableGenerationActive(htmlTable);
  
  return tioh;
}

Keywords: large - image - export - output - tiling - JPG - GIF - PNG - BMP - low - memory - tile - width - height - dimension - ViewPortConfigurator - TiledImageOutputHandler - row - column - HTML

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2012 yWorks · ALL RIGHTS RESERVED imprint | terms of use | privacy policy | home