Dear All, I am working on a project that generates a lot of JFreecharts on fly and put them in a PDF report.
I wrote a preloader, loader and source, and for unknown reason, the chart only
shows the coordinates, legends, etc, but NO DATA curves. See the attachment.
I feel confused, any help or suggestions will be highly appreciated.
Thanks
Jing
/**
* Created by IntelliJ IDEA. User: lij40 Date: 9/1/11 Time: 11:04 AM To change
* this template use File | Settings | File Templates.
*/
public class ChartPreloader extends AbstractImagePreloader implements
SVGConstants
{
/** Logger instance */
private static Log log = LogFactory.getLog(ChartPreloader.class);
/** {@inheritDoc} */
public ImageInfo preloadImage(String uri, Source src, ImageContext context)
throws IOException
{
return (src instanceof JFreeChartSource) ?
getImage(uri, (JFreeChartSource) src, context) : null;
}
private ImageInfo getImage(String uri, final JFreeChartSource src,
ImageContext context)
{
ImageInfo info = new ImageInfo(uri, ImageLoaderFactoryOnFly.MIME_TYPE);
ImageSize size = new ImageSize();
size.setSizeInPixels((int)src.getWidth(), (int)src.getHeight());
//Set the resolution to that of the FOUserAgent
size.setResolution(context.getSourceResolution());
size.calcSizeFromPixels();
info.setSize(size);
Graphics2DImagePainter painter = new Graphics2DImagePainter()
{
public Dimension getImageSize()
{
return new Dimension((int)src.getWidth(), (int)src.getHeight());
}
public void paint(Graphics2D g2d, Rectangle2D area)
{
src.getChart().draw(g2d, area, null, null);
}
};
//The whole image had to be loaded to determine the image size, so keep
that information
info.getCustomObjects()
.put(ImageInfo.ORIGINAL_IMAGE, new ImageGraphics2D(info, painter));
return info;
}
}
public class JFreeChartSource implements Source
{
//public static final int CHART_SIZE = 133;
public static final int CHART_SIZE = 250;
private String systemId;
private final JFreeChart chart;
private final double width;
private final double height;
public JFreeChartSource(String systemId, JFreeChart chart, double width,
double height)
{
this.systemId = systemId;
this.chart = chart;
this.width = width;
this.height = height;
}
@Override public void setSystemId(String systemId)
{
this.systemId = systemId;
}
@Override public String getSystemId()
{
return systemId;
}
public JFreeChart getChart()
{
return chart;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
}
public class ChartHandler implements ResponseGenerator
{
@Override public boolean canHandleUriForSource(
Query query, Properties parameters, String uri)
{
return uri.contains("chart");
}
// test case generating a Sin(x) vs Cos(x) chart
@Override public Source getSourceResponse(
Query query, Properties parameters, String queryText)
{
XYSeries dataset = new XYSeries("series0");
for (int i = -10; i < 10; i++)
{
dataset.add(i, Math.cos(i / 10.0));
}
XYSeries series = new XYSeries("Average Weight");
series.add(20.0, 20.0);
series.add(40.0, 25.0);
series.add(55.0, 50.0);
series.add(70.0, 65.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("Sin(x) vs X", "x",
"Cos(X)",
/*new XYSeriesCollection(dataset)*/xyDataset,
PlotOrientation.VERTICAL, false, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
/* plot.setBackgroundPaint(new Color(0, 0, 0, 100));//
Color.decode("0xF5F5F5"));
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.black);
plot.setDomainGridlinePaint(Color.black);
*/
plot.setNoDataMessage("No Information.");
// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
chart.setBackgroundPaint(Color.white);
plot.getRenderer().setSeriesToolTipGenerator(0,
new StandardXYToolTipGenerator("{0}: ({1}, {2})",
new DecimalFormat("00"), new DecimalFormat("0.00")));
return new JFreeChartSource(queryText,chart, 400, 400);
}
}
________________________________
Sample disclaimer text
chart.pdf
Description: chart.pdf
--------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
