How to add annotations OUTSIDE plot area?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
oscar
Posts: 26
Joined: Fri May 28, 2004 10:01 am
Location: Switzerland

How to add annotations OUTSIDE plot area?

Post by oscar » Fri Jun 04, 2004 9:45 am

I understand how to place text annotations in a plot using x,y coordinates of the plot data for positioning. However, now I need to place annotations outside the plot area.

I've been through the archives and checked all the demo classes and can't see any examples of this. Also, the API doesn't have any methods that can be used on a JfreeChart object. The nearest is addSubtitle but this allows only the most basic positioning (TOP,BOTTOM, LEFT, RIGHT) - I need to define the position at the level of pixels. I have a chill feeling it can't be done... Can it?

Example:

Code: Select all

+------------------------------------------------------------+
¦                                                            ¦
¦                                               annotate     ¦
¦                                               here how?    ¦
¦         +----------------------------------+               ¦
¦         ¦                                  ¦               ¦
¦         ¦                                  ¦               ¦
¦         ¦      annotate                    ¦               ¦
¦         ¦      here OK                     ¦               ¦
¦         ¦                                  ¦               ¦
¦         ¦                                  ¦               ¦
¦         ¦                                  ¦               ¦
¦         ¦            PLOT                  ¦               ¦
¦         +----------------------------------+               ¦
¦                                                            ¦
¦                                                            ¦
¦                     CHART                                  ¦
+------------------------------------------------------------+

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 » Fri Jun 04, 2004 10:15 am

It isn't supported. You'll need to add something to the draw() method in the JFreeChart class.
David Gilbert
JFreeChart Project Leader

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

oscar
Posts: 26
Joined: Fri May 28, 2004 10:01 am
Location: Switzerland

Post by oscar » Fri Jun 04, 2004 10:29 am

david.gilbert wrote:It isn't supported. You'll need to add something to the draw() method in the JFreeChart class.
I feared as much. I'm actually having a look at that now - when I get something to work, I'll post it in case you might want it for future releases...

BTW, did you have a chance to look at the "Gridlines overwrite domain and range axes" snagette (http://www.jfree.org/phpBB2/viewtopic.php?t=8949)?

oscar
Posts: 26
Joined: Fri May 28, 2004 10:01 am
Location: Switzerland

Post by oscar » Fri Jun 04, 2004 3:58 pm

Here is a fairly rough and ready fix that allows you to print a line of text anywhere in the chart (except in the area used by the plot - use an annotation for that). It involves a couple of extra methods in JFreeChart.java and one new class in org.jfree.chart.title. It's based on the methodology for implementing subtitles in the chart.

1) Patch to src/org/jfree/chart/JFreeChart.java

Code: Select all


146a147,149
> import org.jfree.chart.title.Note;
> import org.jfree.text.TextBlock;
> import org.jfree.text.TextLine;
206a210,212
>     /** The chart notes (zero, one or many). */
>     private List notes;
> 
300a307
>         this.notes = new ArrayList();
457a465,473
>      * Returns the list of notes.
>      *
>      * @return the note list.
>      */
>     public List getNotes() {
>         return this.notes;
>     }
> 
>     /**
512a529,539
>      * Adds a chart note.
>      *
>      * @param note the note.
>      */
>     public void addNote(Note note) {
>         if (note != null) {
>             this.notes.add(note);
>         }
>     }
> 
>     /**
879a907,913
>       // draw any notes
>         Iterator noteIterator = this.notes.iterator();
>         while (noteIterator.hasNext()) {
>             Note currentNote = (Note) noteIterator.next();
>             drawNote(currentNote, g2, nonTitleArea);
>         }
> 
983a1018,1032
>     /**
>      * Draws a note. A note is an arbitrary piece of text which can be
>      * placed anywhere in the chart area. The note contains the text
>      * to be written, the font and color to use and its x,y
>      * coordinates.
>      *
>      * @param note  the note to place (<code>null</code> not permitted).
>      * @param g2  the graphics device (<code>null</code> not permitted).
>      * @param area the chart area, excluding any existing titles (<code>null</code> not permitted).  */
>     public void drawNote(Note note,  Graphics2D g2, Rectangle2D area) {
>       TextBlock tb = new TextBlock();
>       tb.addLine(new TextLine(note.text, note.font, note.paint));
>       tb.draw(g2, note.xCoord, note.yCoord, null);
>     }
> 
2) Add new class, Note.java to src/org/jfree/chart/title/

