Center Gradients from JFreeChart

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).
Locked
remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Center Gradients from JFreeChart

Post by remiohead » Thu Apr 24, 2014 5:29 pm

I needed a solution that would display the center vertical and center horizontal gradients from JFreeChart in SVG.

As you rightly pointed out some months ago this is not straightforward. What I ended up doing was allowing JFreeChart to work with LinearGradientPaint and adding that as a supported gradient type in SVG. The implementation below only supports basic horizontal and vertical, but it works with any number of stops and colours.

Code: Select all

    private String getLinearGradientElement(String id, LinearGradientPaint paint) {
        StringBuilder b = new StringBuilder("<linearGradient id=\"").append(id)
                .append("\" ");
        Point2D p1 = paint.getStartPoint();
        Point2D p2 = paint.getEndPoint();
        boolean vertical = Math.abs(p1.getY() - p2.getY()) > EPSILON;
        if(vertical) {
	        b.append("x1=\"0\" ");
	        b.append("y1=\"0\" ");
	        b.append("x2=\"0\" ");
	        b.append("y2=\"1\" ");
        }
        b.append(">");
        
        int size = paint.getColors().length;
        for(int i = 0; i < size; i++) {
        	int offset = (int)Math.round(paint.getFractions()[i]*100.0);
            b.append("<stop offset=\"")
            	.append(offset)
            	.append("%\" style=\"stop-color: ")
            	.append(getSVGColor(paint.getColors()[i]))
            	.append(";\"/>");	
        }
        return b.append("</linearGradient>").toString();
    }

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

Re: Center Gradients from JFreeChart

Post by david.gilbert » Mon May 05, 2014 7:42 pm

Thanks for the feedback - I'd forgotten that LinearGradientPaint wasn't supported. I've fixed that for the upcoming 1.9 release and also improved the GradientPaint handling so that it will support the direction specified by the points on the GradientPaint. I'll get it released as soon as possible.
David Gilbert
JFreeChart Project Leader

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

Locked