how can I change font color of the legend?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Elly
Posts: 19
Joined: Tue Jun 07, 2005 2:26 pm
Location: Germany
Contact:

how can I change font color of the legend?

Post by Elly » Wed Aug 03, 2005 4:35 pm

how can I change font color of the legend?

thanx in advance
Elly
using JFreeChart 1.0.0 rc1, Java 1.4.2

Elly
Posts: 19
Joined: Tue Jun 07, 2005 2:26 pm
Location: Germany
Contact:

Post by Elly » Fri Aug 05, 2005 12:29 pm

is it really impossible? I wasn't able to find it anywhere.
It is possible to change the font itself, but how to change it's color???
using JFreeChart 1.0.0 rc1, Java 1.4.2

Guest

Post by Guest » Tue Aug 23, 2005 1:11 pm

hi, HELP, I steel need an answer to that question!!!
Please!!!

pmlb

Post by pmlb » Tue Aug 23, 2005 3:18 pm

I guess that you should first get the legend from the chart then set the paint for the legend items, for instance:

Code: Select all

StandardLegend sl = (StandardLegend)yourChart.getLegend();
sl.setItemPaint(Color.red);
Pierre-Marie

Elly
Posts: 19
Joined: Tue Jun 07, 2005 2:26 pm
Location: Germany
Contact:

Post by Elly » Tue Aug 23, 2005 3:39 pm

Hi,
thank you for the answer.
which JFreeChart Version do you use?

I use the 1.0.0. rc1 an there is no method "setItemPaint" for legend...
Class "StandardLegend" doesn't exist also...

or am I doing something wrong :shock:

best wishes Elena.
using JFreeChart 1.0.0 rc1, Java 1.4.2

pmlb

Post by pmlb » Tue Aug 23, 2005 5:11 pm

Sorry Elena I use the previous version (1.0.0-pre2) and I did not check if this still exists.

In the 1.0.0-pre2 version the class is org.jfree.chart.StandardLegend and it extends Legend and has a setItemPaint method.

I will check and keep you informed.

Pierre-Marie

pmlb

Post by pmlb » Tue Aug 23, 2005 6:07 pm

Well the legend management has changed a lot, and I can't see for the while how to modify the legend color.

Sorry about this. I'll try to look further.

Pierre-Marie

cris-rj
Posts: 16
Joined: Mon Oct 11, 2004 2:44 pm
Location: rio de janeiro, brazil

Post by cris-rj » Tue Aug 23, 2005 6:15 pm

I think you need use this:
LegendTitle leg = chart.getLegend();
[]´s
Cristiano

pmlb

Post by pmlb » Thu Aug 25, 2005 5:04 pm

Hi Elena,

Sorry for the delay, I was off yesterday.

It seems that the color of the legend is hardcoded to Color.black and the only way to change it is to ask for an enhancement and/or customise JFreeChart:

Two classes are involved:
org.jfree.chart.title.LegendTitle
org.jfree.chart.block.LabelBlock

Regenerate jfreechart-1.0.0-rc1.jar with the modified classes joined to this post and you will be able to change the color of the legend this way:

Code: Select all

LegendTitle legend = yourChart.getLegend();
legend.setItemPaint(Color.red);
This time I tried the code, it works.

Pierre-Marie

org.jfree.chart.block.LabelBlock modified:

Code: Select all

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2005, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------
 * LabelBlock.java
 * ---------------
 * (C) Copyright 2004, 2005, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: LabelBlock.java,v 1.8 2005/05/13 15:14:18 mungady Exp $
 *
 * Changes:
 * --------
 * 22-Oct-2004 : Version 1 (DG);
 * 19-Apr-2005 : Added optional tooltip and URL text items,
 *               draw() method now returns entities if 
 *               requested (DG);
 * 13-May-2005 : Added methods to set the font (DG);
 * 25-Aug-2005 : Added paint management (PMLB);
 * 
 */

package org.jfree.chart.block;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
// PMLB
import java.awt.Paint;
// FIN PMLB
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.text.TextBlock;
import org.jfree.text.TextBlockAnchor;
import org.jfree.text.TextUtilities;
import org.jfree.ui.Size2D;

