Page MenuHomeHEPForge

PlotCombinedImage.java
No OneTemporary

PlotCombinedImage.java

package cedar.hepdata.webapp.pages;
import cedar.hepdata.model.*;
import cedar.hepdata.xml.*;
import cedar.hepdata.db.*;
import cedar.hepdata.webapp.plot.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.services.*;
import org.hibernate.*;
import org.hibernate.criterion.*;
import java.util.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.List;
import java.util.*;
import java.util.regex.*;
import java.awt.image.*;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Ellipse2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.font.TextAttribute;
import java.io.File;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.DefaultFontMapper;
import com.itextpdf.text.pdf.BaseFont;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.EventContext;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.ioc.services.TypeCoercer;
import org.apache.tapestry5.dom.Element;
import org.apache.tapestry5.services.Response;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.xy.*;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYErrorRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;
import org.jfree.data.Range;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.TextAnchor;
import java.awt.Color;
public class PlotCombinedImage {
@Inject
private RequestGlobals _reqGlobals;
@Inject
private Request _req;
public Request getRequest() { return _req; }
@Inject
private org.hibernate.Session _session;
private List <String> _saved;
public void setSavedList(List saved){
this._saved=saved;
}
public List <String> getSavedList(){
return this._saved;
}
private List <String> _yaxislist = new ArrayList();
private String _xscale = null;
private String _yscale = null;
private String _xmin = null;
private String _xmax = null;
private String _ymin = null;
private String _ymax = null;
private String _xsize = null;
private String _ysize = null;
private String _plotType = null;
private String _xkey = null;
private String _ykey = null;
private String _xtext = null;
private String _ytext = null;
private String _xheader = null;
private String _yheader = null;
private String[] _optionstring = new String[11];
private Paper _paper=null;
public List <YAxis> getYAxes(){
List <YAxis> _selected = new ArrayList();
for (String saved : _yaxislist){
int i1 = saved.indexOf("d");
String hepid = saved.substring(4,i1);
int i2 = saved.indexOf("y");
String dsid = saved.substring(i1+2,i2);
String yid = saved.substring(i2+2);
Query q=null;
q = _session.createQuery("select distinct p from Paper p where p._hepdataId = " + hepid);
_paper = (Paper) q.uniqueResult();
_selected.add(_paper.getDataset(Integer.parseInt(dsid)).getYAxis(Integer.parseInt(yid)));
}
return _selected;
}
private Double OptionHasScale(int n){
Double rtn = null;
String _teststring=_optionstring[n];
if(_teststring != null){
if(_teststring.contains("scale")){
System.out.println("teststring " + _teststring);
String sh = _teststring.substring(_teststring.indexOf("scale"));
int len = sh.length();
if(sh.indexOf(",") > -1) len = sh.indexOf(",");
if(sh.substring(6).indexOf(" ") > -1) len = sh.substring(6).indexOf(" ")+6;
if( len > 6) rtn = Double.valueOf(sh.substring(6,len));
}
}
return rtn;
}
private String OptionHasText(int n){
String rtn = null;
String _teststring=_optionstring[n];
if(_teststring != null){
if(_teststring.contains("text")){
String sh = _teststring.substring(_teststring.indexOf("text"));
int len = sh.length();
if(sh.indexOf(",") > -1) len = sh.indexOf(",");
if( len > 5) rtn = sh.substring(5,len);
}
}
return rtn;
}
private String OptionHasColor(int n){
String rtn = null;
String _teststring=_optionstring[n];
if(_teststring != null){
if(_teststring.contains("black")){ rtn = "black"; }
if(_teststring.contains("gray")){ rtn = "gray"; }
if(_teststring.contains("darkgray")){ rtn = "darkGray"; }
if(_teststring.contains("lightgray")){ rtn = "lightGray"; }
if(_teststring.contains("magenta")){ rtn = "magenta"; }
if(_teststring.contains("red")){ rtn = "red"; }
if(_teststring.contains("pink")){ rtn = "pink"; }
if(_teststring.contains("orange")){ rtn = "orange"; }
if(_teststring.contains("yellow")){ rtn = "yellow"; }
if(_teststring.contains("green")){ rtn = "green"; }
if(_teststring.contains("cyan")){ rtn = "cyan"; }
if(_teststring.contains("blue")){ rtn = "blue"; }
if(_teststring.contains("black")){ rtn = "black"; }
}
return rtn;
}
private String OptionHasShape(int n){
String rtn = null;
String _teststring=_optionstring[n];
if(_teststring != null){
if(_teststring.contains("circle")){ rtn = "circle"; }
else if(_teststring.contains("triangle")){
if(_teststring.contains("invert")) { rtn = "triangle invert"; }
else if(_teststring.contains("left")) { rtn = "triangle left"; }
else if(_teststring.contains("right")) { rtn = "triangle right"; }
else { rtn = "triangle"; }
}
else if(_teststring.contains("square")){ rtn = "square"; }
else if(_teststring.contains("diamond")){ rtn = "diamond"; }
else if(_teststring.contains("star")){ rtn = "star"; }
else { rtn = "circle"; }
}
return rtn;
}
private String OptionHasOpen(int n){
String rtn = null;
String _teststring=_optionstring[n];
if(_teststring != null){
if(_teststring.contains("open")){ rtn = "open"; }
}
return rtn;
}
public StreamResponse onActivate(EventContext context) {
final XYIntervalSeriesCollection collection = new XYIntervalSeriesCollection();
System.out.println("context = "+context);
if (context.getCount() > 0) {
String ps = context.get(String.class, 0);
while (ps.contains("insp")){
Matcher m = Pattern.compile("(.*)insp(\\d+)(ds\\d+ya\\d+.*)").matcher(ps);
while (m.find()){
Query q=null;
q = _session.createQuery("select distinct p from Paper p where p._inspireId = " + m.group(2));
Paper p = (Paper) q.uniqueResult();
Long hepid = p.getHepdataId();
ps=m.group(1)+"save"+hepid+m.group(3);
}
}
// System.out.println("ps: "+ps);
Matcher m = Pattern.compile("(save\\d+ds\\d+ya\\d+)").matcher(ps);
_yaxislist.clear();
while (m.find()){_yaxislist.add(m.group(1));}
m = Pattern.compile("xscale:(lin|log)").matcher(ps);
while (m.find()){_xscale=m.group(1);}
m = Pattern.compile("yscale:(lin|log)").matcher(ps);
while (m.find()){ _yscale=m.group(1);}
m = Pattern.compile("xmin:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){ _xmin=m.group(1);}
m = Pattern.compile("xmax:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){ _xmax=m.group(1);}
m = Pattern.compile("ymin:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){_ymin=m.group(1);}
m = Pattern.compile("ymax:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){_ymax=m.group(1);}
m = Pattern.compile("xsize:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){_xsize=m.group(1);}
m = Pattern.compile("ysize:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){_ysize=m.group(1);}
m = Pattern.compile("xkey:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){_xkey=m.group(1);}
m = Pattern.compile("ykey:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){ _ykey=m.group(1);}
m = Pattern.compile("xtext:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){_xtext=m.group(1);}
m = Pattern.compile("ytext:([\\+\\-\\.\\d]+)").matcher(ps);
while (m.find()){ _ytext=m.group(1);}
m = Pattern.compile("yheader:([\\d\\w\\+\\-]+)").matcher(ps);
while (m.find()){_yheader=m.group(1);}
m = Pattern.compile("xheader:([\\d\\w\\+\\-]+)").matcher(ps);
while (m.find()){_xheader=m.group(1);}
m = Pattern.compile("plotType:([\\d\\w\\+\\-]+)").matcher(ps);
while (m.find()){_plotType=m.group(1);}
for (int i=0; i<=10; i++){
m = Pattern.compile("optionstring" + i + ":([=\\d\\w\\+\\-\\s\\.]+)").matcher(ps);
while (m.find()){ _optionstring[i]=m.group(1);}
}
}
int count = 0;
XYIntervalSeries series = null;
double xlow = 1000000.0;
double xhigh = -1000000.0;
double ylow = 1000000.0;
double ylowpos = 1000000.0;
double yhigh = -1000000.0;
String titlestring = "";
String xlabel = "";
String ylabel = "";
for ( YAxis ya: getYAxes()) {
count += 1;
int xid = 1;
series = DataSeries.getDataSeries(ya,xid,count,_xscale,_yscale,OptionHasScale(count),true);
for (int nn=0; nn<series.getItemCount(); nn++){
if(series.getXLowValue(nn) < xlow){ xlow = series.getXLowValue(nn);};
if(series.getXHighValue(nn) > xhigh){ xhigh = series.getXHighValue(nn);};
if(series.getYLowValue(nn) < ylow){ ylow = series.getYLowValue(nn);};
if(series.getYLowValue(nn) > 0.0 && series.getYLowValue(nn) < ylowpos){ ylowpos = series.getYLowValue(nn);};
if(series.getYHighValue(nn) > yhigh){ yhigh = series.getYHighValue(nn);};
}
collection.addSeries(series);
// titlestring += DataSeries.getTitleString(ya,count) + "\n";
xlabel = DataSeries.getXlabel(ya,xid);
ylabel = DataSeries.getYlabel(ya);
}
if(_xscale.equals("lin")){
xlow = xlow - 0.05*(xhigh-xlow);
xhigh = xhigh + 0.05*(xhigh-xlow);
} else{
xlow = 0.9*xlow;
xhigh = 1.10*xhigh;
}
if(_yscale.equals("lin")){
ylow = ylow - 0.05*(yhigh-ylow);
yhigh = yhigh + 0.05*(yhigh-ylow);
} else{
ylow = 0.9*ylow;
yhigh = 1.10*yhigh;
}
if(xlow == xhigh){
xlow = 0.90*xlow;
xhigh = 1.10*xhigh;
}
if(_xmax != null) { xhigh = Double.valueOf(_xmax); } else { _xmax=(Double.toString(xhigh)); }
if(_xmin != null) { xlow = Double.valueOf(_xmin); } else { _xmin=Double.toString(xlow); }
if(_ymax != null) { yhigh = Double.valueOf(_ymax); } else { _ymax=Double.toString(yhigh); }
if(_yscale.equals("lin")){
if(_ymin != null) { ylow = Double.valueOf(_ymin); } else { _ymin=Double.toString(ylow); }
} else {
if(_ymin != null) { ylowpos = Double.valueOf(_ymin); } else { _ymin=Double.toString(ylowpos); }
}
final XYErrorRenderer renderer1 = new XYErrorRenderer();
double pointsize = (Double.valueOf(_xsize)+Double.valueOf(_ysize))/150.0;
double offset = -pointsize/2.0;
int off = (int) offset;
Ellipse2D circle = new Ellipse2D.Double(offset,offset, pointsize, pointsize);
Rectangle2D square = new Rectangle2D.Double(offset, offset, pointsize, pointsize);
Polygon downtriangle = new Polygon();
downtriangle.addPoint(0,-2*off);
downtriangle.addPoint(-off,off);
downtriangle.addPoint(off,off);
Polygon triangle = new Polygon();
triangle.addPoint(0,2*off);
triangle.addPoint(-off,-off);
triangle.addPoint(off,-off);
Polygon righttriangle = new Polygon();
righttriangle.addPoint(-2*off,0);
righttriangle.addPoint(off,off);
righttriangle.addPoint(off,-off);
Polygon lefttriangle = new Polygon();
lefttriangle.addPoint(-off,off);
lefttriangle.addPoint(2*off,0);
lefttriangle.addPoint(-off,-off);
Polygon star = new Polygon();
star.addPoint(0,2*off);
star.addPoint(off/2,off/2);
star.addPoint(2*off,0);
star.addPoint(off/2,-off/2);
star.addPoint(0,-2*off);
star.addPoint(-off/2,-off/2);
star.addPoint(-2*off,0);
star.addPoint(-off/2,off/2);
Polygon diamond = new Polygon();
diamond.addPoint(0,2*off);
diamond.addPoint(off,off);
diamond.addPoint(2*off,0);
diamond.addPoint(off,-off);
diamond.addPoint(0,-2*off);
diamond.addPoint(-off,-off);
diamond.addPoint(-2*off,0);
diamond.addPoint(-off,off);
renderer1.setCapLength(3);
for (int m=1; m<=collection.getSeriesCount(); m++){
System.out.println("checking color: " + m + " " + collection.getSeriesCount() + " " + OptionHasColor(m));
if(OptionHasColor(m) != null){
if(OptionHasColor(m).equals("black")) { renderer1.setSeriesPaint(m-1,Color.black); }
else if(OptionHasColor(m).equals("darkGray")) { renderer1.setSeriesPaint(m-1,Color.darkGray); }
else if(OptionHasColor(m).equals("gray")) { renderer1.setSeriesPaint(m-1,Color.gray); }
else if(OptionHasColor(m).equals("lightGray")){ renderer1.setSeriesPaint(m-1,Color.lightGray); }
else if(OptionHasColor(m).equals("magenta")) { renderer1.setSeriesPaint(m-1,Color.magenta); }
else if(OptionHasColor(m).equals("red")) { renderer1.setSeriesPaint(m-1,Color.red);}
else if(OptionHasColor(m).equals("pink")) { renderer1.setSeriesPaint(m-1,Color.pink);}
else if(OptionHasColor(m).equals("orange")) { renderer1.setSeriesPaint(m-1,Color.orange);}
else if(OptionHasColor(m).equals("yellow")) { renderer1.setSeriesPaint(m-1,Color.yellow);}
else if(OptionHasColor(m).equals("green")) { renderer1.setSeriesPaint(m-1,Color.green);}
else if(OptionHasColor(m).equals("cyan")) { renderer1.setSeriesPaint(m-1,Color.cyan);}
else if(OptionHasColor(m).equals("blue")) { renderer1.setSeriesPaint(m-1,Color.blue); }
else { renderer1.setSeriesPaint(m-1,Color.black);}
}
}
for (int m=1; m<=collection.getSeriesCount(); m++){
System.out.println("checking shape: " + m + " " + collection.getSeriesCount() + " " + OptionHasShape(m));
if(OptionHasShape(m) != null){
if(OptionHasShape(m).contains("circle")) { renderer1.setSeriesShape(m-1,circle); }
else if(OptionHasShape(m).contains("square")) { renderer1.setSeriesShape(m-1,square); }
else if(OptionHasShape(m).contains("star")) { renderer1.setSeriesShape(m-1,star); }
else if(OptionHasShape(m).contains("diamond")) { renderer1.setSeriesShape(m-1,diamond); }
else if(OptionHasShape(m).contains("triangle")) {
if(OptionHasShape(m).contains("invert")) { renderer1.setSeriesShape(m-1,downtriangle); }
else if(OptionHasShape(m).contains("left")) { renderer1.setSeriesShape(m-1,lefttriangle); }
else if(OptionHasShape(m).contains("right")){ renderer1.setSeriesShape(m-1,righttriangle); }
else { renderer1.setSeriesShape(m-1,triangle); }
}
else { renderer1.setSeriesShape(m-1,circle); }
}
}
for (int m=1; m<=collection.getSeriesCount(); m++){
System.out.println("checking open: " + m + " " + collection.getSeriesCount() + " " + OptionHasOpen(m));
if(OptionHasOpen(m) != null){
if(OptionHasOpen(m).contains("open")) { renderer1.setSeriesShapesFilled(m-1,false); }
else { renderer1.setSeriesShapesFilled(m-1,true); }
}
}
final Range rangex = new Range(xlow,xhigh);
final Range rangey = new Range(ylow,yhigh);
final Range rangeypos = new Range(ylowpos,yhigh);
NumberAxis rangeAxisX = null;
if(_xscale.equals("lin")){
rangeAxisX = new NumberAxis();
rangeAxisX.setRange(rangex);
} else{
LogarithmicAxis logaxistemp = new LogarithmicAxis("header");
rangeAxisX = logaxistemp;
if(_xmin != null && _xmax != null) {rangeAxisX.setRange(rangex);}
}
rangeAxisX.setLabel(xlabel);
NumberAxis rangeAxisY = null;
if(_yscale.equals("lin")) {
rangeAxisY = new NumberAxis();
rangeAxisY.setRange(rangey);
} else{
// LogarithmicAxis logaxistemp = new LogarithmicAxis(_yaxis.getHeader());
LogarithmicAxis logaxistemp = new LogarithmicAxis("header");
rangeAxisY = logaxistemp;
rangeAxisY.setRange(rangeypos);
//if(_ymin != null && _ymax != null) {rangeAxisY.setRange(rangey);}
}
rangeAxisY.setLabel(ylabel);
Double xfract = 0.65;
Double legposy1 = rangeAxisY.getRange().getUpperBound();
Double legposy2 = rangeAxisY.getRange().getLowerBound();
Double yfract = 0.7;
XYIntervalSeriesCollection collectiontemp = new XYIntervalSeriesCollection();
for (int n=0; n<collection.getSeriesCount(); n++){
XYIntervalSeries seriestemp = collection.getSeries(n);
yfract = 0.9-n*0.05;
if(_xkey != null) xfract = Double.valueOf(_xkey)-0.05;
Double xxx= xfract*xhigh+(1.0-xfract)*xlow;
if(_ykey != null) yfract = Double.valueOf(_ykey)-n*0.05;
Double yyy= yfract*yhigh+(1.0-yfract)*ylow;
if(_xscale.equals("log")){xxx = Math.exp(xfract*Math.log(xhigh)+(1.0-xfract)*Math.log(xlow));}
if(_yscale.equals("log")){yyy = Math.exp(yfract*Math.log(legposy1)+(1.0-yfract)*Math.log(legposy2));}
seriestemp.add(xxx,xxx,xxx,yyy,yyy,yyy);
collectiontemp.addSeries(seriestemp);
}
Double legposx1 = rangeAxisX.getRange().getUpperBound();
Double legposx2 = rangeAxisX.getRange().getLowerBound();
xfract = 0.7;
Double legposy = null;
if(_xkey != null) xfract = Double.valueOf(_xkey);
Double legposx= xfract*legposx1+(1.0-xfract)*legposx2;
if(_xscale.equals("log")){legposx = Math.exp(xfract*Math.log(legposx1)+(1.0-xfract)*Math.log(legposx2));}
yfract = 0.7;
final XYPlot subplot1 = new XYPlot(collectiontemp, rangeAxisX, rangeAxisY, renderer1);
for (int n=0; n<collectiontemp.getSeriesCount(); n++){
String legtxt = (String) collectiontemp.getSeries(n).getKey();
if(OptionHasText(n+1) != null) { legtxt = legtxt + OptionHasText(n+1); }
yfract = 0.9-n*0.05;
if(_ykey != null) yfract = Double.valueOf(_ykey)-n*0.05;
legposy= yfract*legposy1+(1.0-yfract)*legposy2;
if(_yscale.equals("log")){legposy = Math.exp(yfract*Math.log(legposy1)+(1.0-yfract)*Math.log(legposy2));}
XYTextAnnotation annotation = new XYTextAnnotation(legtxt,legposx,legposy);
annotation.setTextAnchor(TextAnchor.CENTER_LEFT);
subplot1.addAnnotation(annotation);
yfract = 0.9-(n+1)*0.05;
if(_ytext != null) yfract = Double.valueOf(_ytext)-(n+1)*0.05;
legposy= yfract*legposy1+(1.0-yfract)*legposy2;
if(_yscale.equals("log")){legposy = Math.exp(yfract*Math.log(legposy1)+(1.0-yfract)*Math.log(legposy2));}
}
if( _optionstring[0] != null) {
if(_xtext != null) xfract = Double.valueOf(_xtext);
legposx= xfract*legposx1+(1.0-xfract)*legposx2;
if(_xscale.equals("log")){legposx = Math.exp(xfract*Math.log(legposx1)+(1.0-xfract)*Math.log(legposx2));}
String[] temp = _optionstring[0].split("\n");
for (int i=0; i<temp.length; i++){
String tt = temp[i].replaceAll("GAMMA",Greek.GAMMA.toString());
tt = tt.replaceAll("gamma",Greek.GAMMA.toString());
tt = tt.replaceAll("-->","\u2192");
XYTextAnnotation annotation = new XYTextAnnotation(tt,legposx,legposy);
annotation.setTextAnchor(TextAnchor.CENTER_LEFT);
subplot1.addAnnotation(annotation);
yfract = 0.9-(collectiontemp.getSeriesCount()+i+1)*0.05;
if(_ytext != null) yfract = Double.valueOf(_ytext)-(collectiontemp.getSeriesCount()+i+1)*0.05;
legposy= yfract*legposy1+(1.0-yfract)*legposy2;
if(_yscale.equals("log")){legposy = Math.exp(yfract*Math.log(legposy1)+(1.0-yfract)*Math.log(legposy2));}
}
}
final JFreeChart chart = new JFreeChart("", new Font("Serif", Font.PLAIN, 14), subplot1, true);
chart.setBackgroundPaint(new Color(255,255,255,0));
// chart.setTitle(titlestring);
int width = Integer.parseInt(_xsize);
int height = Integer.parseInt(_ysize);
// String spng = "png";
String spng = _plotType;
ImageFormat format = ImageFormat.valueOf(spng.toUpperCase());
return Plotter.makeChartStreamResponse(chart, format, width,height);
}
}

File Metadata

Mime Type
text/plain
Expires
Mon, Jan 20, 8:25 PM (12 h, 16 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4242219
Default Alt Text
PlotCombinedImage.java (23 KB)

Event Timeline