/* Reattore HTTP Server
Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: ChartReporter.java,v 1.3 2003/03/05 04:31:56 michaelh Exp $
*/
package juju.reattore.perfcap.reporter.impl;
import java.util.*;
import java.io.*;
import java.net.*;
import com.jrefinery.chart.*;
import com.jrefinery.data.*;
import org.apache.commons.beanutils.BeanUtils;
import juju.reattore.perfcap.reporter.Reporter;
import juju.reattore.perfcap.var.Variable;
import juju.reattore.perfcap.tester.Results;
/** Generates a XY chart with multiple series from the input data.
@tag chartreporter
@group Reporter
*/
public class ChartReporter
implements Reporter {
private String filter;
private String out;
private int width = 400;
private int height = 300;
private String title = "Chart Title";
private String xAxisTitle = "X";
private String yAxisTitle = "Y";
private Map series = new LinkedHashMap();
private XYSeriesCollection col = new XYSeriesCollection();
/** Width of the output image in pixels.
@param argWidth Value to assign to this.width
@default 400
*/
public void setWidth(int argWidth) {
this.width = argWidth;
}
/** Height of the output image in pixels.
@param argHeight Value to assign to this.height
@default 300
*/
public void setHeight(int argHeight) {
this.height = argHeight;
}
/** Title to place on the chart.
@param argTitle Value to assign to this.title
@default Chart Title
*/
public void setTitle(String argTitle) {
this.title = argTitle;
}
/** Title of the X (horizontal) axis.
@param argXAxisTitle Value to assign to this.xAxisTitle
@default X
*/
public void setXAxisTitle(String argXAxisTitle) {
this.xAxisTitle = argXAxisTitle;
}
/** Title of the Y (vertical) axis.
@param argYAxisTitle Value to assign to this.yAxisTitle
@default Y
*/
public void setYAxisTitle(String argYAxisTitle) {
this.yAxisTitle = argYAxisTitle;
}
/** The property from the test results.
@param filter The Bean name of the property.
*/
public void setFilter(String filter) {
filter = "".equals(filter) ? null : filter;
this.filter = filter;
}
/** Name of the file to write to.
@param out Name of the file.
*/
public void setOut(String out) {
this.out = out;
}
private Number toNumber(Object obj)
throws NumberFormatException {
if (obj instanceof Number) {
return (Number)obj;
}
else {
return new Double(obj.toString());
}
}
/** @see Reporter */
public void add(List ind, Results res)
throws Exception {
Variable var = (Variable)ind.get(0);
XYSeries se = (XYSeries)series.get(var.getValue());
if (se == null) {
se = new XYSeries(var.getValue().toString());
series.put(var.getValue(), se);
col.addSeries(se);
}
Object val;
if (filter != null) {
val = BeanUtils.getProperty(res, filter);
}
else {
/* PENDING */
val = new Integer(0);
}
se.add(toNumber(((Variable)ind.get(1)).getValue()), toNumber(val));
}
/** @see Reporter */
public void end()
throws Exception {
JFreeChart chart = ChartFactory.createLineXYChart(title,
xAxisTitle,
yAxisTitle,
col,
true,
false,
false);
ChartUtilities.saveChartAsPNG(new File(out),
chart,
width, height);
}
}