使用自定义色系美化JfreeChart图表

JFreeChart所使用的色系生成的图表很难看,笔者深有体会。在使用到JfreeChart的一些项目中经常会有客户提出"能不能由我们自己去设置图表中每个分类的颜色"等等诸如此类的需求。但用户往往不具备美工的实力,设置的颜色也往往差强人意。为此,何不将问题解决在最初发生的阶段,由我们来指定JFreeChart的色系。 在仔细研究JFreeChart的源码后发现,想更改JfreeChart的色系其实不难。下面就来具体谈一谈如何让JFreeChart绘制我们指定的色系。

JFreeChart的默认色系

DualAxisDemo1-254

自定义色系

image

使用自定义色系可以采用以下2种方式:

一、直接修改ChartColor.class文件

ChartColor的源码:

/* ===========================================================

* JFreeChart : a free chart library for the Java(tm) platform

* ===========================================================

*

* (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.

*

* Project Info:  http://www.jfree.org/jfreechart/index.html

*

* This library is free software; you can redistribute it and/or modify it

* under the terms of the GNU Lesser General Public License as published by

* the Free Software Foundation; either version 2.1 of the License, or

* (at your option) any later version.

*

* This library 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 Lesser General Public

* License for more details.

*

* You should have received a copy of the GNU Lesser General Public

* License along with this library; if not, write to the Free Software

* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,

* USA.

*

* [Java is a trademark or registered trademark of Sun Microsystems, Inc.

* in the United States and other countries.]

*

* ---------------

* ChartColor.java

* ---------------

* (C) Copyright 2003-2008, by Cameron Riley and Contributors.

*

* Original Author:  Cameron Riley;

* Contributor(s):   David Gilbert (for Object Refinery Limited);

*

* Changes

* -------

* 23-Jan-2003 : Version 1, contributed by Cameron Riley (DG);

* 25-Nov-2004 : Changed first 7 colors to softer shades (DG);

* 03-Nov-2005 : Removed orange color, too close to yellow - see bug

*               report 1328408 (DG);

* ------------- JFREECHART 1.0.x ---------------------------------------------

* 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);

*

*/

package org.jfree.chart;

import java.awt.Color;

import java.awt.Paint;

/**

* Class to extend the number of Colors available to the charts. This

* extends the java.awt.Color object and extends the number of final

* Colors publically accessible.

*/

public class ChartColor extends Color {

    /** A very dark red color. */

    public static final Color VERY_DARK_RED = new Color(0x80, 0x00, 0x00);

    /** A dark red color. */

    public static final Color DARK_RED = new Color(0xc0, 0x00, 0x00);

    /** A light red color. */

    public static final Color LIGHT_RED = new Color(0xFF, 0x40, 0x40);

    /** A very light red color. */

    public static final Color VERY_LIGHT_RED = new Color(0xFF, 0x80, 0x80);

    /** A very dark yellow color. */

    public static final Color VERY_DARK_YELLOW = new Color(0x80, 0x80, 0x00);

    /** A dark yellow color. */

    public static final Color DARK_YELLOW = new Color(0xC0, 0xC0, 0x00);

    /** A light yellow color. */

    public static final Color LIGHT_YELLOW = new Color(0xFF, 0xFF, 0x40);

    /** A very light yellow color. */

    public static final Color VERY_LIGHT_YELLOW = new Color(0xFF, 0xFF, 0x80);

    /** A very dark green color. */

    public static final Color VERY_DARK_GREEN = new Color(0x00, 0x80, 0x00);

    /** A dark green color. */

    public static final Color DARK_GREEN = new Color(0x00, 0xC0, 0x00);

    /** A light green color. */

    public static final Color LIGHT_GREEN = new Color(0x40, 0xFF, 0x40);

    /** A very light green color. */

    public static final Color VERY_LIGHT_GREEN = new Color(0x80, 0xFF, 0x80);

    /** A very dark cyan color. */

    public static final Color VERY_DARK_CYAN = new Color(0x00, 0x80, 0x80);

    /** A dark cyan color. */

