Code: Select all
public static void ExportChartToSVG(String strFullFileName, JFreeChart chart) throws UnsupportedEncodingException, FileNotFoundException, SVGGraphics2DIOException{
do{
try {
GlobalDataStore.logger.debug("Sleeping");
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}while(new File(strFullFileName).exists());
//THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...
//Get a DOMImplementation
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
//Create an instance of org.w3c.dom.Document
Document document = domImpl.createDocument(null, "svg", null);
//Create an instance of the SVG Generator
SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
//set the precision to avoid a null pointer exception in Batik 1.5
svgGenerator.getGeneratorContext().setPrecision(6);
//Ask the chart to render into the SVG Graphics2D implementation
chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);
//Finally, stream out SVG to a file using UTF-8 character to
//byte encoding
boolean useCSS = true;
Writer out = new OutputStreamWriter(new FileOutputStream(new File(strFullFileName)), "UTF-8");
svgGenerator.stream(out, useCSS);
}
Here is the beginning of the exception that is thrown:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at org.jfree.data.DefaultKeyedValues2D.getRowKey(DefaultKeyedValues2D.java:169)
at org.jfree.data.category.DefaultCategoryDataset.getRowKey(DefaultCategoryDataset.java:126)
at org.jfree.chart.labels.StandardCategorySeriesLabelGenerator.createItemArray(StandardCategorySeriesLabelGenerator.java:115)
at org.jfree.chart.labels.StandardCategorySeriesLabelGenerator.generateLabel(StandardCategorySeriesLabelGenerator.java:99)
at org.jfree.chart.renderer.category.BarRenderer.getLegendItem(BarRenderer.java:666)
at org.jfree.chart.plot.CategoryPlot.getLegendItems(CategoryPlot.java:1773)
at org.jfree.chart.title.LegendTitle.fetchLegendItems(LegendTitle.java:404)
at org.jfree.chart.title.LegendTitle.arrange(LegendTitle.java:477)
at org.jfree.chart.JFreeChart.drawTitle(JFreeChart.java:1317)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1208)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1120)
I'm not sure what would be causing this. I thought it might be because the 1st temporary file was still being written to so I put in a sleep loop to check if the previous temporary SVG file still exists, but the loop consistently ends after 1 second.
Any idea on what could be causing this?