CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jfree--jfreechart

JFreeChart is a comprehensive free chart library for the Java platform that enables developers to create professional-quality charts for both client-side and server-side applications with export capabilities to multiple formats including PNG and JPEG.

Pending
Overview
Eval results
Files

labels-tooltips-legends.mddocs/

Labels, Tooltips, and Legends

Label generation, tooltip creation, and legend management for enhancing chart interactivity and readability. These components provide essential information display and user interaction capabilities that make charts more informative and user-friendly.

Capabilities

Label Generators

Interfaces and implementations for generating text labels for chart data items.

/**
 * Interface for generating labels for category items
 */
public interface CategoryItemLabelGenerator {
    /**
     * Generates a label for a specific data item
     * @param dataset the dataset
     * @param row the row index (zero-based)
     * @param column the column index (zero-based)
     * @return the item label (possibly null)
     */
    public String generateLabel(CategoryDataset dataset, int row, int column);
    
    /**
     * Generates a label for a data row (series)
     * @param dataset the dataset
     * @param row the row index (zero-based)
     * @return the row label (possibly null)
     */
    public String generateRowLabel(CategoryDataset dataset, int row);
    
    /**
     * Generates a label for a data column (category)
     * @param dataset the dataset
     * @param column the column index (zero-based)
     * @return the column label (possibly null)
     */
    public String generateColumnLabel(CategoryDataset dataset, int column);
}

/**
 * Standard implementation of CategoryItemLabelGenerator
 */
public class StandardCategoryItemLabelGenerator implements CategoryItemLabelGenerator, Cloneable, PublicCloneable, Serializable {
    // Default format string showing the value
    public static final String DEFAULT_LABEL_FORMAT_STRING = "{2}";
    
    /**
     * Creates a generator with default format
     */
    public StandardCategoryItemLabelGenerator();
    
    /**
     * Creates a generator with custom format
     * @param labelFormat the label format string (uses MessageFormat)
     * @param formatter the number formatter
     */
    public StandardCategoryItemLabelGenerator(String labelFormat, NumberFormat formatter);
    
    /**
     * Creates a generator with separate number and date formatters
     * @param labelFormat the label format string
     * @param formatter the number formatter
     * @param percentFormatter the percentage formatter
     */
    public StandardCategoryItemLabelGenerator(String labelFormat, NumberFormat formatter, NumberFormat percentFormatter);
    
    public String getLabelFormat();
    public void setLabelFormat(String format);
    public NumberFormat getNumberFormat();
    public void setNumberFormat(NumberFormat formatter);
    public NumberFormat getPercentFormat();
    public void setPercentFormat(NumberFormat formatter);
    
    public String generateLabel(CategoryDataset dataset, int row, int column);
    public String generateRowLabel(CategoryDataset dataset, int row);
    public String generateColumnLabel(CategoryDataset dataset, int column);
}

/**
 * Interface for generating labels for XY items
 */
public interface XYItemLabelGenerator {
    /**
     * Generates a label for a data item
     * @param dataset the dataset
     * @param series the series index (zero-based)
     * @param item the item index (zero-based)
     * @return the item label (possibly null)
     */
    public String generateLabel(XYDataset dataset, int series, int item);
}

/**
 * Standard implementation of XYItemLabelGenerator
 */
public class StandardXYItemLabelGenerator implements XYItemLabelGenerator, Cloneable, PublicCloneable, Serializable {
    // Default format showing X and Y values
    public static final String DEFAULT_ITEM_LABEL_FORMAT = "{0}: ({1}, {2})";
    
    /**
     * Creates a generator with default format
     */
    public StandardXYItemLabelGenerator();
    
    /**
     * Creates a generator with custom format
     * @param formatString the format string
     * @param xFormat the X value formatter
     * @param yFormat the Y value formatter
     */
    public StandardXYItemLabelGenerator(String formatString, NumberFormat xFormat, NumberFormat yFormat);
    
    /**
     * Creates a generator with date formatting for X values
     * @param formatString the format string
     * @param xFormat the X value date formatter
     * @param yFormat the Y value number formatter
     */
    public StandardXYItemLabelGenerator(String formatString, DateFormat xFormat, NumberFormat yFormat);
    
    public String getFormatString();
    public void setFormatString(String format);
    public NumberFormat getXFormat();
    public void setXFormat(NumberFormat format);
    public NumberFormat getYFormat();
    public void setYFormat(NumberFormat format);
    public DateFormat getXDateFormat();
    public void setXDateFormat(DateFormat format);
    
    public String generateLabel(XYDataset dataset, int series, int item);
}

/**
 * Interface for generating labels for pie sections
 */