    public static final Color DARK_CYAN = new Color(0x00, 0xC0, 0xC0);

    /** A light cyan color. */

    public static final Color LIGHT_CYAN = new Color(0x40, 0xFF, 0xFF);

    /** Aa very light cyan color. */

    public static final Color VERY_LIGHT_CYAN = new Color(0x80, 0xFF, 0xFF);

    /** A very dark blue color. */

    public static final Color VERY_DARK_BLUE = new Color(0x00, 0x00, 0x80);

    /** A dark blue color. */

    public static final Color DARK_BLUE = new Color(0x00, 0x00, 0xC0);

    /** A light blue color. */

    public static final Color LIGHT_BLUE = new Color(0x40, 0x40, 0xFF);

    /** A very light blue color. */

    public static final Color VERY_LIGHT_BLUE = new Color(0x80, 0x80, 0xFF);

    /** A very dark magenta/purple color. */

    public static final Color VERY_DARK_MAGENTA = new Color(0x80, 0x00, 0x80);

    /** A dark magenta color. */

    public static final Color DARK_MAGENTA = new Color(0xC0, 0x00, 0xC0);

    /** A light magenta color. */

    public static final Color LIGHT_MAGENTA = new Color(0xFF, 0x40, 0xFF);

    /** A very light magenta color. */

    public static final Color VERY_LIGHT_MAGENTA = new Color(0xFF, 0x80, 0xFF);

    /**

     * Creates a Color with an opaque sRGB with red, green and blue values in

     * range 0-255.

     *

     * @param r  the red component in range 0x00-0xFF.

     * @param g  the green component in range 0x00-0xFF.

     * @param b  the blue component in range 0x00-0xFF.

     */

    public ChartColor2(int r, int g, int b) {

        super(r, g, b);

    }

    /**

     * Convenience method to return an array of <code>Paint</code> objects that

     * represent the pre-defined colors in the <code>Color<code> and

     * <code>ChartColor</code> objects.

     *

     * @return An array of objects with the <code>Paint</code> interface.

     */

    public static Paint[] createDefaultPaintArray() {

        return new Paint[] {

            new Color(0xFF, 0x55, 0x55),

            new Color(0x55, 0x55, 0xFF),

            new Color(0x55, 0xFF, 0x55),

            new Color(0xFF, 0xFF, 0x55),

            new Color(0xFF, 0x55, 0xFF),

            new Color(0x55, 0xFF, 0xFF),

            Color.pink,

            Color.gray,

            ChartColor.DARK_RED,

            ChartColor.DARK_BLUE,

            ChartColor.DARK_GREEN,

            ChartColor.DARK_YELLOW,

            ChartColor.DARK_MAGENTA,

            ChartColor.DARK_CYAN,

            Color.darkGray,

            ChartColor.LIGHT_RED,

            ChartColor.LIGHT_BLUE,

            ChartColor.LIGHT_GREEN,

            ChartColor.LIGHT_YELLOW,

            ChartColor.LIGHT_MAGENTA,

            ChartColor.LIGHT_CYAN,

            Color.lightGray,

            ChartColor.VERY_DARK_RED,

            ChartColor.VERY_DARK_BLUE,

            ChartColor.VERY_DARK_GREEN,

            ChartColor.VERY_DARK_YELLOW,

            ChartColor.VERY_DARK_MAGENTA,

            ChartColor.VERY_DARK_CYAN,

            ChartColor.VERY_LIGHT_RED,

            ChartColor.VERY_LIGHT_BLUE,

            ChartColor.VERY_LIGHT_GREEN,

            ChartColor.VERY_LIGHT_YELLOW,

            ChartColor.VERY_LIGHT_MAGENTA,

            ChartColor.VERY_LIGHT_CYAN

        };

    }

}

JFreeChart在该类中定义了色系中使用颜色的常量,并且在createDefaultPaintArray方法中返回整个色系的数组。我们可以将其替换成自己定义好的色系,如下代码所示(笔者借鉴了FusionChart的色系):

package org.jfree.chart;

