Center Gradients from JFreeChart

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).

Center Gradients from JFreeChart

Postby 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\" ");
        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("%\" style=\"stop-color: ")
        return b.append("</linearGradient>").toString();
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm

Re: Center Gradients from JFreeChart

Postby 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: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
JFreeChart Project Leader
Posts: 11664
Joined: Fri Mar 14, 2003 10:29 am

Return to JFreeSVG

Who is online

Users browsing this forum: No registered users and 1 guest