Code: Select all

package org.jfree.chart.title;

import java.awt.Font;
import java.awt.Paint;

/** 
 * An arbitrary piece of text which can be placed anywhere in the
 * chart area. The coordinates are given in pixels with (0,0) at
 * the top-left of the chart. Note that the plot area overwrites any
 * notes so if you need text inside the plot, use an Annotation
 * instead.
 */
public class Note {

    /** The text to be printed in the note */
    public String text;

    /** The font to be used for the note */
    public Font font;
    
    /** The paint (ie, color) to be used for the note */
    public Paint paint;
    
    /** The x-coordinate of the note (in pixels) */
    public int xCoord;

    /** The y-coordinate of the note (in pixels) */
    public int yCoord;

    /**
     * Constructs a Note - A note is an arbitrary piece of text which
     * can be placed anywhere in the chart area.
     */
    public Note (String text, Font font, Paint paint, int xCoord, int yCoord) {
        this.text = text;
        this.font = font;
        this.paint = paint;
        this.xCoord = xCoord;
        this.yCoord = yCoord;
    }
}
3) To demo; add this patch to src/org/jfree/chart/demo/TimeSeriesDemo.java

Code: Select all

> import java.awt.Font;
> import org.jfree.chart.title.Note;

106a112,113
>       chart.addNote(new Note("Banana", new Font("Helvetica", Font.PLAIN, 10), Color.black, 5, 5));
Rgds,
Oscar

oscar
Posts: 26
Joined: Fri May 28, 2004 10:01 am
Location: Switzerland

Patch updated to version 1.0.5

Post by oscar » Fri Apr 27, 2007 2:16 pm

I updated my patch to provide this ability (see https://sourceforge.net/tracker/index.p ... tid=315494)

Note.java is now cloneable and serializable...

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 » Mon Apr 30, 2007 10:41 am

Thanks! That's definitely along the right lines. It might even be possible to reuse all the title/subtitle code (so that we can support text, images, etc) by allowing a new title position "FLOATING". I've already done some experimentation with that inside the plot (XYTitleAnnotation), so I should try this outside the plot as well.
David Gilbert
JFreeChart Project Leader

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

jfreeuser2006
Posts: 59
Joined: Mon Nov 20, 2006 1:00 pm

Post by jfreeuser2006 » Wed May 02, 2007 8:19 am

Will this feature be included in the next release for Annotations to the Category Plot?

bztom33
Posts: 35
Joined: Tue Jul 11, 2006 10:35 pm

Post by bztom33 » Wed May 09, 2007 6:10 pm

I am very much like to use it in my chart, but I don't know how to rebuild Jfreechart with ant. Is there a precompiled version out there that has already included the Note feature?

oscar
Posts: 26
Joined: Fri May 28, 2004 10:01 am
Location: Switzerland

Post by oscar » Thu May 10, 2007 7:42 am

> bztom33

If you e-mail me I'll send you the JAR...

(http://www.jfree.org/phpBB2/profile.php ... ile&u=2140)

Rgds,
Oscar

bztom33
Posts: 35
Joined: Tue Jul 11, 2006 10:35 pm

Post by bztom33 » Mon May 21, 2007 6:28 pm

Will this feature be added to the newer version of JFreeChart?

oscar
Posts: 26
Joined: Fri May 28, 2004 10:01 am
Location: Switzerland

Calling Insaf Fradi...

Post by oscar » Fri Aug 08, 2008 9:44 am

Calling Insaf Fradi...

You e-mailed me asking for the jar file with the Note class but somehow your return-path is not working (it is fradi@exchange.robeco).

If you'd like to get back to me with a working e-mail address, I'd be happy to send on the jar.

Locked