import java.awt.Color;

import java.awt.Paint;

/**

* @author xum

* 自定义色系

*/

public class ChartColor  extends Color{

         public static final Color COLOR1 = new Color(188,228,255);

         public static final Color COLOR2 = new Color(255,205,35);

         public static final Color COLOR3 = new Color(157,202,20);

         public static final Color COLOR4 = new Color(209,126,73);

         public static final Color COLOR5 = new Color(35,167,167);

         public static final Color COLOR6 = new Color(237,98,98);

         public static final Color COLOR7 = new Color(167,98,167);

         public static final Color COLOR8 = new Color(102,147,53);

         public static final Color COLOR9 = new Color(209,202,40);

         public static final Color COLOR10 = new Color(20,159,230);

         public static final Color COLOR11 = new Color(180,43,48);

         public static final Color COLOR12 = new Color(183,159,210);

         public static final Color COLOR13 = new Color(227,128,30);

         public static final Color COLOR14 = new Color(255,208,147);

         public static final Color COLOR15 = new Color(198,188,35);

         public static final Color COLOR16 = new Color(255,139,154);

         public static final Color COLOR17 = new Color(209,198,9);

         public static final Color COLOR18 = new Color(20,103,183);

         public static final Color COLOR19 = new Color(255,169,52);

         public static final Color COLOR20 = new Color(227,79,30);

         public static final Color COLOR21= new Color(30,128,30);

         public static final Color COLOR22 = new Color(132,83,35);

         public ChartColor(int r, int g, int b) {

                   super(r, g, b);

                   // TODO Auto-generated constructor stub

         }

         public static Paint[] createDefaultPaintArray() {

        return new Paint[] {

                 ChartColor.COLOR1,

                 ChartColor.COLOR2,

                 ChartColor.COLOR3,

                 ChartColor.COLOR4,

                 ChartColor.COLOR5,

                 ChartColor.COLOR6,

                 ChartColor.COLOR7,

                 ChartColor.COLOR8,

                 ChartColor.COLOR9,

                 ChartColor.COLOR10,

                 ChartColor.COLOR11,

                 ChartColor.COLOR12,

                 ChartColor.COLOR13,

                 ChartColor.COLOR14,

                 ChartColor.COLOR15,

                 ChartColor.COLOR16,

                 ChartColor.COLOR17,

                 ChartColor.COLOR18,

                 ChartColor.COLOR19,

                 ChartColor.COLOR20,

                 ChartColor.COLOR21,

                 ChartColor.COLOR22,

        };

    }

}

编译后替换进JfreeChart的Jar包即可。

二、在JFreeChart的基础上进行扩展

如果担心存在开源协议问题的话,可以考虑这种方式。

新建一个色系类(同上,在此不做赘述)。

自定义一个DefaultDrawingSupplier类,该类位于org.jfree.chart.plot包中

代码如下:

/* ===========================================================

* JFreeChart : a free chart library for the Java(tm) platform

* ===========================================================

*

* (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.

*

* Project Info:  http://www.jfree.org/jfreechart/index.html

*

* This library is free software; you can redistribute it and/or modify it

* under the terms of the GNU Lesser General Public License as published by

* the Free Software Foundation; either version 2.1 of the License, or

* (at your option) any later version.

*

* This library 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 Lesser General Public

* License for more details.

*

* You should have received a copy of the GNU Lesser General Public

* License along with this library; if not, write to the Free Software

* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,

* USA.

*

* [Java is a trademark or registered trademark of Sun Microsystems, Inc.

* in the United States and other countries.]

*

* ---------------------------

* DefaultDrawingSupplier.java

* ---------------------------

* (C) Copyright 2003-2008, by Object Refinery Limited.

*

* Original Author:  David Gilbert (for Object Refinery Limited);

* Contributor(s):   Jeremy Bowman;

*

* Changes

* -------

* 16-Jan-2003 : Version 1 (DG);

* 17-Jan-2003 : Added stroke method, renamed DefaultPaintSupplier

*               --> DefaultDrawingSupplier (DG)

* 27-Jan-2003 : Incorporated code from SeriesShapeFactory, originally

*               contributed by Jeremy Bowman (DG);

* 25-Mar-2003 : Implemented Serializable (DG);

* 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);

* ------------- JFREECHART 1.0.x ---------------------------------------------

* 13-Jun-2007 : Added fillPaintSequence (DG);

*

*/