/**
 * A block containing a label.
 */
public class LabelBlock extends AbstractBlock implements Block {
    
    /** 
     * The text for the label - retained in case the label needs 
     * regenerating (for example, to change the font). 
     */
    private String text;
    
    /** The label. */
    private TextBlock label;
    
    /** The font. */
    private Font font;
    
    /** The tool tip text (can be <code>null</code>). */
    private String toolTipText;
    
    /** The URL text (can be <code>null</code>). */
    private String urlText;
    
    // PMLB
    /** The default color. */
    public static final Paint DEFAULT_PAINT = Color.black;

    /** The paint. */
    private Paint paint;
    // FIN PMLB
    
    /**
     * Creates a new label block.
     * 
     * @param label  the label (<code>null</code> not permitted).
     */
    public LabelBlock(String label) {
    	// PMLB
        //this(label, new Font("SansSerif", Font.PLAIN, 10));
        this(label, new Font("SansSerif", Font.PLAIN, 10), DEFAULT_PAINT);
        // FIN PMLB
    }
    
    /**
     * Creates a new label block.
     * 
     * @param text  the text for the label (<code>null</code> not permitted).
     * @param font  the font (<code>null</code> not permitted).
     */
    public LabelBlock(String text, Font font) {  
    	// PMLB      
        //this.text = text;
        //this.label = TextUtilities.createTextBlock(text, font, Color.black);
        //this.font = font;
        //this.toolTipText = null;
        //this.urlText = null;
        this(text, font, DEFAULT_PAINT);
        // FIN PMLB
    }
    
    // PMLB
    /**
     * Creates a new label block.
     * 
     * @param text  the text for the label (<code>null</code> not permitted).
     * @param font  the font (<code>null</code> not permitted).
     * @param paint the paint (<code>null</code> not permitted).
     */
    public LabelBlock(String text, Font font, Paint paint) {        
        this.text = text;
        this.paint = paint;
        this.label = TextUtilities.createTextBlock(text, font, this.paint);
        this.font = font;
        this.toolTipText = null;
        this.urlText = null;
    }
    // FIN PMLB
    
    /**
     * Returns the font.
     *
     * @return The font.
     */
    public Font getFont() {
        return this.font;    
    }
    
    /**
     * Sets the font and regenerates the label.
     *
     * @param font  the font.
     */
    public void setFont(Font font) {
        this.font = font;
        // PMLB
        //this.label = TextUtilities.createTextBlock(
        //    this.text, font, Color.black
        //);
        this.label = TextUtilities.createTextBlock(
            this.text, font, this.paint
        );
        // FIN PMLB
    }
    
    //PMLB
    /**
     * Returns the paint.
     *
     * @return The paint.
     */
    public Paint getPaint() {
        return this.paint;    
    }
    
    /**
     * Sets the paint and regenerates the label.
     *
     * @param paint  the paint.
     */
    public void setPaint(Paint paint) {
        this.paint = paint;
        this.label = TextUtilities.createTextBlock(
            this.text, font, this.paint
        );
    }
    // FIN PMLB
    
    /**
     * Returns the tool tip text.
     * 
     * @return The tool tip text (possibly <code>null</code>).
     */
    public String getToolTipText() {
        return this.toolTipText;
    }
    
    /**
     * Sets the tool tip text.
     * 
     * @param text  the text (<code>null</code> permitted).
     */
    public void setToolTipText(String text) {
        this.toolTipText = text;   
    }
    
    /**
     * Returns the URL text.
     * 
     * @return The URL text (possibly <code>null</code>).
     */
    public String getURLText() {
        return this.urlText;
    }
    
    /**
     * Sets the URL text.
     * 
     * @param text  the text (<code>null</code> permitted).
     */
    public void setURLText(String text) {
        this.urlText = text;   
    }
    