public interface PieSectionLabelGenerator {
    /**
     * Generates a label for a pie section
     * @param dataset the dataset
     * @param key the section key
     * @return the section label (possibly null)
     */
    public String generateSectionLabel(PieDataset dataset, Comparable key);
    
    /**
     * Generates an attributed string for a pie section (for advanced formatting)
     * @param dataset the dataset
     * @param key the section key
     * @return the attributed section label (possibly null)
     */
    public AttributedString generateAttributedSectionLabel(PieDataset dataset, Comparable key);
}

/**
 * Standard implementation of PieSectionLabelGenerator
 */
public class StandardPieSectionLabelGenerator implements PieSectionLabelGenerator, Cloneable, PublicCloneable, Serializable {
    // Default format showing key and percentage
    public static final String DEFAULT_SECTION_LABEL_FORMAT = "{0} = {1} ({2})";
    
    /**
     * Creates a generator with default format
     */
    public StandardPieSectionLabelGenerator();
    
    /**
     * Creates a generator with custom format
     * @param labelFormat the label format string
     * @param numberFormat the number formatter
     * @param percentFormat the percentage formatter
     */
    public StandardPieSectionLabelGenerator(String labelFormat, NumberFormat numberFormat, NumberFormat percentFormat);
    
    /**
     * Creates a generator with locale-specific formatting
     * @param locale the locale for default formatting
     */
    public StandardPieSectionLabelGenerator(Locale locale);
    
    public String getLabelFormat();
    public void setLabelFormat(String format);
    public NumberFormat getNumberFormat();
    public void setNumberFormat(NumberFormat formatter);
    public NumberFormat getPercentFormat();
    public void setPercentFormat(NumberFormat formatter);
    
    public String generateSectionLabel(PieDataset dataset, Comparable key);
    public AttributedString generateAttributedSectionLabel(PieDataset dataset, Comparable key);
}

Usage Example:

import org.jfree.chart.labels.*;
import java.text.DecimalFormat;

// Category item label generator
StandardCategoryItemLabelGenerator categoryGenerator = 
    new StandardCategoryItemLabelGenerator(
        "{1}: {2}",  // Format: Category: Value
        new DecimalFormat("#,##0.00")  // Number format
    );

BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDefaultItemLabelGenerator(categoryGenerator);
renderer.setDefaultItemLabelsVisible(true);

// XY item label generator  
StandardXYItemLabelGenerator xyGenerator = 
    new StandardXYItemLabelGenerator(
        "({1}, {2})",  // Format: (X, Y)
        new DecimalFormat("#.##"),   // X format
        new DecimalFormat("#.##")    // Y format
    );

XYLineAndShapeRenderer xyRenderer = (XYLineAndShapeRenderer) xyPlot.getRenderer();
xyRenderer.setDefaultItemLabelGenerator(xyGenerator);
xyRenderer.setDefaultItemLabelsVisible(true);

// Pie section label generator
StandardPieSectionLabelGenerator pieGenerator = 
    new StandardPieSectionLabelGenerator(
        "{0}: {2}",  // Format: Key: Percentage
        new DecimalFormat("#,##0"),      // Number format
        new DecimalFormat("#.#%")        // Percentage format
    );

PiePlot piePlot = (PiePlot) chart.getPlot();
piePlot.setLabelGenerator(pieGenerator);

Tooltip Generators

Interfaces and implementations for generating interactive tooltips.

/**
 * Interface for generating tooltips for category items
 */
public interface CategoryToolTipGenerator {
    /**
     * Generates a tooltip for a data item
     * @param dataset the dataset
     * @param row the row index (zero-based)
     * @param column the column index (zero-based)
     * @return the tooltip text (possibly null)
     */
    public String generateToolTip(CategoryDataset dataset, int row, int column);
}

/**
 * Standard implementation of CategoryToolTipGenerator
 */
public class StandardCategoryToolTipGenerator implements CategoryToolTipGenerator, Cloneable, PublicCloneable, Serializable {
    // Default tooltip format
    public static final String DEFAULT_TOOL_TIP_FORMAT_STRING = "({0}, {1}) = {2}";
    
    /**
     * Creates a generator with default format
     */
    public StandardCategoryToolTipGenerator();
    
    /**
     * Creates a generator with custom format
     * @param labelFormat the tooltip format string
     * @param formatter the number formatter
     */
    public StandardCategoryToolTipGenerator(String labelFormat, NumberFormat formatter);
    
    public String getLabelFormat();
    public void setLabelFormat(String format);
    public NumberFormat getNumberFormat();
    public void setNumberFormat(NumberFormat formatter);
    
