Transparency issues with jfreeSVG

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).
Locked
Mala
Posts: 8
Joined: Tue Jan 28, 2014 1:06 pm
antibot: No, of course not.

Transparency issues with jfreeSVG

Post by Mala » Fri Feb 07, 2014 8:12 am

Hi,
I have been studying the jFreeSvg for some time.
I was analysing the transparency factor.
I set

Code: Select all

 renderer.setSeriesPaint(0, new Color(255, 0, 0, 255));
 renderer.setSeriesPaint(1, new Color(0,255,0,64));
as colors for series.
However I do not see any transparency effect in the svg generated.
Here is the perview. : https://docs.google.com/file/d/0B9fcNO1 ... er=1&w=800
The SVG generated has fill-opacity :1 , set for both series.

I guess there is bug somewhere in setting opacity to svg element.
Please help me with this.

Complete code here :

Code: Select all

/* ===================================================
 * JFreeSVG : an SVG library for the Java(tm) platform
 * ===================================================
 * 
 * (C)opyright 2013, by Object Refinery Limited.  All rights reserved.
 *
 * Project Info:  http://www.jfree.org/jfreesvg/index.html
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.]
 * 
 */

package demo;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;
import org.jfree.ui.TextAnchor;

/**
 * A demo/test for a bar chart.
 */
public class SVGBarChartDemo2 {
    
    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {
        DefaultStatisticalCategoryDataset dataset
            = new DefaultStatisticalCategoryDataset();
        dataset.add(10.0, 2.4, "Row 1", "Column 1");
        dataset.add(15.0, 4.4, "Row 1", "Column 2");
        dataset.add(13.0, 2.1, "Row 1", "Column 3");
        dataset.add(7.0, 1.3, "Row 1", "Column 4");
        dataset.add(22.0, 2.4, "Row 2", "Column 1");
        dataset.add(18.0, 4.4, "Row 2", "Column 2");
        dataset.add(28.0, 2.1, "Row 2", "Column 3");
        dataset.add(17.0, 1.3, "Row 2", "Column 4");
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  a dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {

        JFreeChart chart = ChartFactory.createLineChart(
            "Statistical Bar Chart Demo 1", "Type", "Value", dataset);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(false);

        // customise the renderer...
        StatisticalBarRenderer renderer = new StatisticalBarRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setErrorIndicatorPaint(Color.black);
        renderer.setIncludeBaseInRange(false);
        plot.setRenderer(renderer);

        // ensure the current theme is applied to the renderer just added
        ChartUtilities.applyCurrentTheme(chart);

        renderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelPaint(Color.yellow);
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.INSIDE6, TextAnchor.BOTTOM_CENTER));

        
        renderer.setSeriesPaint(0, new Color(255, 0, 0, 255));
        renderer.setSeriesPaint(1, new Color(0,255,0,64));
        return chart;
    }

    /**
     * Starting point for the demo.
     * 
     * @param args  ignored.
     * 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        JFreeChart chart = createChart(createDataset());
        SVGGraphics2D g2 = new SVGGraphics2D(600, 400);
        Rectangle r = new Rectangle(0, 0, 600, 400);
        chart.draw(g2, r);
        File f = new File("SVGBarChartDemoTransparency.svg");
        SVGUtils.writeToSVG(f, g2.getSVGElement());
    }
}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Transparency issues with jfreeSVG

Post by david.gilbert » Tue Feb 25, 2014 4:41 pm

Hi Mala,

Apologies, I somehow missed replying to this. Thanks for raising this bug, it is fixed for the upcoming 1.7 release. This is the patch in case you need it sooner:

Code: Select all

# This patch file was generated by NetBeans IDE
# It uses platform neutral UTF-8 encoding and \n newlines.
--- HEAD
+++ Modified In Working Tree
@@ -978,10 +978,10 @@
      * 
      * @return An SVG color string. 
      */