    /**
     * Arranges the contents of the block, within the given constraints, and 
     * returns the block size.
     * 
     * @param g2  the graphics device.
     * @param constraint  the constraint (<code>null</code> not permitted).
     * 
     * @return The block size (in Java2D units, never <code>null</code>).
     */
    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
        RectangleConstraint contentConstraint = toContentConstraint(constraint);
        g2.setFont(this.font);
        Size2D s = this.label.calculateDimensions(g2);
        return new Size2D(
            calculateTotalWidth(s.getWidth()), 
            calculateTotalHeight(s.getHeight())
        );
    }
    
    /**
     * Draws the block.
     * 
     * @param g2  the graphics device.
     * @param area  the area.
     */
    public void draw(Graphics2D g2, Rectangle2D area) {
        draw(g2, area, null);
    }
    
    /**
     * Draws the block within the specified area.
     * 
     * @param g2  the graphics device.
     * @param area  the area.
     * @param params  ignored (<code>null</code> permitted).
     * 
     * @return Always <code>null</code>.
     */
    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
        area = trimMargin(area);
        drawBorder(g2, area);
        area = trimBorder(area);
        area = trimPadding(area);
        
        // check if we need to collect chart entities from the container
        EntityBlockParams ebp = null;
        StandardEntityCollection sec = null;
        Shape entityArea = null;
        if (params instanceof EntityBlockParams) {
            ebp = (EntityBlockParams) params;
            if (ebp.getGenerateEntities()) {
                sec = new StandardEntityCollection();
                // TODO:  this transformation doesn't work always.  Fix!
                entityArea = g2.getTransform().createTransformedShape(area);
            }
        }
        //PMLB
        //g2.setPaint(Color.black);
        g2.setPaint(this.paint);
        // FIN PMLB
        g2.setFont(this.font);
        this.label.draw(
            g2, (float) area.getX(), (float) area.getY(), 
            TextBlockAnchor.TOP_LEFT
        );
        BlockResult result = null;
        if (ebp != null && sec != null) {
            if (this.toolTipText != null || this.urlText != null) {
                ChartEntity entity = new ChartEntity(
                    entityArea, this.toolTipText, this.urlText
                );   
                sec.add(entity);
                result = new BlockResult();
                result.setEntityCollection(sec);
            }
        }
        return result;
    }

}
org.jfree.chart.title.LegendTitle modified:

Code: Select all

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2005, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 * 
 * ----------------
 * LegendTitle.java
 * ----------------
 * (C) Copyright 2002-2005, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 * $Id: LegendTitle.java,v 1.20 2005/05/19 15:44:59 mungady Exp $
 *
 * Changes
 * -------
 * 25-Nov-2004 : First working version (DG);
 * 11-Jan-2005 : Removed deprecated code in preparation for 1.0.0 release (DG);
 * 08-Feb-2005 : Updated for changes in RectangleConstraint class (DG);
 * 11-Feb-2005 : Implemented PublicCloneable (DG);
 * 23-Feb-2005 : Replaced chart reference with LegendItemSource (DG);
 * 16-Mar-2005 : Added itemFont attribute (DG);
 * 17-Mar-2005 : Fixed missing fillShape setting (DG);
 * 20-Apr-2005 : Added new draw() method (DG);
 * 03-May-2005 : Modified equals() method to ignore sources (DG);
 * 13-May-2005 : Added settings for legend item label and graphic padding (DG);
 * 25-May-2005 : Added itemPaint attribute (PMLB);
 * 
 */

package org.jfree.chart.title;

// PMLB
import java.awt.Color;
// FIN PMLB
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.LegendItemSource;
import org.jfree.chart.block.Arrangement;
import org.jfree.chart.block.Block;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.CenterArrangement;
import org.jfree.chart.block.ColumnArrangement;
import org.jfree.chart.block.FlowArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.block.RectangleConstraint;
import org.jfree.chart.event.TitleChangeEvent;
import org.jfree.io.SerialUtilities;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.Size2D;
import org.jfree.util.PaintUtilities;
import org.jfree.util.PublicCloneable;