    public String generateToolTip(CategoryDataset dataset, int row, int column);
}

/**
 * Interface for generating tooltips for XY items
 */
public interface XYToolTipGenerator {
    /**
     * Generates a tooltip for a data item
     * @param dataset the dataset
     * @param series the series index (zero-based)
     * @param item the item index (zero-based)
     * @return the tooltip text (possibly null)
     */
    public String generateToolTip(XYDataset dataset, int series, int item);
}

/**
 * Standard implementation of XYToolTipGenerator
 */
public class StandardXYToolTipGenerator implements XYToolTipGenerator, Cloneable, PublicCloneable, Serializable {
    // Default tooltip format
    public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2})";
    
    /**
     * Creates a generator with default format
     */
    public StandardXYToolTipGenerator();
    
    /**
     * Creates a generator with custom format
     * @param formatString the format string
     * @param xFormat the X value formatter
     * @param yFormat the Y value formatter
     */
    public StandardXYToolTipGenerator(String formatString, NumberFormat xFormat, NumberFormat yFormat);
    
    /**
     * Creates a generator with date formatting for X values
     * @param formatString the format string
     * @param xFormat the X value date formatter
     * @param yFormat the Y value number formatter
     */
    public StandardXYToolTipGenerator(String formatString, DateFormat xFormat, NumberFormat yFormat);
    
    public String generateToolTip(XYDataset dataset, int series, int item);
}

/**
 * Interface for generating tooltips for pie sections
 */
public interface PieToolTipGenerator {
    /**
     * Generates a tooltip for a pie section
     * @param dataset the dataset
     * @param key the section key
     * @return the tooltip text (possibly null)
     */
    public String generateToolTip(PieDataset dataset, Comparable key);
}

/**
 * Standard implementation of PieToolTipGenerator
 */
public class StandardPieToolTipGenerator implements PieToolTipGenerator, Cloneable, PublicCloneable, Serializable {
    // Default tooltip format
    public static final String DEFAULT_TOOLTIP_FORMAT = "{0} = {1} ({2})";
    
    /**
     * Creates a generator with default format
     */
    public StandardPieToolTipGenerator();
    
    /**
     * Creates a generator with custom format
     * @param labelFormat the tooltip format string
     * @param numberFormat the number formatter
     * @param percentFormat the percentage formatter
     */
    public StandardPieToolTipGenerator(String labelFormat, NumberFormat numberFormat, NumberFormat percentFormat);
    
    /**
     * Creates a generator with locale-specific formatting
     * @param locale the locale for default formatting
     */
    public StandardPieToolTipGenerator(Locale locale);
    
    public String generateToolTip(PieDataset dataset, Comparable key);
}

/**
 * Specialized tooltip generators for specific chart types
 */

// Box and whisker tooltip generator
public class BoxAndWhiskerToolTipGenerator implements CategoryToolTipGenerator {
    public BoxAndWhiskerToolTipGenerator();
    public String generateToolTip(CategoryDataset dataset, int row, int column);
}

// XY box and whisker tooltip generator
public class BoxAndWhiskerXYToolTipGenerator implements XYToolTipGenerator {
    public BoxAndWhiskerXYToolTipGenerator();
    public String generateToolTip(XYDataset dataset, int series, int item);
}

// High-low-open-close tooltip generator
public class HighLowItemLabelGenerator implements XYItemLabelGenerator, XYToolTipGenerator {
    public static final String DEFAULT_TOOL_TIP_FORMAT = "Date: {1}; High: {2}; Low: {3}; Open: {4}; Close: {5}; Volume: {6}";
    public static final String DEFAULT_LABEL_FORMAT = "{5}";
    
    public HighLowItemLabelGenerator();
    public HighLowItemLabelGenerator(String toolTipFormat, DateFormat dateFormatter);
    
    public String generateLabel(XYDataset dataset, int series, int item);
    public String generateToolTip(XYDataset dataset, int series, int item);
}

// Bubble chart tooltip generator
public class BubbleXYItemLabelGenerator extends StandardXYItemLabelGenerator {
    public static final String DEFAULT_FORMAT_STRING = "({1}, {2}, {3})";
    
    public BubbleXYItemLabelGenerator();
    public BubbleXYItemLabelGenerator(String formatString, NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat);
}

// 3D XYZ tooltip generator  
public class StandardXYZToolTipGenerator extends StandardXYToolTipGenerator {
    public static final String DEFAULT_TOOL_TIP_FORMAT = "{0}: ({1}, {2}, {3})";
    
    public StandardXYZToolTipGenerator();
    public StandardXYZToolTipGenerator(String formatString, NumberFormat xFormat, NumberFormat yFormat, NumberFormat zFormat);
}