package org.jfree.chart.plot;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Paint;

import java.awt.Polygon;

import java.awt.Shape;

import java.awt.Stroke;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Rectangle2D;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

import java.util.Arrays;

import org.jfree.chart.ChartColor;

import org.jfree.io.SerialUtilities;

import org.jfree.util.PublicCloneable;

import org.jfree.util.ShapeUtilities;

/**

* A default implementation of the {@link DrawingSupplier} interface.  All

* {@link Plot} instances have a new instance of this class installed by

* default.

*/

public class DefaultDrawingSupplier implements DrawingSupplier, Cloneable,

        PublicCloneable, Serializable {

    /** For serialization. */

    private static final long serialVersionUID = -7339847061039422538L;

    /** The default fill paint sequence. */

    public static final Paint[] DEFAULT_PAINT_SEQUENCE

            = ChartColor.createDefaultPaintArray();

    /** The default outline paint sequence. */

    public static final Paint[] DEFAULT_OUTLINE_PAINT_SEQUENCE = new Paint[] {

            Color.lightGray};

    /** The default fill paint sequence. */

    public static final Paint[] DEFAULT_FILL_PAINT_SEQUENCE = new Paint[] {

            Color.white};

    /** The default stroke sequence. */

    public static final Stroke[] DEFAULT_STROKE_SEQUENCE = new Stroke[] {

            new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,

                    BasicStroke.JOIN_BEVEL)};

    /** The default outline stroke sequence. */

    public static final Stroke[] DEFAULT_OUTLINE_STROKE_SEQUENCE

            = new Stroke[] {new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,

                    BasicStroke.JOIN_BEVEL)};

    /** The default shape sequence. */

    public static final Shape[] DEFAULT_SHAPE_SEQUENCE

            = createStandardSeriesShapes();

    /** The paint sequence. */

    private transient Paint[] paintSequence;

    /** The current paint index. */

    private int paintIndex;

    /** The outline paint sequence. */

    private transient Paint[] outlinePaintSequence;

    /** The current outline paint index. */

    private int outlinePaintIndex;

    /** The fill paint sequence. */

    private transient Paint[] fillPaintSequence;

    /** The current fill paint index. */

    private int fillPaintIndex;

    /** The stroke sequence. */

    private transient Stroke[] strokeSequence;

    /** The current stroke index. */

    private int strokeIndex;

    /** The outline stroke sequence. */

    private transient Stroke[] outlineStrokeSequence;

    /** The current outline stroke index. */

    private int outlineStrokeIndex;

    /** The shape sequence. */

    private transient Shape[] shapeSequence;

    /** The current shape index. */

    private int shapeIndex;

    /**

     * Creates a new supplier, with default sequences for fill paint, outline

     * paint, stroke and shapes.

     */

    public DefaultDrawingSupplier() {

        this(DEFAULT_PAINT_SEQUENCE, DEFAULT_FILL_PAINT_SEQUENCE,

             DEFAULT_OUTLINE_PAINT_SEQUENCE,

             DEFAULT_STROKE_SEQUENCE,

             DEFAULT_OUTLINE_STROKE_SEQUENCE,

             DEFAULT_SHAPE_SEQUENCE);

    }

    /**

     * Creates a new supplier.

     *

     * @param paintSequence  the fill paint sequence.

     * @param outlinePaintSequence  the outline paint sequence.

     * @param strokeSequence  the stroke sequence.

     * @param outlineStrokeSequence  the outline stroke sequence.

     * @param shapeSequence  the shape sequence.

     */

    public DefaultDrawingSupplier(Paint[] paintSequence,

                                  Paint[] outlinePaintSequence,

                                  Stroke[] strokeSequence,

                                  Stroke[] outlineStrokeSequence,

                                  Shape[] shapeSequence) {

        this.paintSequence = paintSequence;

        this.fillPaintSequence = DEFAULT_FILL_PAINT_SEQUENCE;

        this.outlinePaintSequence = outlinePaintSequence;

        this.strokeSequence = strokeSequence;

        this.outlineStrokeSequence = outlineStrokeSequence;

        this.shapeSequence = shapeSequence;

    }

    /**

     * Creates a new supplier.

     *

     * @param paintSequence  the paint sequence.

     * @param fillPaintSequence  the fill paint sequence.

     * @param outlinePaintSequence  the outline paint sequence.

     * @param strokeSequence  the stroke sequence.

     * @param outlineStrokeSequence  the outline stroke sequence.

     * @param shapeSequence  the shape sequence.

     *

     * @since 1.0.6

     */

    public DefaultDrawingSupplier(Paint[] paintSequence,

            Paint[] fillPaintSequence, Paint[] outlinePaintSequence,

            Stroke[] strokeSequence, Stroke[] outlineStrokeSequence,

            Shape[] shapeSequence) {

        this.paintSequence = paintSequence;

        this.fillPaintSequence = fillPaintSequence;

        this.outlinePaintSequence = outlinePaintSequence;

        this.strokeSequence = strokeSequence;

        this.outlineStrokeSequence = outlineStrokeSequence;

        this.shapeSequence = shapeSequence;

    }

    /**

     * Returns the next paint in the sequence.

     *

     * @return The paint.

     */

    public Paint getNextPaint() {

        Paint result

            = this.paintSequence[this.paintIndex % this.paintSequence.length];

        this.paintIndex++;

        return result;

    }

    /**

     * Returns the next outline paint in the sequence.

     *

     * @return The paint.

     */

    public Paint getNextOutlinePaint() {

        Paint result = this.outlinePaintSequence[

                this.outlinePaintIndex % this.outlinePaintSequence.length];

        this.outlinePaintIndex++;

        return result;

    }

    /**

     * Returns the next fill paint in the sequence.

     *

     * @return The paint.

     *

     * @since 1.0.6

     */

    public Paint getNextFillPaint() {

        Paint result = this.fillPaintSequence[this.fillPaintIndex

                % this.fillPaintSequence.length];

        this.fillPaintIndex++;

        return result;

    }

    /**

     * Returns the next stroke in the sequence.

     *

     * @return The stroke.

     */

    public Stroke getNextStroke() {

        Stroke result = this.strokeSequence[

                this.strokeIndex % this.strokeSequence.length];

        this.strokeIndex++;

        return result;

    }

    /**

     * Returns the next outline stroke in the sequence.

     *

     * @return The stroke.

     */

    public Stroke getNextOutlineStroke() {

        Stroke result = this.outlineStrokeSequence[

                this.outlineStrokeIndex % this.outlineStrokeSequence.length];

        this.outlineStrokeIndex++;

        return result;

    }

    /**

     * Returns the next shape in the sequence.

     *

     * @return The shape.

     */

    public Shape getNextShape() {

        Shape result = this.shapeSequence[

                this.shapeIndex % this.shapeSequence.length];

        this.shapeIndex++;

        return result;

    }

    /**

     * Creates an array of standard shapes to display for the items in series

     * on charts.

     *

     * @return The array of shapes.

     */

    public static Shape[] createStandardSeriesShapes() {

        Shape[] result = new Shape[10];

        double size = 6.0;

        double delta = size / 2.0;

        int[] xpoints = null;

        int[] ypoints = null;

        // square

        result[0] = new Rectangle2D.Double(-delta, -delta, size, size);

        // circle

        result[1] = new Ellipse2D.Double(-delta, -delta, size, size);

        // up-pointing triangle

        xpoints = intArray(0.0, delta, -delta);

        ypoints = intArray(-delta, delta, delta);

        result[2] = new Polygon(xpoints, ypoints, 3);

        // diamond

        xpoints = intArray(0.0, delta, 0.0, -delta);

        ypoints = intArray(-delta, 0.0, delta, 0.0);

        result[3] = new Polygon(xpoints, ypoints, 4);

        // horizontal rectangle

        result[4] = new Rectangle2D.Double(-delta, -delta / 2, size, size / 2);

        // down-pointing triangle

        xpoints = intArray(-delta, +delta, 0.0);

        ypoints = intArray(-delta, -delta, delta);

        result[5] = new Polygon(xpoints, ypoints, 3);

        // horizontal ellipse

        result[6] = new Ellipse2D.Double(-delta, -delta / 2, size, size / 2);

        // right-pointing triangle

        xpoints = intArray(-delta, delta, -delta);

        ypoints = intArray(-delta, 0.0, delta);

        result[7] = new Polygon(xpoints, ypoints, 3);

        // vertical rectangle

        result[8] = new Rectangle2D.Double(-delta / 2, -delta, size / 2, size);

        // left-pointing triangle

        xpoints = intArray(-delta, delta, delta);

        ypoints = intArray(0.0, -delta, +delta);

        result[9] = new Polygon(xpoints, ypoints, 3);

        return result;

    }

    /**

     * Tests this object for equality with another object.

     *

     * @param obj  the object (<code>null</code> permitted).

     *

     * @return A boolean.

     */

    public boolean equals(Object obj) {

        if (obj == this) {

            return true;

        }

        if (!(obj instanceof DefaultDrawingSupplier)) {

            return false;

        }

        DefaultDrawingSupplier that = (DefaultDrawingSupplier) obj;

        if (!Arrays.equals(this.paintSequence, that.paintSequence)) {

            return false;

        }

        if (this.paintIndex != that.paintIndex) {

            return false;

        }

        if (!Arrays.equals(this.outlinePaintSequence,

                that.outlinePaintSequence)) {

            return false;

        }

        if (this.outlinePaintIndex != that.outlinePaintIndex) {

            return false;

        }

        if (!Arrays.equals(this.strokeSequence, that.strokeSequence)) {

            return false;

        }

        if (this.strokeIndex != that.strokeIndex) {

            return false;

        }

        if (!Arrays.equals(this.outlineStrokeSequence,

                that.outlineStrokeSequence)) {

            return false;

        }

        if (this.outlineStrokeIndex != that.outlineStrokeIndex) {

            return false;

        }

        if (!equalShapes(this.shapeSequence, that.shapeSequence)) {

            return false;

        }

        if (this.shapeIndex != that.shapeIndex) {

            return false;

        }

        return true;

    }

    /**

     * A utility method for testing the equality of two arrays of shapes.

     *

     * @param s1  the first array (<code>null</code> permitted).

     * @param s2  the second array (<code>null</code> permitted).

     *

     * @return A boolean.

     */

    private boolean equalShapes(Shape[] s1, Shape[] s2) {

        if (s1 == null) {

            return s2 == null;

        }

        if (s2 == null) {

            return false;

        }

        if (s1.length != s2.length) {

            return false;

        }

        for (int i = 0; i < s1.length; i++) {

            if (!ShapeUtilities.equal(s1[i], s2[i])) {

                return false;

            }

        }

        return true;

    }

    /**

     * Handles serialization.

     *

     * @param stream  the output stream.

     *

     * @throws IOException if there is an I/O problem.

     */

    private void writeObject(ObjectOutputStream stream) throws IOException {

        stream.defaultWriteObject();

        int paintCount = this.paintSequence.length;

        stream.writeInt(paintCount);

        for (int i = 0; i < paintCount; i++) {

            SerialUtilities.writePaint(this.paintSequence[i], stream);

        }

        int outlinePaintCount = this.outlinePaintSequence.length;

        stream.writeInt(outlinePaintCount);

        for (int i = 0; i < outlinePaintCount; i++) {

            SerialUtilities.writePaint(this.outlinePaintSequence[i], stream);

        }

        int strokeCount = this.strokeSequence.length;

        stream.writeInt(strokeCount);

        for (int i = 0; i < strokeCount; i++) {

            SerialUtilities.writeStroke(this.strokeSequence[i], stream);

        }

        int outlineStrokeCount = this.outlineStrokeSequence.length;

        stream.writeInt(outlineStrokeCount);

        for (int i = 0; i < outlineStrokeCount; i++) {

            SerialUtilities.writeStroke(this.outlineStrokeSequence[i], stream);

        }

        int shapeCount = this.shapeSequence.length;

        stream.writeInt(shapeCount);

        for (int i = 0; i < shapeCount; i++) {

            SerialUtilities.writeShape(this.shapeSequence[i], stream);

        }

    }

    /**

     * Restores a serialized object.

     *

     * @param stream  the input stream.

     *

     * @throws IOException if there is an I/O problem.

     * @throws ClassNotFoundException if there is a problem loading a class.

     */

    private void readObject(ObjectInputStream stream)

        throws IOException, ClassNotFoundException {

        stream.defaultReadObject();

        int paintCount = stream.readInt();

        this.paintSequence = new Paint[paintCount];

        for (int i = 0; i < paintCount; i++) {

            this.paintSequence[i] = SerialUtilities.readPaint(stream);

        }

        int outlinePaintCount = stream.readInt();

        this.outlinePaintSequence = new Paint[outlinePaintCount];

        for (int i = 0; i < outlinePaintCount; i++) {

            this.outlinePaintSequence[i] = SerialUtilities.readPaint(stream);

        }

        int strokeCount = stream.readInt();

        this.strokeSequence = new Stroke[strokeCount];

        for (int i = 0; i < strokeCount; i++) {

            this.strokeSequence[i] = SerialUtilities.readStroke(stream);

        }

        int outlineStrokeCount = stream.readInt();

        this.outlineStrokeSequence = new Stroke[outlineStrokeCount];

        for (int i = 0; i < outlineStrokeCount; i++) {

            this.outlineStrokeSequence[i] = SerialUtilities.readStroke(stream);

        }

        int shapeCount = stream.readInt();

        this.shapeSequence = new Shape[shapeCount];

        for (int i = 0; i < shapeCount; i++) {

            this.shapeSequence[i] = SerialUtilities.readShape(stream);

        }

    }

    /**

     * Helper method to avoid lots of explicit casts in getShape().  Returns

     * an array containing the provided doubles cast to ints.

     *

     * @param a  x

     * @param b  y

     * @param c  z

     *

     * @return int[3] with converted params.

     */

    private static int[] intArray(double a, double b, double c) {

        return new int[] {(int) a, (int) b, (int) c};

    }

    /**

     * Helper method to avoid lots of explicit casts in getShape().  Returns

     * an array containing the provided doubles cast to ints.

     *

     * @param a  x

     * @param b  y

     * @param c  z

     * @param d  t

     *

     * @return int[4] with converted params.

     */

    private static int[] intArray(double a, double b, double c, double d) {

        return new int[] {(int) a, (int) b, (int) c, (int) d};

    }

    /**

     * Returns a clone.

     *

     * @return A clone.

     *

     * @throws CloneNotSupportedException if a component of the supplier does

     *                                    not support cloning.

     */

    public Object clone() throws CloneNotSupportedException {

        DefaultDrawingSupplier clone = (DefaultDrawingSupplier) super.clone();

        return clone;

    }

}

只需要如下代码中的"ChartColor" 替换成自定义色系类即可。

    public static final Paint[] DEFAULT_PAINT_SEQUENCE

            = ChartColor.createDefaultPaintArray();

然后在生成JFreeChart实例中设置DrawingSupplier属性即可,代码片段如下:

JFreeChart jfreechart = createChart(categorydataset);

        jfreechart.getPlot().setDrawingSupplier(new CustomDrawingSupplier());

 

这篇是从我以前CSDN博客上转来的,系本人原创,转载请注明出处。

posted @ 2010-01-17 00:27  希言  阅读(1840)  评论(0编辑  收藏  举报