00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
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         
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         
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         
00134         for (i = 0; i < this.categories.length && i < this.series.length; i++) {
00135             currentCategory = this.categories[i];
00136             currentElement = this.series[i];
00137             
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, 
00211                                                                             
00212                                                                             
00213                 rangeAxis, 
00214                 dataset, 
00215                 PlotOrientation.VERTICAL, 
00216                 true, 
00217                 true, 
00218                 false 
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 }