0

I have a SVG image (a Map of a country) and I am trying to add a JfreeChart on top of it which would show 3d Bars on particular locations. Can someone please help point out how do i add a JfreeChart to an existing SVG image.

Following is the code so far

            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory sax = new SAXSVGDocumentFactory(parser);
            String uri = "file:///F://WorldMap.svg"; 
            SVGDocument doc = sax.createSVGDocument(uri); 
            SVGGraphics2D my_svg_generator = new SVGGraphics2D(doc);

            // Create a converter for this document.
            SVGGraphics2D g = new SVGGraphics2D(doc);

            //got a JfreeChart from a static method depending on data
            JFreeChart chart = createChart(createDataset());
            //I can add up normal nodes
            Element svgRoot = doc.getDocumentElement();
            svgRoot.appendChild(doc.createElementNS("http://www.w3.org/2000/svg", "rect"));

I have tried converting Jfreechart to Bytes and using g.drawBytes() but it renders some garbage on top of the SVG.

Would appreciate if someone can help with this.

Amit
  • 143
  • 1
  • 14

1 Answers1

1

A jfreechart is an image so you need to create an image element and then turn the jfreechart data into a data uri and set the data url as the xlink:href attribute of the image.

jfreechart can generate gif, png or jpeg images so you'll have to adjust the data uri appropriately but it should look like this for a png image.

<image xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" />

Don't forget to convert the byte data to base64 too. There are various base64 encoders around

Community
  • 1
  • 1
Robert Longson
  • 118,664
  • 26
  • 252
  • 242