/**
 * A chart title that displays a legend for the data in the chart.
 * <P>
 * The title can be populated with legend items manually, or you can assign a
 * reference to the plot, in which case the legend items will be automatically
 * created to match the dataset(s).
 */
public class LegendTitle extends Title 
                         implements Cloneable, PublicCloneable, Serializable {

    /** For serialization. */
    private static final long serialVersionUID = 2644010518533854633L;
    
    /** The default item font. */
    public static final Font DEFAULT_ITEM_FONT 
        = new Font("SansSerif", Font.PLAIN, 12);
        
    // PMLB
    /** The default item paint. */
    public static final Paint DEFAULT_ITEM_PAINT = Color.black;
    // FIN PMLB

    /** The sources for legend items. */
    private transient LegendItemSource[] sources;
    
    /** The background paint (possibly <code>null</code>). */
    private transient Paint backgroundPaint;
    
    /** The edge for the legend item graphic relative to the text. */
    private RectangleEdge legendItemGraphicEdge;
    
    /** The anchor point for the legend item graphic. */
    private RectangleAnchor legendItemGraphicAnchor;
    
    /** The legend item graphic location. */
    private RectangleAnchor legendItemGraphicLocation;
    
    /** The padding for the legend item graphic. */
    private RectangleInsets legendItemGraphicPadding;

    /** The item font. */
    private Font itemFont;
    
    // PMLB
    /** The item paint. */
    private Paint itemPaint;
    // FIN PMLB
    
    /** The padding for the item labels. */
    private RectangleInsets itemLabelPadding;

    /**
     * A container that holds and displays the legend items.
     */
    private BlockContainer items;
    
    private Arrangement hLayout;
    
    private Arrangement vLayout;
    
    /** 
     * An optional container for wrapping the legend items (allows for adding
     * a title or other text to the legend). 
     */
    private BlockContainer wrapper;

    /**
     * Constructs a new (empty) legend for the specified source.
     * 
     * @param source  the source.
     */
    public LegendTitle(LegendItemSource source) {
        this(source, new FlowArrangement(), new ColumnArrangement());
    }
    
    /**
     * Creates a new legend title with the specified arrangement.
     * 
     * @param source  the source.
     * @param hLayout  the horizontal item arrangement (<code>null</code> not
     *                 permitted).
     * @param vLayout  the vertical item arrangement (<code>null</code> not
     *                 permitted).
     */
    public LegendTitle(LegendItemSource source, 
                       Arrangement hLayout, Arrangement vLayout) {
        this.sources = new LegendItemSource[] {source};
        this.items = new BlockContainer(hLayout);
        this.hLayout = hLayout;
        this.vLayout = vLayout;
        this.backgroundPaint = null;  
        this.legendItemGraphicEdge = RectangleEdge.LEFT;
        this.legendItemGraphicAnchor = RectangleAnchor.CENTER;
        this.legendItemGraphicLocation = RectangleAnchor.CENTER;
        this.legendItemGraphicPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
        this.itemFont = DEFAULT_ITEM_FONT;
        // PMLB
        this.itemPaint = DEFAULT_ITEM_PAINT;
        // FIN PMLB
        this.itemLabelPadding = new RectangleInsets(2.0, 2.0, 2.0, 2.0);
    }
    
    /**
     * Returns the legend item sources.
     * 
     * @return The sources.
     */
    public LegendItemSource[] getSources() {
        return this.sources;   
    }
    
    /**
     * Sets the legend item sources.
     * 
     * @param sources  the sources.
     */
    public void setSources(LegendItemSource[] sources) {
        this.sources = sources;
        notifyListeners(new TitleChangeEvent(this));
    }

    /**
     * Returns the background paint.
     * 
     * @return The background paint (possibly <code>null</code>).
     */
    public Paint getBackgroundPaint() {
        return this.backgroundPaint;   
    }
    
    /**
     * Sets the background paint for the legend.
     * 
     * @param paint  the paint (<code>null</code> permitted).
     */
    public void setBackgroundPaint(Paint paint) {
        this.backgroundPaint = paint;   
    }
    
    /**
     * Returns the location of the shape within each legend item. 
     * 
     * @return The location (never <code>null</code>).
     */
    public RectangleEdge getLegendItemGraphicEdge() {
        return this.legendItemGraphicEdge;
    }
    
    /**
     * Sets the location of the shape within each legend item.
     * 
     * @param edge  the edge (<code>null</code> not permitted).
     */
    public void setLegendItemGraphicEdge(RectangleEdge edge) {
        if (edge == null) {
            throw new IllegalArgumentException("Null 'edge' argument.");
        }
        this.legendItemGraphicEdge = edge;
    }
    
    /**
     * Returns the legend item graphic anchor.
     * 
     * @return The graphic anchor (never <code>null</code>).
     */
    public RectangleAnchor getLegendItemGraphicAnchor() {
        return this.legendItemGraphicAnchor;
    }
    
    /**
     * Sets the anchor point used for the graphic in each legend item.
     * 
     * @param anchor  the anchor point (<code>null</code> not permitted).
     */
    public void setLegendItemGraphicAnchor(RectangleAnchor anchor) {
        if (anchor == null) {
            throw new IllegalArgumentException("Null 'anchor' point.");
        }
        this.legendItemGraphicAnchor = anchor;
    }
    
    /**
     * Returns the legend item graphic location.
     * 
     * @return The location (never <code>null</code>).
     */
    public RectangleAnchor getLegendItemGraphicLocation() {
        return this.legendItemGraphicLocation;
    }
    
    /**
     * Sets the legend item graphic location.
     * 
     * @param anchor  the anchor (<code>null</code> not permitted).
     */
    public void setLegendItemGraphicLocation(RectangleAnchor anchor) {
        this.legendItemGraphicLocation = anchor;
    }
    
    /**
     * Returns the padding used for the legend item graphics.
     * 
     * @return The padding.
     */
    public RectangleInsets getLegendItemGraphicPadding() {
        return this.legendItemGraphicPadding;   
    }
    
    /**
     * Sets the padding used for the legend item graphics.
     * 
     * @param padding  the padding (<code>null</code> not permitted).
     */
    public void setLegendItemGraphicPadding(RectangleInsets padding) {
        if (padding == null) {
            throw new IllegalArgumentException("Null 'padding' argument.");   
        }
        this.legendItemGraphicPadding = padding;
        notifyListeners(new TitleChangeEvent(this));
    }
    
    /**
     * Returns the item font.
     * 
     * @return The font (never <code>null</code>).
     */
    public Font getItemFont() {
        return this.itemFont;   
    }
    
    /**
     * Sets the item font.
     * 
     * @param font  the font (<code>null</code> not permitted).
     */
    public void setItemFont(Font font) {
        if (font == null) {
            throw new IllegalArgumentException("Null 'font' argument.");   
        }
        this.itemFont = font;
        notifyListeners(new TitleChangeEvent(this));
    }
    
    // PMLB
    /**
     * Returns the item paint.
     * 
     * @return The paint (never <code>null</code>).
     */
    public Paint getItemPaint() {
        return this.itemPaint;   
    }
    
    /**
     * Sets the item paint.
     * 
     * @param paint  the paint (<code>null</code> not permitted).
     */
    public void setItemPaint(Paint paint) {
        if (paint == null) {
            throw new IllegalArgumentException("Null 'paint' argument.");   
        }
        this.itemPaint = paint;
        notifyListeners(new TitleChangeEvent(this));
    }
    // FIN PMLB
    
    /**
     * Returns the padding used for the items labels.
     * 
     * @return The padding.
     */
    public RectangleInsets getItemLabelPadding() {
        return this.itemLabelPadding;   
    }
    
    /**
     * Sets the padding used for the item labels in the legend.
     * 
     * @param padding  the padding (<code>null</code> not permitted).
     */
    public void setItemLabelPadding(RectangleInsets padding) {
        if (padding == null) {
            throw new IllegalArgumentException("Null 'padding' argument.");   
        }
        this.itemLabelPadding = padding;
        notifyListeners(new TitleChangeEvent(this));
    }
    
    /**
     * Fetches the latest legend items.
     */
    protected void fetchLegendItems() {
        this.items.clear();
        RectangleEdge p = getPosition();
        if (RectangleEdge.isTopOrBottom(p)) {
            this.items.setArrangement(this.hLayout);   
        }
        else {
            this.items.setArrangement(this.vLayout);   
        }
        for (int s = 0; s < this.sources.length; s++) {
            LegendItemCollection legendItems = this.sources[s].getLegendItems();
            if (legendItems != null) {
                for (int i = 0; i < legendItems.getItemCount(); i++) {
                    LegendItem item = legendItems.get(i);
                    Block block = createLegendItemBlock(item);
                    this.items.add(block);
                }
            }
        }
    }
    
    /**
     * Creates a legend item block.
     * 
     * @param item  the legend item.
     * 
     * @return The block.
     */
    protected Block createLegendItemBlock(LegendItem item) {
        BlockContainer result = null;
        LegendGraphic lg = new LegendGraphic(
            item.getShape(), item.getFillPaint()
        );
        lg.setShapeFilled(item.isShapeFilled());
        lg.setLine(item.getLine());
        lg.setLineStroke(item.getLineStroke());
        lg.setLinePaint(item.getLinePaint());
        lg.setLineVisible(item.isLineVisible());
        lg.setShapeVisible(item.isShapeVisible());
        lg.setShapeOutlineVisible(item.isShapeOutlineVisible());
        lg.setOutlinePaint(item.getOutlinePaint());
        lg.setOutlineStroke(item.getOutlineStroke());
        lg.setPadding(this.legendItemGraphicPadding);

        BlockContainer legendItem = new BlockContainer(new BorderArrangement());
        lg.setShapeAnchor(getLegendItemGraphicAnchor());
        lg.setShapeLocation(getLegendItemGraphicLocation());
        legendItem.add(lg, this.legendItemGraphicEdge);
        // PMLB
        //LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont);
        LabelBlock labelBlock = new LabelBlock(item.getLabel(), this.itemFont, this.itemPaint);
        // FIN PMLB
        labelBlock.setPadding(this.itemLabelPadding);
        labelBlock.setToolTipText(item.getToolTipText());
        legendItem.add(labelBlock);
        
        result = new BlockContainer(new CenterArrangement());
        result.add(legendItem);
        
        return result;
    }
    
    /**
     * Returns the container that holds the legend items.
     * 
     * @return The container for the legend items.
     */
    public BlockContainer getItemContainer() {
        return this.items;
    }

    /**
     * Arranges the contents of the block, within the given constraints, and 
     * returns the block size.
     * 
     * @param g2  the graphics device.
     * @param constraint  the constraint (<code>null</code> not permitted).
     * 
     * @return The block size (in Java2D units, never <code>null</code>).
     */
    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
        Size2D result = new Size2D();
        fetchLegendItems();
        if (this.items.isEmpty()) {
            return result;   
        }
        BlockContainer container = this.wrapper;
        if (container == null) {
            container = this.items;
        }
        RectangleConstraint c = toContentConstraint(constraint);
        Size2D size = container.arrange(g2, c);
        result.height = calculateTotalHeight(size.height);
        result.width = calculateTotalWidth(size.width);
        return result;
    }

    /**
     * Draws the title on a Java 2D graphics device (such as the screen or a
     * printer).
     *
     * @param g2  the graphics device.
     * @param area  the available area for the title.
     */
    public void draw(Graphics2D g2, Rectangle2D area) {
        draw(g2, area, null);
    }

    /**
     * Draws the block within the specified area.
     * 
     * @param g2  the graphics device.
     * @param area  the area.
     * @param params  ignored (<code>null</code> permitted).
     * 
     * @return An {@link org.jfree.chart.block.EntityBlockResult} or 
     *         <code>null</code>.
     */
    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
        Rectangle2D target = (Rectangle2D) area.clone();
        target = trimMargin(target);
        if (this.backgroundPaint != null) {
            g2.setPaint(this.backgroundPaint);
            g2.fill(target);
        }
        getBorder().draw(g2, target);
        getBorder().getInsets().trim(target);
        BlockContainer container = this.wrapper;
        if (container == null) {
            container = this.items; 
        }
        target = trimPadding(target);
        return container.draw(g2, target, params);   
    }

    /**
     * Sets the wrapper container for the legend.
     * 
     * @param wrapper  the wrapper container.
     */
    public void setWrapper(BlockContainer wrapper) {
        this.wrapper = wrapper;
    }
    
    /**
     * Tests this title for equality with an arbitrary 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 LegendTitle)) {
            return false;   
        }
        if (!super.equals(obj)) {
            return false;   
        }
        LegendTitle that = (LegendTitle) obj;
        if (!PaintUtilities.equal(this.backgroundPaint, that.backgroundPaint)) {
            return false;   
        }
        if (this.legendItemGraphicEdge != that.legendItemGraphicEdge) {
            return false;   
        }
        if (this.legendItemGraphicAnchor != that.legendItemGraphicAnchor) {
            return false;   
        }
        if (this.legendItemGraphicLocation != that.legendItemGraphicLocation) {
            return false;   
        }
        if (!this.itemFont.equals(that.itemFont)) {
            return false;   
        }
        // PMLB
        if (!this.itemPaint.equals(that.itemPaint)) {
            return false;   
        }
        // FIN PMLB
        if (!this.hLayout.equals(that.hLayout)) {
            return false;   
        }
        if (!this.vLayout.equals(that.vLayout)) {
            return false;   
        }
        return true;
    }
    
    /**
     * Provides serialization support.
     *
     * @param stream  the output stream.
     *
     * @throws IOException  if there is an I/O error.
     */
    private void writeObject(ObjectOutputStream stream) throws IOException {
        stream.defaultWriteObject();
        SerialUtilities.writePaint(this.backgroundPaint, stream);
        // PMLB
        SerialUtilities.writePaint(this.itemPaint, stream);
        // FIN PMLB
    }

    /**
     * Provides serialization support.
     *
     * @param stream  the input stream.
     *
     * @throws IOException  if there is an I/O error.
     * @throws ClassNotFoundException  if there is a classpath problem.
     */
    private void readObject(ObjectInputStream stream) 
        throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        this.backgroundPaint = SerialUtilities.readPaint(stream);
        // PMLB
        this.itemPaint = SerialUtilities.readPaint(stream);
        // FIN PMLB
        this.sources = new LegendItemSource[0];
    }

}

