You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Nick Stuart <ni...@gmail.com> on 2005/04/01 20:02:21 UTC

JFreeChart, FOP, and Batik

Hello all, I'm new Batik and have tried to find the answer to my
problem but cant seem to get it.

 I'm creating an SVG file through JFreeChart using Batik. It gets
created fine if I dont try and touch it. The problem is that in order
for FOP to use the image it needs to have a width and height in the
<svg> root.

If I add the attributes in there manually it works fine (say to like,
width="14cm" height="10cm"). The image loads up in FOP and everything
is good to go. BUT when I try to do this through code:
        //this is after the image is generated from JFree chart.
       Element root = svgGenerator.getRoot();
        svgGenerator.getRoot(root);
        root.setAttributeNS(null, "width", "14cm");
        root.setAttributeNS(null, "height", "14cm");

All I end up with after using svgGenerator.stream() is an empty image
document, AND the attributes still arent even seet.

Here's how the image is written out:
Writer out = new OutputStreamWriter(
        new FileOutputStream(file), "UTF-8");
        svgGenerator.stream(out, useCSS);
        out.close();

And here is what I get for output if I try to add the attributes above.

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1;
color-rendering:auto; color-interpolation:auto; text-rendering:auto;
stroke:black; stroke-linecap:square; stroke-miterlimit:10;
shape-rendering:auto; stroke-opacity:1; fill:black;
stroke-dasharray:none; font-weight:normal; stroke-width:1;
font-family:&apos;Dialog&apos;; font-style:normal;
stroke-linejoin:miter; font-size:12; stroke-dashoffset:0;
image-rendering:auto;" xmlns="http://www.w3.org/2000/svg">
  <!--Generated by the Batik Graphics2D SVG Generator-->
  <defs id="genericDefs" />
  <g />
</svg>


Any ideas? Thanks for your help!

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: JFreeChart, FOP, and Batik

Posted by Nick Stuart <ni...@gmail.com>.
The problem with setting the canvas size is that I need to set it in
centimeters in order for FOP to use it correctly, but Dimension is
only pixels which makes sense for most images and the like.

I got around it by doing to following (which is ugly, but it works).

DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
        //Hack below in order to add the width/height to the svg file.
        //Width and height are needed by FOP in order to render correctly.
        try {
            DocumentBuilder builder =
                    factory.newDocumentBuilder();
            document = builder.parse(file);
            log.error("Element: " +
document.getDocumentElement().getNodeName());
            
            document.getDocumentElement().setAttributeNS(null, "width", "14cm");
            document.getDocumentElement().setAttributeNS(null,
"height", "9.5cm");
            TransformerFactory tFactory =
                    TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            
            DOMSource source = new DOMSource(document);
            file.delete();
            file.createNewFile();
            out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            StreamResult result = new StreamResult(out);
            transformer.transform(source, result);
        } catch (Exception spe) {
            log.error("Error parsing document.", spe);
        }

I can change the canvas side with out any problems, but thats not the
issue. I'm guessing what I want/need to do is not a part of SVG (not
sure) so it isn't really supported.

Thanks for the help and suggestions.
-Nick

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: JFreeChart, FOP, and Batik

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Nick,

Nick Stuart wrote:
> [...] It needs to have a width and height in the
> <svg> root.
> [...] when I try to do this through code:
>         //this is after the image is generated from JFree chart.
>        Element root = svgGenerator.getRoot();
>         svgGenerator.getRoot(root);
>         root.setAttributeNS(null, "width", "14cm");
>         root.setAttributeNS(null, "height", "14cm");

    When you call 'getRoot' the svgGenerator gives it to you.
It no longer remembers the content under that root.

> Here's how the image is written out:
> Writer out = new OutputStreamWriter(
>         new FileOutputStream(file), "UTF-8");
>         svgGenerator.stream(out, useCSS);
>         out.close();

   One option is to use 'stream(Element svgRoot, Writer w, boolean useCSS)'.

   The other better option is to call
	setSVGCanvasSize(Dimension svgCanvasSize)
Before you start drawing with your desired canvas size.

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org