Usage Example:

import org.jfree.chart.labels.*;
import java.text.SimpleDateFormat;

// Category tooltip generator
StandardCategoryToolTipGenerator categoryTooltip = 
    new StandardCategoryToolTipGenerator(
        "Series: {0}, Category: {1}, Value: {2}",
        new DecimalFormat("#,##0.00")
    );

renderer.setDefaultToolTipGenerator(categoryTooltip);

// Time series tooltip generator
StandardXYToolTipGenerator timeTooltip = 
    new StandardXYToolTipGenerator(
        "{0}: {1} = {2}",
        new SimpleDateFormat("MMM dd, yyyy"),  // Date format for X
        new DecimalFormat("#,##0.00")          // Number format for Y
    );

xyRenderer.setDefaultToolTipGenerator(timeTooltip);

// Financial data tooltip generator
HighLowItemLabelGenerator financialTooltip = new HighLowItemLabelGenerator(
    "Date: {1}; Open: {4}; High: {2}; Low: {3}; Close: {5}",
    new SimpleDateFormat("yyyy-MM-dd")
);

candlestickRenderer.setDefaultToolTipGenerator(financialTooltip);

// Bubble chart tooltip generator
BubbleXYItemLabelGenerator bubbleTooltip = new BubbleXYItemLabelGenerator(
    "X: {1}, Y: {2}, Size: {3}",
    new DecimalFormat("#.##"),   // X format
    new DecimalFormat("#.##"),   // Y format  
    new DecimalFormat("#.##")    // Z format
);

bubbleRenderer.setDefaultToolTipGenerator(bubbleTooltip);

Item Label Positioning

Classes for controlling the position and orientation of item labels.

/**
 * Defines the position of an item label relative to a data item
 */
public class ItemLabelPosition implements Serializable {
    /**
     * Creates an item label position
     * @param itemLabelAnchor the item label anchor point
     * @param textAnchor the text anchor point
     * @param rotationAnchor the rotation anchor point
     * @param angle the rotation angle (in radians)
     */
    public ItemLabelPosition(ItemLabelAnchor itemLabelAnchor, TextAnchor textAnchor, TextAnchor rotationAnchor, double angle);
    
    public ItemLabelAnchor getItemLabelAnchor();
    public TextAnchor getTextAnchor();
    public TextAnchor getRotationAnchor();
    public double getAngle();
}

/**
 * Anchor points for item labels relative to data items
 */
public final class ItemLabelAnchor implements Serializable {
    // Inside positions (within the data item area)
    public static final ItemLabelAnchor CENTER = new ItemLabelAnchor("CENTER");
    public static final ItemLabelAnchor INSIDE1 = new ItemLabelAnchor("INSIDE1");
    public static final ItemLabelAnchor INSIDE2 = new ItemLabelAnchor("INSIDE2");
    public static final ItemLabelAnchor INSIDE3 = new ItemLabelAnchor("INSIDE3");
    public static final ItemLabelAnchor INSIDE4 = new ItemLabelAnchor("INSIDE4");
    public static final ItemLabelAnchor INSIDE5 = new ItemLabelAnchor("INSIDE5");
    public static final ItemLabelAnchor INSIDE6 = new ItemLabelAnchor("INSIDE6");
    public static final ItemLabelAnchor INSIDE7 = new ItemLabelAnchor("INSIDE7");
    public static final ItemLabelAnchor INSIDE8 = new ItemLabelAnchor("INSIDE8");
    public static final ItemLabelAnchor INSIDE9 = new ItemLabelAnchor("INSIDE9");
    public static final ItemLabelAnchor INSIDE10 = new ItemLabelAnchor("INSIDE10");
    public static final ItemLabelAnchor INSIDE11 = new ItemLabelAnchor("INSIDE11");
    public static final ItemLabelAnchor INSIDE12 = new ItemLabelAnchor("INSIDE12");
    
