tags:

views:

49

answers:

3

I have a tree structure that represents an org chart and I need to output the tree in PDF form. I have looked at Prefuse, JFreeChart, yFiles and JGraph but all seem to require that I write some logic. Has anyone done this before? Can anyone recommend a good library to use?

Jeff

A: 

Try AT&T's Graphviz.

Not sure what you mean by "logic", but you have to give the data about parents and children in the hierarchy someplace.

duffymo
I mean I don't want to figure out how many nodes will fit in a given row in my chart. I want to use a library that is smart enough to do the layout. I have all the org chart data.
patriot21
GraphViz is great for that.
Daniel Beck
Graphviz is exactly that - it does the layout for you. I'm not sure that you'll be so happy with the result, because what I've seen wouldn't look like a classic org chart, but it's got the smarts built in.
duffymo
+4  A: 

You can use Jfree Chart to generate a graph/tree. I have used JFree Chat ,it is very simple.you have to put data in 2 d array pass it to method.

/**
 * A simple demonstration application showing how to create a bar chart.
 * http://opengeekz.com
 */
public class CreateBarChart  {

       String aFileName="image1.jpg";
       int width=250;
       int height=200;
       double quality=100;
       JFreeChart chart=null;

/**
 * Create bar chart for given data
 * @param aFileName file name of image.
 * @param width Width of image.
 * @param height Height of image.
 * @param quality Quality of image file .
 */
   public CreateBarChart(String aFileName,int width,int height,double quality)
   {
       this.aFileName=aFileName;
        this.width=width;
       this.height=height;
       this.quality=quality;
   }
   public CreateBarChart()
   {

   }

   /**
    * Accept 2D array for graph row denotes x axis,column denotes y axis.
    * @param barchartdata 2D array for x axis, and y axis.
    * @return CategoryDataset
    */

    private static CategoryDataset createDataset(String barchartdata[][]) {

        // row keys...
        String series1 = "";
        // column keys...

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for(int i=barchartdata.length-1;i>=0;i--)
        {
            if(barchartdata[i][0]==null)
            {
                break;
            }
            else
            {
                String value_str=barchartdata[i][0];
                value_str=value_str.replaceAll("#","");
                float value=0;
                value=Integer.parseInt(value_str);
                dataset.addValue(value, series1,barchartdata[i][1]);
            }

        }
        // create the dataset...
        return dataset;

    }
/**
 *
 * @param barchartdata 2D array for x axis, and y axis.
 * @return JFreeChart object for data.
 */
    public  JFreeChart createChart(String barchartdata[][]) {
        CategoryDataset dataset = createDataset(barchartdata);
        chart = ChartFactory.createBarChart3D(
            "",         // chart title
            "",               // domain axis label
            "",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            false,                     // include legend
            false,                     // tooltips?
            false                     // URLs?
        );

        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

        // set the background color for the chart...
        chart.setBackgroundPaint(Color.white);
        chart.setPadding(RectangleInsets.ZERO_INSETS);
        // get a reference to the plot for further customisation...
        CategoryPlot plot = chart.getCategoryPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setDomainGridlinePaint(Color.white);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.white);
        plot.getDomainAxis().setCategoryMargin(0.0f);

        // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // disable bar outlines...
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(true);


        // set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(
            0.0f, 0.0f,new Color(0,118,112),
            100.0f,100.0f,new Color(0,118,112)
        );

        renderer.setSeriesPaint(0, gp0);

        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(Math.PI /5.0)
        );
        // OPTIONAL CUSTOMISATION COMPLETED.
        try{
        saveToFile(chart, aFileName);
        }catch(Exception ex){ex.printStackTrace();}
        return chart;


    }
/**
 * Save generated chart in jpg file.
 * @param chart Object of JFreeChart class
 * @param aFileName file name where jpg image to be saved.
 * @throws FileNotFoundException
 * @throws IOException
 */
     private  void saveToFile(JFreeChart chart,String aFileName)throws FileNotFoundException, IOException
    {
    BufferedImage img = draw(chart,width,height);
    FileOutputStream fos = new FileOutputStream(aFileName);
    JPEGImageEncoder encoder2 =JPEGCodec.createJPEGEncoder(fos);
    JPEGEncodeParam param2 = encoder2.getDefaultJPEGEncodeParam(img);
    param2.setQuality((float) quality, true);
    encoder2.encode(img,param2);
    fos.close();
    } // saveToFile

     /**
      * Draw bar chat for JFreeChart object
      * @param chart JFreeChart Object
      * @param width Width of image
      * @param height Height of Image
      * @return Object of Buffered Image class
      */
    private static BufferedImage draw(JFreeChart chart, int width, int height)
    {
    BufferedImage img =
    new BufferedImage(width , height,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = img.createGraphics();
    try{
    chart.draw(g2, new Rectangle2D.Double(0, 0, width, height));
    g2.dispose();
    }catch(Exception ex){ex.printStackTrace();}
    return img;
    } // end draw

/**
 *
 * @param args
 * @throws FileNotFoundException
 * @throws IOException
 */
    public static void main(String[] args) throws FileNotFoundException, IOException {

        CreateBarChart demo = new CreateBarChart("f:/image.jpg",250,300,100);
        String data[][]=new String[2][2];
        data[0][0]="30";
        data[0][1]="jan";
        data[1][0]="20";
        data[1][1]="feb";
        demo.createChart(data);
        demo.saveToFile(demo.chart,"iamge.jpg");

    }

}

This is a simple example of Jfree chart. You can use iText for Pdf Generation.

prakash.panjwani
Thanks prakash. I will give this a try and let you know if it works for me.
patriot21
A: 

JGraph does have automatical layouting of nodes, try the tree layout, for example. There's also an example of exporting to PDF using iText.

David
Thanks David I have seen some of the examples in JGraph. I will be doing some prototyping with the JFreeChart code above and some JGraph code.
patriot21