KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > charts > ChartHelper


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23  
24 package org.infoglue.deliver.util.charts;
25
26 import java.io.File JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.infoglue.cms.util.CmsPropertyHandler;
30 import org.infoglue.deliver.controllers.kernel.impl.simple.TemplateController;
31 import org.jfree.chart.ChartUtilities;
32
33 /**
34  * @author Mattias Bogeblad
35  *
36  * This is the helper class for charts. It gives the user a possibility to
37  * invoke different charts and supply data.
38  */

39
40 public class ChartHelper
41 {
42     private final static Logger logger = Logger.getLogger(ChartHelper.class.getName());
43
44     private TemplateController templateController = null;
45     
46     /**
47      * The default constructor.
48      */

49
50     public ChartHelper(TemplateController templateController)
51     {
52         this.templateController = templateController;
53     }
54     
55     /**
56      * This method invokes a named chart with the data supplied in xml-format.
57      */

58     
59     public String JavaDoc renderGraph(String JavaDoc chartName, String JavaDoc dataAsXML, int width, int height)
60     {
61         String JavaDoc assetUrl = "";
62
63         try
64         {
65             XMLDataDiagram demo = null;
66             
67             if(chartName.equals("TimeSeriesDiagram"))
68             {
69                 demo = new TimeSeriesDiagram();
70                 demo.setDiagramData(dataAsXML);
71                 demo.renderChart();
72             }
73             else
74             {
75                 //Todo - for now...
76
demo = new TimeSeriesDiagram();
77                 demo.setDiagramData(dataAsXML);
78                 demo.renderChart();
79             }
80             
81             String JavaDoc uniqueId = chartName + width + height + dataAsXML.length();
82             String JavaDoc fileName = uniqueId + ".png";
83             
84             int i = 0;
85             String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
86             File JavaDoc file = null;
87             while(filePath != null)
88             {
89                 file = new File JavaDoc(filePath + java.io.File.separator + fileName);
90                 ChartUtilities.saveChartAsPNG(file, demo.getChart(), width, height);
91                 i++;
92                 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
93             }
94
95             /*
96             String filePath = CmsPropertyHandler.getDigitalAssetPath();
97             File file = new File(filePath + java.io.File.separator + fileName);
98             ChartUtilities.saveChartAsPNG(file, demo.getChart(), width, height);
99             */

100             
101             assetUrl = this.templateController.getDigitalAssetBaseUrl() + "/" + file.getName();
102         }
103         catch(Exception JavaDoc e)
104         {
105             logger.warn("An error occurred when we tried to generate a graph:" + e.getMessage(), e);
106         }
107         
108         return assetUrl;
109     }
110     
111 }
112
Popular Tags