-    private String getSVGColor() {
+    private String svgColorStr() {
         String result = "black;";
         if (this.paint instanceof Color) {
-            return getSVGColor((Color) this.paint);
+            return rgbaColorStr((Color) this.paint);
         } else if (this.paint instanceof GradientPaint 
                 || this.paint instanceof RadialGradientPaint) {
             return "url(#" + this.gradientPaintRef + ")";
@@ -996,7 +996,7 @@
      * 
      * @return The SVG RGB color string.
      */
-    private String getSVGColor(Color c) {
+    private String rgbColorStr(Color c) {
         StringBuilder b = new StringBuilder("rgb(");
         b.append(c.getRed()).append(",").append(c.getGreen()).append(",")
                 .append(c.getBlue()).append(")");
@@ -1004,6 +1004,23 @@
     }
     
     /**
+     * Returns a string representing the specified color in RGBA format.
+     * 
+     * @param c  the color (<code>null</code> not permitted).
+     * 
+     * @return The SVG RGBA color string.
+     */
+    private String rgbaColorStr(Color c) {
+        StringBuilder b = new StringBuilder("rgba(");
+        double alphaPercent = c.getAlpha() / 255.0;
+        b.append(c.getRed()).append(",").append(c.getGreen()).append(",")
+                .append(c.getBlue());
+        b.append(",").append(transformDP(alphaPercent));
+        b.append(")");
+        return b.toString();
+    }
+    
+    /**
      * Returns a stroke style string based on the current stroke and
      * alpha settings.
      * 
@@ -1019,7 +1036,7 @@
         }
         StringBuilder b = new StringBuilder();
         b.append("stroke-width: ").append(strokeWidth).append(";");
-        b.append("stroke: ").append(getSVGColor()).append(";");
+        b.append("stroke: ").append(svgColorStr()).append(";");
         b.append("stroke-opacity: ").append(getAlpha()).append(";");
         if (dashArray != null && dashArray.length != 0) {
             b.append("stroke-dasharray: ");
@@ -1040,7 +1057,7 @@
      */
     private String getSVGFillStyle() {
         StringBuilder b = new StringBuilder();
-        b.append("fill: ").append(getSVGColor()).append(";");
+        b.append("fill: ").append(svgColorStr()).append(";");
         b.append("fill-opacity: ").append(getAlpha());
         return b.toString();
     }
@@ -1106,7 +1123,7 @@
      */
     private String getSVGFontStyle() {
         StringBuilder b = new StringBuilder();
-        b.append("fill: ").append(getSVGColor()).append("; ");
+        b.append("fill: ").append(svgColorStr()).append("; ");
         String fontFamily = this.fontMapper.mapFont(this.font.getFamily());
         b.append("font-family: ").append(fontFamily).append("; ");
         b.append("font-size: ").append(this.font.getSize()).append("px; ");
@@ -2219,9 +2236,9 @@
         b.append("x2=\"").append(h ? "100%" : "50%").append("\" ");
         b.append("y2=\"").append(v ? "100%" : "50%").append("\">");
         b.append("<stop offset=\"0%\" style=\"stop-color: ").append(
-                getSVGColor(paint.getColor1())).append(";\"/>");
+                rgbColorStr(paint.getColor1())).append(";\"/>");
         b.append("<stop offset=\"100%\" style=\"stop-color: ").append(
-                getSVGColor(paint.getColor2())).append(";\"/>");
+                rgbColorStr(paint.getColor2())).append(";\"/>");
         return b.append("</linearGradient>").toString();
     }
     
@@ -2252,7 +2269,7 @@
             Color c = colors[i];
             float f = fractions[i];
             b.append("<stop offset=\"").append(geomDP(f * 100)).append("%\" ");
-            b.append("stop-color=\"").append(getSVGColor(c)).append("\"/>");
+            b.append("stop-color=\"").append(rgbColorStr(c)).append("\"/>");
         }
         return b.append("</radialGradient>").toString();
     }
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked