How to add a title when printing a diagram

Tips & Tricks

Summary

Describes how to add a title when printing a diagram

This article was written for an older version. It is only online as a reference for customers using this old version. The information it contains is probably out of date.

The latest information can be found in the yFiles documentation

Description

Printing a diagram instance of type Graph2D can easily be accomplished by using the yFiles class Graph2DPrinter. Graph2DPrinter lets you conveniently configure the viewport for printing. It can be used for poster printing as well. The yFiles source code demo demo.view.ViewActionDemo shows how a print action can be implemented.

Adding a title string to a diagram is not directly supported by Graph2DView. The easiest and most flexible way to add a title to the page is to subclass Graph2DPrinter and override its print method. The sample below shows how this can be done. Find below the source code of EnhancedGraph2DPrinter.

Since yFiles 2.3 Graph2DPrinter provides most of this as a convenience to the developer by default.
public int print(Graphics g1d, PageFormat pf, int pi) throws PrinterException
{
  Graphics2D gfx = (Graphics2D)g1d.create();

  if(titleText != null && !"".equals(titleText))
  {
    TextLayout tl = new TextLayout(titleText, titleFont, gfx.getFontRenderContext());
    double titleHeight = tl.getBounds().getHeight() + 20;        

   //determine the region for the title and the region for the content.
   Paper p = pf.getPaper();
   Rectangle2D.Double cr = null;
   Rectangle2D.Double tr = null; 
   if(pf.getOrientation() == PageFormat.PORTRAIT)
   {
      cr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY()+titleHeight, 
                                  p.getImageableWidth(), p.getImageableHeight()-titleHeight);
      tr = new Rectangle2D.Double(p.getImageableX(), p.getImageableY(), 
                                  p.getImageableWidth(), titleHeight);
    }
    else if(pf.getOrientation() == PageFormat.LANDSCAPE)
    {
      cr = new Rectangle2D.Double(p.getImageableX()+titleHeight,p.getImageableY(), 
                                  p.getImageableWidth()-titleHeight, p.getImageableHeight());
      tr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY(), 
                                  p.getImageableHeight(), titleHeight);  
    }
    else if(pf.getOrientation() == PageFormat.REVERSE_LANDSCAPE)
    {
      cr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY(), 
                                  p.getImageableWidth()-titleHeight, p.getImageableHeight());
      tr = new Rectangle2D.Double(p.getImageableX(),p.getImageableY(), 
                                  p.getImageableHeight(), titleHeight);  
    }


    //print a title bar that spans the whole page
    if(titleBarColor != null)
    {
      gfx.setColor(Color.lightGray);
      gfx.fill(tr);
    }

    //print the title text
    gfx.setColor(titleColor);
    Rectangle2D tlb = tl.getBounds();
    tl.draw(gfx, 
       (float)(tr.x + (tr.width-tlb.getWidth())/2.0),
       (float)(tr.y + (titleHeight + tlb.getHeight())/2.0));

    //reduce size of the paper so that the content is below the title 
    pf = (PageFormat)pf.clone();
    p.setImageableArea(cr.x, cr.y, cr.width, cr.height);
    pf.setPaper(p);
   }

    //print the content
    return super.print(g1d, pf, pi);
}


Resources

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > Printing a Graph's Visual Representation
Applies to:
yFiles for Java 2: 2.2
Keywords:
print - printing - title - Graph2DPrinter