Hi, I am creating a web charting application using Batik.
I create a Document by first creating a DOMImplementation and
then using this DOMImplementation to create the Document.
The Document is used to create a SVGGeneratorContext object and then
the SVGGeneratorContext is used to create the SVGGraphics2D object.
I then use this SVGGraphics2D object as the graphical context in the
charting component. I am assuming that the Document I created is used
to hold the DOM tree created when performing Java2D commands with the
SVGGraphics2D object as graphical context.
The charting component contains Java2D commands to create a simple line
chart where the lines for each series of data are connected with circles,
i.e. each value in the line chart is marked by a circle.
I want to be able to perform drill down on this chart by clicking on the
circles to retrieve more information. Thus I need to be able to make the
circles in the chart clickable. To do so I think I need to go through
the DOM tree for the chart and replace each circle element with an X-link
element where the cirle is a child element of the X-link element.
Here is the code I am using to perform what I have described above :
public static void writeChartAsSVG(OutputStream out, JFreeChart chart, int width, int
height) throws
IOException
{
SVGGeneratorContext ctx;
SVGGraphics2D svgGenerator;
XmlWriter xmlwriter;
DOMImplementation domImpl;
Document document;
DocumentFragment docFrag;
NodeList circleNodes;
Node dummyNode;
Node parentNode;
Element linkElement;
Element root;
Element circle;
int numCircleNodes;
// Get a DOMImplementation
domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
document = domImpl.createDocument(null, "svg", null);
ctx = SVGGeneratorContext.createDefault(document);
svgGenerator = new SVGGraphics2D(ctx,false);
chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
docFrag = document.createDocumentFragment();
linkElement = (Element)
docFrag.appendChild(document.createElement(SVGSyntax.SVG_A_TAG));
linkElement.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.ATTR_XLINK_HREF,"http://www.vg.no");
root = svgGenerator.getRoot();
circle =
document.createElementNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_RECT_TAG);
circle.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.SVG_FILL_ATTRIBUTE,"red");
root.appendChild(circle);
circleNodes = root.getElementsByTagName(SVGSyntax.SVG_CIRCLE_TAG);
numCircleNodes = circleNodes.getLength();
for(int nodeCounter=0;nodeCounter<numCircleNodes;nodeCounter++)
{
dummyNode = circleNodes.item(nodeCounter).cloneNode(true);
linkElement.appendChild(dummyNode);
parentNode = circleNodes.item(nodeCounter).getParentNode();
parentNode.replaceChild(docFrag,circleNodes.item(nodeCounter));
docFrag = document.createDocumentFragment();
linkElement = (Element)
docFrag.appendChild(document.createElement(SVGSyntax.SVG_A_TAG));
linkElement.setAttributeNS(SVGSyntax.SVG_NAMESPACE_URI,SVGSyntax.ATTR_XLINK_HREF,"http://www.vg.no");
}
// Finally, stream out SVG to the standard output using UTF-8
// character to byte encoding
boolean useCSS = true;
Writer svgOutputWriter = new OutputStreamWriter(out, "UTF-8");
svgGenerator.stream(root,svgOutputWriter,useCSS);
}
When I try to stream out using the svgGenerator.stream method
all I receive is the SVG document without any changes made.
It seems as though I am not able to access the DOM tree and
performs the replacements. Help on how to access the DOM tree created
by the SVGGenerator and manipulate the nodes of the tree
before streaming them to the output would be greatly appreciated.
Regards
Phillip Larsen
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]