org/objectweb/proactive/benchmarks/timit/util/charts/Line2dChart.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.benchmarks.timit.util.charts;
00032 
00033 import java.awt.Rectangle;
00034 import java.util.Iterator;
00035 import java.util.List;
00036 import org.jdom.Element;
00037 import org.jdom.filter.ElementFilter;
00038 import org.jfree.chart.ChartFactory;
00039 import org.jfree.chart.ChartUtilities;
00040 import org.jfree.chart.JFreeChart;
00041 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
00042 import org.jfree.chart.plot.CategoryPlot;
00043 import org.jfree.chart.plot.PlotOrientation;
00044 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
00045 import org.jfree.chart.title.TextTitle;
00046 import org.jfree.data.category.CategoryDataset;
00047 import org.jfree.data.category.DefaultCategoryDataset;
00048 import org.objectweb.proactive.benchmarks.timit.config.ConfigChart;
00049 import org.objectweb.proactive.benchmarks.timit.util.BenchmarkStatistics;
00050 import org.objectweb.proactive.benchmarks.timit.util.XMLHelper;
00051 
00058 public class Line2dChart implements Chart {
00059 
00063     private static final long serialVersionUID = -3848003891108767655L;
00064 
00065     private Element[] series;
00066 
00067     private Comparable[] categories;
00068 
00069     private String wantedTag;
00070 
00071     private String[] names;
00072 
00073     private String selectedAttributeName;
00074 
00075     public void generateChart(Element eTimit, BenchmarkStatistics bstats,
00076             ConfigChart cChart) {
00077 
00078         Element eTimitClone = (Element) eTimit.clone();
00079 
00080         // Apply filter on elements
00081         while(true ) {
00082             Iterator<Element> it =
00083                 eTimitClone.getDescendants(new ElementFilter(cChart.get("tag")));
00084             try {
00085                 while (it.hasNext()) {
00086                     XMLHelper.tagFiltering(
00087                             it.next(),
00088                             cChart.get("filter").split(","));
00089                 }
00090             } catch( java.util.ConcurrentModificationException e ) {
00091                 continue;
00092             }
00093             break;
00094         }
00095 
00096         
00097         // Get values from XML tree (Element)
00098         List fstats = eTimitClone.getChildren();
00099         this.series = new Element[fstats.size()];
00100         this.categories = new Comparable[fstats.size()];
00101 
00102         for (int i = 0; i < fstats.size(); i++) {
00103             Element fstat = (Element) fstats.get(i);
00104             fstat.removeContent(new ElementFilter(cChart.get("tag")).negate());
00105             this.series[i] = fstat.getChild(cChart.get("tag"));
00106             this.categories[i] = fstat.getAttributeValue("name");
00107         }
00108         this.wantedTag =
00109             ((Element) this.series[0].getChildren().get(0)).getName();
00110         this.names = cChart.get("filter").split(",");
00111         this.selectedAttributeName = cChart.get("attribute");
00112         
00113 
00114         this.buildFinalChart(cChart);
00115     }
00116 
00122     private CategoryDataset createDataset() {
00123 
00124         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
00125 
00126         int i, k;
00127         Comparable currentCategory;
00128         Element currentElement, iteratedElement;
00129         Iterator it;
00130         String currentTagName, attributeNameValue, stringValue;
00131         double value;
00132 
00133         // Iterate through the categories
00134         for (i = 0; i < this.categories.length && i < this.series.length; i++) {
00135             currentCategory = this.categories[i];
00136             currentElement = this.series[i];
00137             // Iterate through the selected Element
00138             it = currentElement.getDescendants();
00139             while (it.hasNext()) {
00140                 Object o = it.next();
00141                 if (o instanceof Element) {
00142                     iteratedElement = (Element) o;
00143                     currentTagName = iteratedElement.getName();
00144                     if (currentTagName.equals(this.wantedTag)) {
00145                         for (k = 0; k < this.names.length; k++) {
00146                             attributeNameValue = iteratedElement
00147                                     .getAttributeValue("name");
00148                             if (attributeNameValue != null
00149                                     && attributeNameValue.equals(this.names[k])) {
00150                                 stringValue = iteratedElement
00151                                         .getAttributeValue(this.selectedAttributeName);
00152                                 if (stringValue != null) {
00153                                     try {
00154                                         value = Double.parseDouble(stringValue);
00155                                         dataset
00156                                                 .setValue(
00157                                                         value,
00158                                                         attributeNameValue
00159                                                                 + " ("
00160                                                                 + this.selectedAttributeName
00161                                                                 + ")",
00162                                                         currentCategory);
00163                                     } catch (NumberFormatException ex) {
00164                                         ex.printStackTrace();
00165                                     }
00166                                 } else {
00167                                     System.out.println("No attribute "
00168                                             + this.selectedAttributeName
00169                                             + " for tag " + this.wantedTag
00170                                             + " with name " + this.names[k]);
00171                                 }
00172                             }
00173                         }
00174                     }
00175                 }
00176             }
00177         }
00178 
00179         return dataset;
00180     }
00181 
00186     private void buildFinalChart(ConfigChart cChart) {
00187         this.buildFinalChart(cChart.get("title"), cChart.get("subTitle"),
00188                 cChart.get("filename"), cChart.get("xaxislabel"), cChart
00189                         .get("yaxislabel"), Integer
00190                         .valueOf(cChart.get("width")), Integer.valueOf(cChart
00191                         .get("height")));
00192     }
00193 
00204     private void buildFinalChart(String title, String subTitle,
00205             String fileName, String domainAxis, String rangeAxis, int width,
00206             int height) {
00207 
00208         CategoryDataset dataset = this.createDataset();
00209 
00210         JFreeChart chart = ChartFactory.createLineChart(title, domainAxis, // domain
00211                                                                             // axis
00212                                                                             // label
00213                 rangeAxis, // range axis label
00214                 dataset, // data
00215                 PlotOrientation.VERTICAL, // orientation
00216                 true, // include legend
00217                 true, // tooltips?
00218                 false // URLs?
00219                 );
00220         chart.addSubtitle(new TextTitle(subTitle));
00221 
00222         final LineAndShapeRenderer renderer = new LineAndShapeRenderer();
00223         renderer
00224                 .setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
00225 
00226         CategoryPlot c = chart.getCategoryPlot();
00227         c.setRenderer(renderer);
00228         c.setRangeGridlinesVisible(true);
00229 
00230         try {
00231             ChartUtilities.saveChartAsPNG(new java.io.File(fileName + ".png"),
00232                     chart, width, height);
00233 
00234             Utilities.saveChartAsSVG(chart, new Rectangle(width, height),
00235                     new java.io.File(fileName + ".svg"));
00236         } catch (java.io.IOException e) {
00237             System.err.println("Error writing image to file");
00238             e.printStackTrace();
00239         }
00240     }
00241 }

Generated on Mon Jan 22 15:16:04 2007 for ProActive by  doxygen 1.5.1