    // Outside positions (outside the data item area)
    public static final ItemLabelAnchor OUTSIDE1 = new ItemLabelAnchor("OUTSIDE1");
    public static final ItemLabelAnchor OUTSIDE2 = new ItemLabelAnchor("OUTSIDE2");
    public static final ItemLabelAnchor OUTSIDE3 = new ItemLabelAnchor("OUTSIDE3");
    public static final ItemLabelAnchor OUTSIDE4 = new ItemLabelAnchor("OUTSIDE4");
    public static final ItemLabelAnchor OUTSIDE5 = new ItemLabelAnchor("OUTSIDE5");
    public static final ItemLabelAnchor OUTSIDE6 = new ItemLabelAnchor("OUTSIDE6");
    public static final ItemLabelAnchor OUTSIDE7 = new ItemLabelAnchor("OUTSIDE7");
    public static final ItemLabelAnchor OUTSIDE8 = new ItemLabelAnchor("OUTSIDE8");
    public static final ItemLabelAnchor OUTSIDE9 = new ItemLabelAnchor("OUTSIDE9");
    public static final ItemLabelAnchor OUTSIDE10 = new ItemLabelAnchor("OUTSIDE10");
    public static final ItemLabelAnchor OUTSIDE11 = new ItemLabelAnchor("OUTSIDE11");
    public static final ItemLabelAnchor OUTSIDE12 = new ItemLabelAnchor("OUTSIDE12");
    
    public String toString();
}

/**
 * Text anchor points for label text positioning
 */
public final class TextAnchor implements Serializable {
    public static final TextAnchor TOP_LEFT = new TextAnchor("TOP_LEFT");
    public static final TextAnchor TOP_CENTER = new TextAnchor("TOP_CENTER");
    public static final TextAnchor TOP_RIGHT = new TextAnchor("TOP_RIGHT");
    public static final TextAnchor HALF_ASCENT_LEFT = new TextAnchor("HALF_ASCENT_LEFT");
    public static final TextAnchor HALF_ASCENT_CENTER = new TextAnchor("HALF_ASCENT_CENTER");
    public static final TextAnchor HALF_ASCENT_RIGHT = new TextAnchor("HALF_ASCENT_RIGHT");
    public static final TextAnchor CENTER_LEFT = new TextAnchor("CENTER_LEFT");
    public static final TextAnchor CENTER = new TextAnchor("CENTER");
    public static final TextAnchor CENTER_RIGHT = new TextAnchor("CENTER_RIGHT");
    public static final TextAnchor BASELINE_LEFT = new TextAnchor("BASELINE_LEFT");
    public static final TextAnchor BASELINE_CENTER = new TextAnchor("BASELINE_CENTER");
    public static final TextAnchor BASELINE_RIGHT = new TextAnchor("BASELINE_RIGHT");
    public static final TextAnchor BOTTOM_LEFT = new TextAnchor("BOTTOM_LEFT");
    public static final TextAnchor BOTTOM_CENTER = new TextAnchor("BOTTOM_CENTER");
    public static final TextAnchor BOTTOM_RIGHT = new TextAnchor("BOTTOM_RIGHT");
    
    public String toString();
}

/**
 * Category label positioning for category axes
 */
public class CategoryLabelPosition implements Serializable {
    /**
     * Creates a category label position
     * @param categoryAnchor the category anchor
     * @param labelAnchor the label anchor
     * @param rotationAnchor the rotation anchor
     * @param angle the rotation angle
     */
    public CategoryLabelPosition(RectangleAnchor categoryAnchor, TextAnchor labelAnchor, TextAnchor rotationAnchor, double angle);
    
    public RectangleAnchor getCategoryAnchor();
    public TextAnchor getLabelAnchor();  
    public TextAnchor getRotationAnchor();
    public double getAngle();
}

/**
 * A set of category label positions for different orientations
 */
