Creating Multiple SVG Images For PDF

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Reggie3
Posts: 12
Joined: Mon Mar 10, 2008 5:19 pm

Creating Multiple SVG Images For PDF

Post by Reggie3 » Tue Apr 01, 2008 3:02 pm

I am trying to export several charts to SVG files so that I can add them to individual pages in a PDF. I use to following code to create the temporary SVG image file that I later import into the PDF.

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);
	 }
The code works when I only need to create one SVG It also works in creating the 1st SVG in a series of them. However, it fails with the following exception when creating the 2nd SVG.
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?

Reggie3
Posts: 12
Joined: Mon Mar 10, 2008 5:19 pm

Some More Info

Post by Reggie3 » Wed Apr 02, 2008 2:53 am

After working on this problem a bit more, I was able to determine the following:

1. If I turn off legends on my individual charts at the time they are created then the problem goes away.
2. The chart that causes the problem only has one item on it's legend while the rest have at least 3.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Thu Apr 03, 2008 9:09 am

How exactly are you generating your 2,3... charts? I assume that you are calling your method several times by another method in which you also switch from one chart to another. Can you show the source of this calling method?

Reggie3
Posts: 12
Joined: Mon Mar 10, 2008 5:19 pm

Figured It Out

Post by Reggie3 » Thu Apr 03, 2008 6:17 pm

paradoxoff, I was able to figure out my problem ... maybe not figure it out totally, but at least it no longer happens.
It was related to one of my charts only having 1 item in it's legend while the others had 3. This was because that particular chart only had one bar on it. This also caused my charts to be out of alignment when I did a category chart; the single bar was being rendered in the middle of a chart the same width as my others.
What I had to do was pad out the dataset for that chart that had the single bar so that it had the same number of series as the others, and thus the same number of legend items.

Locked