Elly
Posts: 19
Joined: Tue Jun 07, 2005 2:26 pm
Location: Germany
Contact:

Post by Elly » Thu Aug 25, 2005 5:58 pm

Hi,
thank you very much for the answer!!!

Asking my question I was very afraid to learn that the only way to change the color is to edit JFreeChart-Library, and that seems to be really so :-(

I really don't understand why!
In the older versions of JFreeChart there was a possibility to change the color of the legend, why it was deleted????? :-(

Thank you for the code, I will try it out. But I'm very dissapointed of JFreeChart now!
I also tried to write my own classes inheriting JFreeChart, but it was also impossible as everything is declared private, so I had to change the library itself. Now with every update I have to move my changes to the new version of JFreeChart every time :-(
That is really not the right way in the object oriented programming as Java :-(
using JFreeChart 1.0.0 rc1, Java 1.4.2

pmlb
Posts: 31
Joined: Thu Aug 25, 2005 5:18 pm
Location: France

Post by pmlb » Thu Aug 25, 2005 6:40 pm

Of course JFreeChart is not perfect but it is getting better and better with time.

Concerning the legend color I suppose that the fonctionnality has been forgotten when legend management has been reorganised.

Besides from that, your modifications could contribute to this improvement, and once integrated no need to apply to new versions!

Best regards.
Pierre-Marie

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

Post by david.gilbert » Thu Sep 01, 2005 4:40 pm

Thanks for the suggested fix. I've incorporated your code into CVS ready for the next release. Sorry about this missing feature...
David Gilbert
JFreeChart Project Leader

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

Locked