public class CategoryLabelPositions implements Serializable {
    // Standard positions
    public static final CategoryLabelPositions STANDARD = new CategoryLabelPositions(
        new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextAnchor.BOTTOM_CENTER, TextAnchor.BOTTOM_CENTER, 0.0),
        new CategoryLabelPosition(RectangleAnchor.TOP, TextAnchor.TOP_CENTER, TextAnchor.TOP_CENTER, 0.0),
        new CategoryLabelPosition(RectangleAnchor.RIGHT, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, 0.0),
        new CategoryLabelPosition(RectangleAnchor.LEFT, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, 0.0)
    );
    
    // Up 45 degree rotation
    public static final CategoryLabelPositions UP_45 = new CategoryLabelPositions(
        new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextAnchor.BOTTOM_LEFT, TextAnchor.BOTTOM_LEFT, -Math.PI / 4.0),
        new CategoryLabelPosition(RectangleAnchor.TOP, TextAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, -Math.PI / 4.0),
        new CategoryLabelPosition(RectangleAnchor.RIGHT, TextAnchor.BOTTOM_RIGHT, TextAnchor.BOTTOM_RIGHT, -Math.PI / 4.0),
        new CategoryLabelPosition(RectangleAnchor.LEFT, TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, -Math.PI / 4.0)
    );
    
    // Down 45 degree rotation
    public static final CategoryLabelPositions DOWN_45 = new CategoryLabelPositions(
        new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextAnchor.BOTTOM_RIGHT, TextAnchor.BOTTOM_RIGHT, Math.PI / 4.0),
        new CategoryLabelPosition(RectangleAnchor.TOP, TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, Math.PI / 4.0),
        new CategoryLabelPosition(RectangleAnchor.RIGHT, TextAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT, Math.PI / 4.0),
        new CategoryLabelPosition(RectangleAnchor.LEFT, TextAnchor.BOTTOM_LEFT, TextAnchor.BOTTOM_LEFT, Math.PI / 4.0)
    );
    
    // Up 90 degree rotation (vertical)
    public static final CategoryLabelPositions UP_90 = new CategoryLabelPositions(
        new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, -Math.PI / 2.0),
        new CategoryLabelPosition(RectangleAnchor.TOP, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, -Math.PI / 2.0),
        new CategoryLabelPosition(RectangleAnchor.RIGHT, TextAnchor.BOTTOM_CENTER, TextAnchor.BOTTOM_CENTER, -Math.PI / 2.0),
        new CategoryLabelPosition(RectangleAnchor.LEFT, TextAnchor.TOP_CENTER, TextAnchor.TOP_CENTER, -Math.PI / 2.0)
    );
    
    // Down 90 degree rotation
    public static final CategoryLabelPositions DOWN_90 = new CategoryLabelPositions(
        new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, Math.PI / 2.0),
        new CategoryLabelPosition(RectangleAnchor.TOP, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, Math.PI / 2.0),
        new CategoryLabelPosition(RectangleAnchor.RIGHT, TextAnchor.TOP_CENTER, TextAnchor.TOP_CENTER, Math.PI / 2.0),
        new CategoryLabelPosition(RectangleAnchor.LEFT, TextAnchor.BOTTOM_CENTER, TextAnchor.BOTTOM_CENTER, Math.PI / 2.0)
    );
    
    /**
     * Creates category label positions
     * @param top position for top edge
     * @param bottom position for bottom edge
     * @param left position for left edge
     * @param right position for right edge
     */
    public CategoryLabelPositions(CategoryLabelPosition top, CategoryLabelPosition bottom, CategoryLabelPosition left, CategoryLabelPosition right);
    
    public CategoryLabelPosition getLabelPosition(RectangleEdge edge);
}

Usage Example:

import org.jfree.chart.labels.*;

// Configure item label positioning for bar chart
BarRenderer renderer = (BarRenderer) plot.getRenderer();

// Position labels outside bars at the top
ItemLabelPosition positive = new ItemLabelPosition(
    ItemLabelAnchor.OUTSIDE12,  // Above the bar
    TextAnchor.BOTTOM_CENTER,   // Anchor text at bottom center
    TextAnchor.BOTTOM_CENTER,   // No rotation anchor needed
    0.0                         // No rotation
);

// Position labels inside bars for negative values
ItemLabelPosition negative = new ItemLabelPosition(
    ItemLabelAnchor.CENTER,     // Center of bar
    TextAnchor.CENTER,          // Center text
    TextAnchor.CENTER,          // No rotation anchor needed
    0.0                         // No rotation
);

renderer.setDefaultPositiveItemLabelPosition(positive);
renderer.setDefaultNegativeItemLabelPosition(negative);
renderer.setDefaultItemLabelsVisible(true);

// Configure category axis labels with rotation
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

// Custom category label positions
CategoryLabelPositions customPositions = new CategoryLabelPositions(
    new CategoryLabelPosition(RectangleAnchor.BOTTOM, TextAnchor.BOTTOM_RIGHT, TextAnchor.BOTTOM_RIGHT, Math.PI / 6.0),  // 30 degrees
    new CategoryLabelPosition(RectangleAnchor.TOP, TextAnchor.TOP_LEFT, TextAnchor.TOP_LEFT, -Math.PI / 6.0),
    new CategoryLabelPosition(RectangleAnchor.RIGHT, TextAnchor.CENTER_RIGHT, TextAnchor.CENTER_RIGHT, 0.0),
    new CategoryLabelPosition(RectangleAnchor.LEFT, TextAnchor.CENTER_LEFT, TextAnchor.CENTER_LEFT, 0.0)
);
categoryAxis.setCategoryLabelPositions(customPositions);

Legend Management

Classes for creating and customizing chart legends.

/**
 * A chart title that displays a legend
 */
public class LegendTitle extends Title implements LegendItemSource {
    /**
     * Creates a legend title with a single source
     * @param source the legend item source
     */
    public LegendTitle(LegendItemSource source);
    
    /**
     * Creates a legend title with custom arrangement
     * @param source the legend item source
     * @param hLayout the horizontal arrangement
     * @param vLayout the vertical arrangement
     */
    public LegendTitle(LegendItemSource source, Arrangement hLayout, Arrangement vLayout);
    
