00001 package cedar.hepdata.webapp.pages;
00002
00003 import cedar.hepdata.model.*;
00004 import cedar.hepdata.xml.*;
00005 import cedar.hepdata.db.*;
00006
00007 import org.apache.tapestry5.annotations.*;
00008 import org.apache.tapestry5.ioc.annotations.*;
00009 import org.apache.tapestry5.services.*;
00010
00011 import org.hibernate.*;
00012 import org.hibernate.criterion.*;
00013 import java.util.*;
00014
00015 import java.lang.*;
00016
00017 import java.awt.image.BufferedImage;
00018 import java.io.ByteArrayInputStream;
00019 import java.io.ByteArrayOutputStream;
00020 import java.io.IOException;
00021 import java.io.InputStream;
00022 import java.util.List;
00023
00024 import org.apache.tapestry5.ComponentResources;
00025 import org.apache.tapestry5.Link;
00026 import org.apache.tapestry5.MarkupWriter;
00027 import org.apache.tapestry5.StreamResponse;
00028 import org.apache.tapestry5.annotations.*;
00029 import org.apache.tapestry5.ioc.annotations.*;
00030 import org.apache.tapestry5.annotations.*;
00031 import org.apache.tapestry5.ioc.annotations.*;
00032 import org.apache.tapestry5.ioc.services.TypeCoercer;
00033 import org.apache.tapestry5.dom.Element;
00034 import org.apache.tapestry5.services.Response;
00035 import org.jfree.chart.ChartUtilities;
00036 import org.jfree.chart.JFreeChart;
00037 import org.jfree.chart.title.TextTitle;
00038 import org.jfree.data.xy.*;
00039
00040 import org.jfree.chart.JFreeChart;
00041 import org.jfree.chart.axis.NumberAxis;
00042 import org.jfree.chart.axis.ValueAxis;
00043 import org.jfree.chart.axis.LogarithmicAxis;
00044 import org.jfree.chart.plot.XYPlot;
00045 import org.jfree.chart.renderer.xy.XYErrorRenderer;
00046 import org.jfree.chart.renderer.xy.XYItemRenderer;
00047 import org.jfree.data.xy.XYIntervalSeries;
00048 import org.jfree.data.xy.XYIntervalSeriesCollection;
00049 import org.jfree.data.Range;
00050
00051 public class ShapesDoPlot{
00052
00053 @Inject
00054 private org.hibernate.Session _session;
00055 @Inject
00056 private ComponentResources _resources;
00057
00058 @Persist
00059 private int _nplots;
00060 @Persist
00061 private List <Integer> _plots = new ArrayList();
00062 @Persist
00063 private Map <Integer,String> _expnames = new HashMap();
00064 @Persist
00065 private Map <Integer,Integer> _irns = new HashMap();
00066 @Persist
00067 private Map <Integer,Integer> _dsids = new HashMap();
00068 @Persist
00069 private Map <Integer,Integer> _yaids = new HashMap();
00070 @Persist
00071 private Map <Integer,Boolean> _ckbxs = new HashMap();
00072 @Persist
00073 private Map <Integer,String> _ecms = new HashMap();
00074 @Persist
00075 private Map <Integer,String> _vars = new HashMap();
00076
00077 @Persist
00078 private String _ylinlog;
00079
00080 public void setPlots(List plots){ _plots=plots; _nplots=_plots.size()-1; }
00081 public void setExpnames(Map expnames) { _expnames = expnames; }
00082 public void setIrns(Map irns) { _irns = irns; }
00083 public void setDsids(Map dsids) { _dsids = dsids; }
00084 public void setYaids(Map yaids) { _yaids = yaids; }
00085 public void setCkbxs(Map ckbxs) { _ckbxs = ckbxs; }
00086 public void setEcms(Map ecms) { _ecms = ecms; }
00087 public void setVars(Map vars) { _vars = vars; }
00088 public void setYlinlog(String ylinlog){ _ylinlog = ylinlog; }
00089
00090 private int _irn;
00091 private int _dsid;
00092 private int _yaid;
00093
00094 private YAxis getYAxis(){return this.getYAxis(0);}
00095 private YAxis getYAxis(int nplot){
00096 Query q =null;
00097 q = _session.createQuery("select y from Paper p, Dataset d, YAxis y where p._spiresId=:irn and d._localId=:dsid and d._paper=p and y._localId=:yaid and y._dataset=d");
00098 q.setLong("irn",_irns.get(nplot));
00099 q.setLong("dsid",_dsids.get(nplot));
00100 q.setLong("yaid",_yaids.get(nplot));
00101 return (YAxis) q.list().get(0);
00102
00103 }
00104
00105 static private int numberRun = 0;
00106 static private HashMap seriesMap = new HashMap();
00107 static private String titlestring = "Title:";
00108
00109 public Link getChart1(){
00110 if (numberRun > 10000) {
00111 numberRun=0;
00112 }
00113 numberRun += _nplots+1;
00114 int _nn = -1;
00115 for (int n = 0; n<=_nplots; n++){
00116 if( _ckbxs.get(n) ){
00117 _nn += 1;
00118 XYIntervalSeries series = getDataSeries(n,getYAxis(n));
00119 seriesMap.put(numberRun+_nn,series);
00120 }
00121 }
00122 return _resources.createActionLink("chart", false, new Object[]{"400","400",numberRun,_nn+1});
00123 }
00124
00125 public XYIntervalSeries getDataSeries(int n, YAxis yaxis){
00126 XAxis _xaxis = yaxis.getDataset().getXAxis(1);
00127 for (String comment : yaxis.getDataset().getComments()){
00128 titlestring = comment;
00129 }
00130
00131 XYIntervalSeries series = new XYIntervalSeries(
00132 _expnames.get(n)
00133 .concat("/")
00134 .concat(_irns.get(n).toString())
00135 .concat("/")
00136 .concat(_dsids.get(n).toString())
00137 .concat("/")
00138 .concat(_yaids.get(n).toString())
00139 .concat("/")
00140 .concat(_ecms.get(n))
00141 );
00142 for (int ip=1; ip<=yaxis.getPoints().size(); ip++){
00143 double y = yaxis.getPoint(ip).getValue();
00144 double eplus = 0.0;
00145 double eminus = 0.0;
00146 for (Uncertainty e : yaxis.getPoint(ip).getErrors()){
00147 eplus = eplus + e.getPlus()*e.getPlus();
00148 eminus = eminus - e.getMinus()*e.getMinus();
00149 }
00150 eplus = Math.sqrt(eplus);
00151 eminus = Math.sqrt(eminus);
00152 double yhigh = y + eplus;
00153 double ylow = y - eplus;
00154 double x;
00155 double xhigh;
00156 double xlow;
00157 if(_xaxis.getBin(ip).getLowValue() != null && _xaxis.getBin(ip).getHighValue() != null){
00158 x = (_xaxis.getBin(ip).getLowValue()+_xaxis.getBin(ip).getHighValue())/2;
00159 xlow = _xaxis.getBin(ip).getLowValue();
00160 xhigh = _xaxis.getBin(ip).getHighValue();
00161 }
00162 else{
00163 x = _xaxis.getBin(ip).getFocus();
00164 xhigh = _xaxis.getBin(ip).getFocus();
00165 xlow = _xaxis.getBin(ip).getFocus();
00166 }
00167 series.add(x,xlow,xhigh,y,ylow,yhigh);
00168 }
00169 return series;
00170 }
00171 public StreamResponse onChart(final int width, final int height, final int numberRun, int number){
00172
00173 final XYIntervalSeriesCollection collection = new XYIntervalSeriesCollection();
00174 for (int n=0; n<number; n++){
00175 XYIntervalSeries series = (XYIntervalSeries) seriesMap.get(numberRun+n);
00176 seriesMap.remove(numberRun+n);
00177 collection.addSeries(series);
00178 }
00179 IntervalXYDataset data1 = collection;
00180
00181 final XYItemRenderer renderer1 = new XYErrorRenderer();
00182 final Range rangex = new Range(0.0,1.0);
00183 System.out.println(rangex.toString());
00184 final NumberAxis rangeAxisX = new NumberAxis(getYAxis(0).getDataset().getXAxis(1).getHeader());
00185 rangeAxisX.setRange(rangex);
00186
00187 NumberAxis rangeAxisY = null;
00188 String _label = getYAxis(0).getHeader();
00189 System.out.println("log lin is " + _ylinlog);
00190 if(_ylinlog.equals("YLIN")) {
00191 rangeAxisY = new NumberAxis(_label);
00192 }
00193 else{
00194 rangeAxisY = new LogarithmicAxis(_label);
00195 ((LogarithmicAxis)rangeAxisY).setAllowNegativesFlag(true);
00196 ((LogarithmicAxis)rangeAxisY).setAutoRangeNextLogFlag(true);
00197 }
00198 final XYPlot subplot1 = new XYPlot(data1, rangeAxisX, rangeAxisY, renderer1);
00199 final TextTitle title = new TextTitle(titlestring);
00200 final JFreeChart chart = new JFreeChart(subplot1);
00201 chart.addSubtitle(title);
00202 return new StreamResponse(){
00203 public String getContentType(){
00204 return "image/png";
00205 }
00206 public InputStream getStream() throws IOException {
00207 BufferedImage image = chart.createBufferedImage(width, height);
00208 ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
00209 ChartUtilities.writeBufferedImageAsPNG(byteArray, image) ;
00210 return new ByteArrayInputStream(byteArray.toByteArray());
00211 }
00212 public void prepareResponse(Response response){}
00213 };
00214 }
00215
00216 }