    // Sources
    public LegendItemSource[] getSources();
    public void setSources(LegendItemSource[] sources);
    
    // Item arrangement
    public Arrangement getItemArrangement();
    public void setItemArrangement(Arrangement arrangement);
    
    // Item appearance
    public Paint getItemPaint();
    public void setItemPaint(Paint paint);
    public Font getItemFont();
    public void setItemFont(Font font);
    public double getItemLabelPadding();
    public void setItemLabelPadding(double padding);
    
    // Graphic properties
    public RectangleAnchor getLegendItemGraphicAnchor();
    public void setLegendItemGraphicAnchor(RectangleAnchor anchor);
    public RectangleEdge getLegendItemGraphicEdge();
    public void setLegendItemGraphicEdge(RectangleEdge edge);
    public RectangleAnchor getLegendItemGraphicLocation();
    public void setLegendItemGraphicLocation(RectangleAnchor anchor);
    public RectangleInsets getLegendItemGraphicPadding();
    public void setLegendItemGraphicPadding(RectangleInsets padding);
    
    // Sorting
    public SortOrder getSortOrder();
    public void setSortOrder(SortOrder order);
    
    // Background
    public Paint getBackgroundPaint();
    public void setBackgroundPaint(Paint paint);
    public RectangleInsets getItemContainer();
    public void setItemContainer(RectangleInsets container);
}

/**
 * Interface for objects that can provide legend items
 */
public interface LegendItemSource {
    /**
     * Returns the legend items for this source
     * @return the legend items
     */
    public LegendItemCollection getLegendItems();
}

/**
 * A single item in a legend
 */
public class LegendItem implements Cloneable, Serializable {
    /**
     * Creates a simple legend item
     * @param label the label
     */
    public LegendItem(String label);
    
    /**
     * Creates a legend item with color
     * @param label the label
     * @param paint the color
     */
    public LegendItem(String label, Paint paint);
    
    /**
     * Creates a legend item with full customization
     * @param label the label
     * @param description the description
     * @param toolTipText the tooltip text
     * @param urlText the URL text
     * @param shape the shape to display
     * @param fillPaint the fill paint
     */
    public LegendItem(String label, String description, String toolTipText, String urlText, Shape shape, Paint fillPaint);
    
    /**
     * Creates a legend item with shape and line
     * @param label the label
     * @param description the description
     * @param toolTipText the tooltip text  
     * @param urlText the URL text
     * @param shapeVisible flag to show shape
     * @param shape the shape
     * @param shapeFilled flag to fill shape
     * @param fillPaint the fill paint
     * @param shapeOutlineVisible flag to show shape outline
     * @param outlinePaint the outline paint
     * @param outlineStroke the outline stroke
     * @param lineVisible flag to show line
     * @param line the line shape
     * @param lineStroke the line stroke
     * @param linePaint the line paint
     */
    public LegendItem(String label, String description, String toolTipText, String urlText, boolean shapeVisible, Shape shape, boolean shapeFilled, Paint fillPaint, boolean shapeOutlineVisible, Paint outlinePaint, Stroke outlineStroke, boolean lineVisible, Shape line, Stroke lineStroke, Paint linePaint);
    
    // Label properties
    public String getLabel();
    public void setLabel(String label);
    public String getDescription();
    public void setDescription(String description);
    public String getToolTipText();
    public void setToolTipText(String text);
    public String getURLText();
    public void setURLText(String text);
    
    // Shape properties
    public boolean isShapeVisible();
    public void setShapeVisible(boolean visible);
    public Shape getShape();
    public void setShape(Shape shape);
    public boolean isShapeFilled();
    public void setShapeFilled(boolean filled);
    public Paint getFillPaint();
    public void setFillPaint(Paint paint);
    public boolean isShapeOutlineVisible();
    public void setShapeOutlineVisible(boolean visible);
    public Paint getOutlinePaint();
    public void setOutlinePaint(Paint paint);
    public Stroke getOutlineStroke();
    public void setOutlineStroke(Stroke stroke);
    
    // Line properties
    public boolean isLineVisible();
    public void setLineVisible(boolean visible);
    public Shape getLine();
    public void setLine(Shape line);
    public Stroke getLineStroke();
    public void setLineStroke(Stroke stroke);
    public Paint getLinePaint();
    public void setLinePaint(Paint paint);
    
    // Dataset and series information
    public Dataset getDataset();
    public void setDataset(Dataset dataset);
    public int getDatasetIndex();
    public void setDatasetIndex(int index);
    public Comparable getSeriesKey();
    public void setSeriesKey(Comparable key);
    public int getSeriesIndex();
    public void setSeriesIndex(int index);
}

/**
 * A collection of legend items
 */
public class LegendItemCollection implements Cloneable, Serializable {
    /**
     * Creates an empty legend item collection
     */
    public LegendItemCollection();
    
    public void add(LegendItem item);
    public void addAll(LegendItemCollection collection);
    public LegendItem get(int index);
    public int getItemCount();
    public Iterator iterator();
    public void clear();
}

/**
 * Specialized legends for paint scales
 */
public class PaintScaleLegend extends Title implements LegendItemSource {
    /**
     * Creates a paint scale legend
     * @param scale the paint scale
     * @param axis the axis for the legend
     */
    public PaintScaleLegend(PaintScale scale, ValueAxis axis);
    
    public PaintScale getScale();
    public void setScale(PaintScale scale);
    public ValueAxis getAxis();
    public void setAxis(ValueAxis axis);
    public AxisLocation getAxisLocation();
    public void setAxisLocation(AxisLocation location);
    public double getAxisOffset();
    public void setAxisOffset(double offset);
    public double getStripWidth();
    public void setStripWidth(double width);
    public boolean isStripOutlineVisible();
    public void setStripOutlineVisible(boolean visible);
    public Paint getStripOutlinePaint();
    public void setStripOutlinePaint(Paint paint);
    public Stroke getStripOutlineStroke();
    public void setStripOutlineStroke(Stroke stroke);
    public Paint getBackgroundPaint();
    public void setBackgroundPaint(Paint paint);
    public int getSubdivisionCount();
    public void setSubdivisionCount(int count);
}

Usage Example:

import org.jfree.chart.title.*;
import org.jfree.chart.*;

// Get default legend and customize
LegendTitle legend = chart.getLegend();
if (legend != null) {
    // Position and appearance
    legend.setPosition(RectangleEdge.RIGHT);
    legend.setItemFont(new Font("Arial", Font.PLAIN, 12));
    legend.setItemPaint(Color.BLACK);
    legend.setBackgroundPaint(Color.WHITE);
    
    // Item arrangement
    legend.setItemArrangement(new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.CENTER, 2.0, 2.0));
    
    // Graphic positioning
    legend.setLegendItemGraphicAnchor(RectangleAnchor.CENTER_LEFT);
    legend.setLegendItemGraphicEdge(RectangleEdge.LEFT);
    legend.setLegendItemGraphicLocation(RectangleAnchor.CENTER_LEFT);
}

// Create custom legend items
LegendItemCollection customItems = new LegendItemCollection();

// Add shape-based legend item
LegendItem shapeItem = new LegendItem(
    "Data Points",                                    // Label
    "Scatter plot data points",                       // Description
    "Tooltip for data points",                        // Tooltip
    null,                                            // URL
    true,                                            // Shape visible
    new Ellipse2D.Double(-4, -4, 8, 8),            // Shape
    true,                                            // Shape filled
    Color.BLUE,                                      // Fill paint
    true,                                            // Outline visible
    Color.BLACK,                                     // Outline paint
    new BasicStroke(1.0f),                          // Outline stroke
    false,                                           // Line visible
    new Line2D.Double(-5, 0, 5, 0),                // Line shape
    new BasicStroke(2.0f),                          // Line stroke
    Color.BLUE                                       // Line paint
);

// Add line-based legend item
LegendItem lineItem = new LegendItem(
    "Trend Line",                                    // Label
    "Linear regression line",                        // Description
    "Tooltip for trend line",                       // Tooltip
    null,                                           // URL
    false,                                          // Shape visible
    null,                                           // Shape
    false,                                          // Shape filled
    null,                                           // Fill paint
    false,                                          // Outline visible
    null,                                           // Outline paint
    null,                                           // Outline stroke
    true,                                           // Line visible
    new Line2D.Double(-7, 0, 7, 0),               // Line shape
    new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND), // Line stroke
    Color.RED                                       // Line paint
);

customItems.add(shapeItem);
customItems.add(lineItem);

// Create custom legend
LegendTitle customLegend = new LegendTitle(new LegendItemSource() {
    public LegendItemCollection getLegendItems() {
        return customItems;
    }
});

customLegend.setPosition(RectangleEdge.BOTTOM);
chart.addSubtitle(customLegend);

Install with Tessl CLI

npx tessl i tessl/maven-org-jfree--jfreechart

docs

axes-scales.md

chart-creation.md

data-management.md

export-display.md

index.md

labels-tooltips-legends.md

plots-customization.md

rendering-styling.md

tile.json