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

export-display.mddocs/

Export and Display

Utilities for displaying charts in GUI applications and exporting to various image formats. JFreeChart provides comprehensive support for both interactive display in desktop applications and programmatic image generation for web applications and reports.

Capabilities

Swing Display Components

Components for displaying interactive charts in Swing applications with zoom, pan, and menu capabilities.

/**
 * Swing panel for displaying charts with interactive features
 */
public class ChartPanel extends JPanel implements ChartChangeListener, ChartProgressListener, ActionListener, MouseListener, MouseMotionListener, OverlayChangeListener, Printable {
    
    /**
     * Creates a chart panel with default settings
     * @param chart the chart to display
     */
    public ChartPanel(JFreeChart chart);
    
    /**
     * Creates a chart panel with buffer control
     * @param chart the chart to display
     * @param useBuffer flag to control off-screen buffering
     */
    public ChartPanel(JFreeChart chart, boolean useBuffer);
    
    /**
     * Creates a chart panel with full customization
     * @param chart the chart to display
     * @param width preferred width
     * @param height preferred height
     * @param minimumDrawWidth minimum drawing width
     * @param minimumDrawHeight minimum drawing height  
     * @param maximumDrawWidth maximum drawing width
     * @param maximumDrawHeight maximum drawing height
     * @param useBuffer flag to control off-screen buffering
     * @param properties flag to show properties menu item
     * @param copy flag to show copy menu item
     * @param save flag to show save menu item
     * @param print flag to show print menu item
     * @param zoom flag to enable zoom functionality
     * @param tooltips flag to enable tooltips
     */
    public ChartPanel(JFreeChart chart, int width, int height, int minimumDrawWidth, int minimumDrawHeight, int maximumDrawWidth, int maximumDrawHeight, boolean useBuffer, boolean properties, boolean copy, boolean save, boolean print, boolean zoom, boolean tooltips);
    
    // Chart management
    public JFreeChart getChart();
    public void setChart(JFreeChart chart);
    
    // Size control
    public int getMinimumDrawWidth();
    public void setMinimumDrawWidth(int width);
    public int getMaximumDrawWidth();
    public void setMaximumDrawWidth(int width);
    public int getMinimumDrawHeight();
    public void setMinimumDrawHeight(int height);
    public int getMaximumDrawHeight();
    public void setMaximumDrawHeight(int height);
    public double getScaleX();
    public double getScaleY();
    
    // Buffering
    public boolean getRefreshBuffer();
    public void setRefreshBuffer(boolean flag);
    
    // Zooming
    public boolean isDomainZoomable();
    public void setDomainZoomable(boolean zoomable);
    public boolean isRangeZoomable();
    public void setRangeZoomable(boolean zoomable);
    public void restoreAutoBounds();
    public void restoreAutoRangeBounds();
    public void restoreAutoDomainBounds();
    public void zoomInBoth(double x, double y);
    public void zoomInDomain(double x, double y);
    public void zoomInRange(double x, double y);
    public void zoomOutBoth(double x, double y);
    public void zoomOutDomain(double x, double y);
    public void zoomOutRange(double x, double y);
    public void zoom(Rectangle2D selection);
    public Point2D getZoomPoint();
    public void setZoomPoint(Point2D zoomPoint);
    public boolean getZoomAroundAnchor();
    public void setZoomAroundAnchor(boolean zoomAroundAnchor);
    public double getZoomInFactor();
    public void setZoomInFactor(double factor);
    public double getZoomOutFactor();
    public void setZoomOutFactor(double factor);
    public int getZoomTriggerDistance();
    public void setZoomTriggerDistance(int distance);
    
    // Popup menu
    public JPopupMenu getPopupMenu();
    public void setPopupMenu(JPopupMenu popup);
    public ChartPanelPopupMenuOrder getPopupMenuOrder();
    public void setPopupMenuOrder(ChartPanelPopupMenuOrder order);
    
    // Tooltips
    public boolean isDisplayToolTips();
    public void setDisplayToolTips(boolean flag);
    public String getToolTipText(MouseEvent e);
    public Point translateJava2DToScreen(Point2D java2DPoint);
    public Point2D translateScreenToJava2D(Point screenPoint);
    public Rectangle2D scale(Rectangle2D rect);
    
    // Rendering info
    public ChartRenderingInfo getChartRenderingInfo();
    
    // Mouse event handling
    public void addChartMouseListener(ChartMouseListener listener);
    public void removeChartMouseListener(ChartMouseListener listener);
    
    // Overlays
    public void addOverlay(Overlay overlay);
    public void removeOverlay(Overlay overlay);
    
    // Editor
    public void doEditChartProperties();
    
    // File operations
    public void doSaveAs() throws IOException;
    public void doCopy();
    public void doPrint();
    
    // Constants
    public static final boolean DEFAULT_BUFFER_USED = true;
    public static final int DEFAULT_WIDTH = 680;
    public static final int DEFAULT_HEIGHT = 420;
    public static final int DEFAULT_MINIMUM_DRAW_WIDTH = 300;
    public static final int DEFAULT_MINIMUM_DRAW_HEIGHT = 200;
    public static final int DEFAULT_MAXIMUM_DRAW_WIDTH = 1024;
    public static final int DEFAULT_MAXIMUM_DRAW_HEIGHT = 768;
    public static final int DEFAULT_ZOOM_TRIGGER_DISTANCE = 10;
}

/**
 * Specialized chart panel for polar plots
 */
public class PolarChartPanel extends ChartPanel {
    /**
     * Creates a polar chart panel
     * @param chart the polar chart to display
     */
    public PolarChartPanel(JFreeChart chart);
    
    public boolean isRotateEnabled();
    public void setRotateEnabled(boolean enabled);
    public double getAngle();
    public void setAngle(double angle);
}

/**
 * Chart panel optimized for background rendering
 */
public class OfflineRenderingChartPanel extends ChartPanel {
    /**
     * Creates an offline rendering chart panel
     * @param chart the chart to display
     */
    public OfflineRenderingChartPanel(JFreeChart chart);
    
    public boolean isRenderingOffscreen();
    public void setRenderingOffscreen(boolean flag);
}

Usage Example:

import org.jfree.chart.*;
import javax.swing.*;

// Create chart panel with default settings
JFreeChart chart = ChartFactory.createLineChart(
    "Temperature Data", "Time", "Temperature (°C)", dataset);
    
ChartPanel chartPanel = new ChartPanel(chart);

// Customize chart panel
chartPanel.setPreferredSize(new Dimension(800, 600));
chartPanel.setMinimumDrawWidth(400);
chartPanel.setMaximumDrawWidth(1200);
chartPanel.setMinimumDrawHeight(300);
chartPanel.setMaximumDrawHeight(800);

// Enable interactive features
chartPanel.setDomainZoomable(true);
chartPanel.setRangeZoomable(true);
chartPanel.setDisplayToolTips(true);

// Configure zoom behavior
chartPanel.setZoomAroundAnchor(true);
chartPanel.setZoomInFactor(0.9);
chartPanel.setZoomOutFactor(1.1);

// Add to Swing application
JFrame frame = new JFrame("Chart Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(chartPanel);
frame.pack();
frame.setVisible(true);

// Add mouse listener for chart events
chartPanel.addChartMouseListener(new ChartMouseListener() {
    public void chartMouseClicked(ChartMouseEvent event) {
        ChartEntity entity = event.getEntity();
        if (entity != null) {
            System.out.println("Clicked: " + entity.toString());
        }
    }
    
    public void chartMouseMoved(ChartMouseEvent event) {
        // Handle mouse movement
    }
});

Image Export Utilities

Static utility methods for exporting charts to various image formats.

/**
 * Utility class for chart operations and image export
 */
public abstract class ChartUtils {
    
    // Theme management
    public static void applyCurrentTheme(JFreeChart chart);
    
    // PNG export
    public static void writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height) throws IOException;
    public static void writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info) throws IOException;
    public static void writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, boolean encodeAlpha, int compression) throws IOException;
    public static void writeChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info, boolean encodeAlpha, int compression) throws IOException;
    
    public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height) throws IOException;
    public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info) throws IOException;
    public static void saveChartAsPNG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info, boolean encodeAlpha, int compression) throws IOException;
    
    public static void writeScaledChartAsPNG(OutputStream out, JFreeChart chart, int width, int height, int widthScaleFactor, int heightScaleFactor) throws IOException;
    
    // JPEG export
    public static void writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height) throws IOException;
    public static void writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info) throws IOException;
    public static void writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height, float quality) throws IOException;
    public static void writeChartAsJPEG(OutputStream out, JFreeChart chart, int width, int height, ChartRenderingInfo info, float quality) throws IOException;
    
    public static void saveChartAsJPEG(File file, JFreeChart chart, int width, int height) throws IOException;
    public static void saveChartAsJPEG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info) throws IOException;
    public static void saveChartAsJPEG(File file, JFreeChart chart, int width, int height, float quality) throws IOException;
    public static void saveChartAsJPEG(File file, JFreeChart chart, int width, int height, ChartRenderingInfo info, float quality) throws IOException;
    
    // Image map generation for web applications
    public static void writeImageMap(PrintWriter writer, String name, ChartRenderingInfo info) throws IOException;
    public static void writeImageMap(PrintWriter writer, String name, ChartRenderingInfo info, ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, URLTagFragmentGenerator urlTagFragmentGenerator) throws IOException;
    
    public static String getImageMap(String name, ChartRenderingInfo info);
    public static String getImageMap(String name, ChartRenderingInfo info, ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, URLTagFragmentGenerator urlTagFragmentGenerator);
}

/**
 * Rendering information collected during chart drawing
 */
public class ChartRenderingInfo implements Cloneable, Serializable {
    /**
     * Creates new rendering info
     */
    public ChartRenderingInfo();
    
    /**
     * Creates rendering info with entity collection
     * @param entities the entity collection
     */
    public ChartRenderingInfo(EntityCollection entities);
    
    public void clear();
    public PlotRenderingInfo getPlotInfo();
    public void setPlotInfo(PlotRenderingInfo plotInfo);
    public Rectangle2D getChartArea();
    public void setChartArea(Rectangle2D area);
    public EntityCollection getEntityCollection();
    public void setEntityCollection(EntityCollection entities);
}

/**
 * Entity collection for interactive chart areas
 */
public interface EntityCollection {
    public void add(ChartEntity entity);
    public void addAll(EntityCollection collection);
    public void clear();
    public ChartEntity getEntity(double x, double y);
    public ChartEntity getEntity(int index);
    public int getEntityCount();
    public Collection getEntities();
    public Iterator iterator();
}

/**
 * Standard entity collection implementation
 */
public class StandardEntityCollection implements EntityCollection {
    public StandardEntityCollection();
    
    public void add(ChartEntity entity);
    public void addAll(EntityCollection collection);
    public void clear();
    public ChartEntity getEntity(double x, double y);
    public ChartEntity getEntity(int index);
    public int getEntityCount();
    public Collection getEntities();
    public Iterator iterator();
}

Usage Example:

import org.jfree.chart.ChartUtils;
import java.io.*;

// Export chart as PNG
try {
    ChartUtils.saveChartAsPNG(
        new File("chart.png"), 
        chart, 
        800, 600
    );
} catch (IOException e) {
    e.printStackTrace();
}

// Export with rendering info for image maps
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
try {
    // Save image with entity information
    ChartUtils.saveChartAsPNG(
        new File("interactive_chart.png"), 
        chart, 
        800, 600, 
        info
    );
    
    // Generate HTML image map
    String imageMap = ChartUtils.getImageMap("chart", info);
    
    // Write complete HTML
    PrintWriter htmlWriter = new PrintWriter(new FileWriter("chart.html"));
    htmlWriter.println("<html><body>");
    htmlWriter.println("<img src='interactive_chart.png' usemap='#chart' />");
    htmlWriter.println(imageMap);
    htmlWriter.println("</body></html>");
    htmlWriter.close();
    
} catch (IOException e) {
    e.printStackTrace();
}

// Export as JPEG with quality control
try {
    ChartUtils.saveChartAsJPEG(
        new File("chart.jpg"), 
        chart, 
        800, 600, 
        0.95f  // 95% quality
    );
} catch (IOException e) {
    e.printStackTrace();
}

// Stream export for web applications
try (OutputStream out = response.getOutputStream()) {
    response.setContentType("image/png");
    ChartUtils.writeChartAsPNG(out, chart, 600, 400);
} catch (IOException e) {
    e.printStackTrace();
}

Chart Event Handling

Event interfaces and classes for handling chart changes and user interactions.

/**
 * Listener interface for chart change events
 */
public interface ChartChangeListener extends EventListener {
    /**
     * Called when a chart is changed
     * @param event details about the change event
     */
    public void chartChanged(ChartChangeEvent event);
}

/**
 * Chart change event information
 */
public class ChartChangeEvent extends EventObject {
    /**
     * Creates a chart change event
     * @param source the chart that changed
     */
    public ChartChangeEvent(Object source);
    
    /**
     * Creates a chart change event with chart reference
     * @param source the object that changed
     * @param chart the chart that was affected
     */
    public ChartChangeEvent(Object source, JFreeChart chart);
    
    public JFreeChart getChart();
    public ChartChangeEventType getType();
}

/**
 * Listener interface for chart progress events
 */
public interface ChartProgressListener extends EventListener {
    /**
     * Called when chart drawing progresses
     * @param event details about the progress event
     */
    public void chartProgress(ChartProgressEvent event);
}

/**
 * Chart progress event information
 */
public class ChartProgressEvent extends EventObject {
    public static final int DRAWING_STARTED = 1;
    public static final int DRAWING_FINISHED = 2;
    
    /**
     * Creates a chart progress event
     * @param source the chart being drawn
     * @param chart the chart
     * @param type the event type
     * @param percent the percentage complete
     */
    public ChartProgressEvent(Object source, JFreeChart chart, int type, int percent);
    
    public JFreeChart getChart();
    public int getType();
    public int getPercent();
}

/**
 * Mouse event listener for chart interactions
 */
public interface ChartMouseListener extends EventListener {
    /**
     * Called when the mouse is clicked on a chart
     * @param event details about the mouse event
     */
    public void chartMouseClicked(ChartMouseEvent event);
    
    /**
     * Called when the mouse moves over a chart
     * @param event details about the mouse event
     */
    public void chartMouseMoved(ChartMouseEvent event);
}

/**
 * Mouse event information for charts
 */
public class ChartMouseEvent extends EventObject {
    /**
     * Creates a chart mouse event
     * @param chart the chart
     * @param trigger the mouse event that triggered this
     * @param entity the chart entity under the mouse (may be null)
     */
    public ChartMouseEvent(JFreeChart chart, MouseEvent trigger, ChartEntity entity);
    
    public JFreeChart getChart();
    public MouseEvent getTrigger();
    public ChartEntity getEntity();
}

/**
 * Base class for chart entities (clickable areas)
 */
public abstract class ChartEntity implements Cloneable, Serializable {
    /**
     * Creates a chart entity
     * @param area the area occupied by the entity
     */  
    public ChartEntity(Shape area);
    
    /**
     * Creates a chart entity with tooltip
     * @param area the area occupied by the entity
     * @param toolTipText the tooltip text
     */
    public ChartEntity(Shape area, String toolTipText);
    
    /**
     * Creates a chart entity with tooltip and URL
     * @param area the area occupied by the entity
     * @param toolTipText the tooltip text
     * @param urlText the URL text
     */
    public ChartEntity(Shape area, String toolTipText, String urlText);
    
    public Shape getArea();
    public void setArea(Shape area);
    public String getToolTipText();
    public void setToolTipText(String text);
    public String getURLText();
    public void setURLText(String text);
    public String getImageMapAreaTag(ToolTipTagFragmentGenerator toolTipTagFragmentGenerator, URLTagFragmentGenerator urlTagFragmentGenerator);
}

/**
 * Entity representing a data item
 */
public class CategoryItemEntity extends ChartEntity {
    public CategoryItemEntity(Shape area, String toolTipText, String urlText, CategoryDataset dataset, Comparable rowKey, Comparable columnKey);
    
    public CategoryDataset getDataset();
    public Comparable getRowKey();
    public Comparable getColumnKey();
}

/**
 * Entity representing an XY data item
 */
public class XYItemEntity extends ChartEntity {
    public XYItemEntity(Shape area, String toolTipText, String urlText, XYDataset dataset, int series, int item);
    
    public XYDataset getDataset();
    public int getSeriesIndex();
    public int getItem();
}

/**
 * Entity representing a pie section
 */
public class PieSectionEntity extends ChartEntity {
    public PieSectionEntity(Shape area, String toolTipText, String urlText, PieDataset dataset, Comparable sectionKey);
    
    public PieDataset getDataset();
    public Comparable getSectionKey();
}

Usage Example:

// Add change listener to chart
chart.addChangeListener(new ChartChangeListener() {
    public void chartChanged(ChartChangeEvent event) {
        System.out.println("Chart changed: " + event.getChart().getTitle().getText());
        // Update dependent components
        updateChartSummary();
    }
});

// Add progress listener for long-running operations
chart.addProgressListener(new ChartProgressListener() {
    public void chartProgress(ChartProgressEvent event) {
        if (event.getType() == ChartProgressEvent.DRAWING_STARTED) {
            progressBar.setVisible(true);
        } else if (event.getType() == ChartProgressEvent.DRAWING_FINISHED) {
            progressBar.setVisible(false);
        }
        progressBar.setValue(event.getPercent());
    }
});

// Handle mouse interactions with chart entities
chartPanel.addChartMouseListener(new ChartMouseListener() {
    public void chartMouseClicked(ChartMouseEvent event) {
        ChartEntity entity = event.getEntity();
        if (entity instanceof CategoryItemEntity) {
            CategoryItemEntity item = (CategoryItemEntity) entity;
            System.out.println("Clicked on: " + item.getRowKey() + ", " + item.getColumnKey());
            // Show detail dialog or update display
            showItemDetails(item);
        } else if (entity instanceof XYItemEntity) {
            XYItemEntity item = (XYItemEntity) entity;
            System.out.println("Clicked on series " + item.getSeriesIndex() + ", item " + item.getItem());
        }
    }
    
    public void chartMouseMoved(ChartMouseEvent event) {
        // Update status bar with entity information
        ChartEntity entity = event.getEntity();
        if (entity != null) {
            statusLabel.setText(entity.getToolTipText());
        } else {
            statusLabel.setText("");
        }
    }
});

Advanced Display Features

Advanced display capabilities including overlays, printing, and custom rendering.

// Overlay support for additional graphics
public interface Overlay {
    public void paintOverlay(Graphics2D g2, ChartPanel chartPanel);
    public void addChangeListener(OverlayChangeListener listener);
    public void removeChangeListener(OverlayChangeListener listener);
}

public class CrosshairOverlay implements Overlay {
    public CrosshairOverlay();
    public void addDomainCrosshair(Crosshair crosshair);
    public void removeDomainCrosshair(Crosshair crosshair);
    public void clearDomainCrosshairs();
    public void addRangeCrosshair(Crosshair crosshair);
    public void removeRangeCrosshair(Crosshair crosshair);
    public void clearRangeCrosshairs();
}

public class Crosshair implements Cloneable, PublicCloneable, Serializable {
    public Crosshair();
    public Crosshair(double value);
    public Crosshair(double value, Paint paint, Stroke stroke);
    
    public boolean isVisible();
    public void setVisible(boolean visible);
    public double getValue();
    public void setValue(double value);
    public Paint getPaint();
    public void setPaint(Paint paint);
    public Stroke getStroke();
    public void setStroke(Stroke stroke);
    public boolean isLabelVisible();
    public void setLabelVisible(boolean visible);
    public String getLabel();
    public void setLabel(String label);
}

// Printing support
public interface Printable {
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException;
}

// Custom mouse wheel handling
public class MouseWheelHandler implements MouseWheelListener {
    public MouseWheelHandler(ChartPanel chartPanel);
    public double getZoomFactor();
    public void setZoomFactor(double zoomFactor);
    public void mouseWheelMoved(MouseWheelEvent e);
}

// Chart transferable for clipboard operations
public class ChartTransferable implements Transferable {
    public ChartTransferable(JFreeChart chart, int width, int height);
    public ChartTransferable(JFreeChart chart, int width, int height, boolean drawBorder);
    
    public DataFlavor[] getTransferDataFlavors();
    public boolean isDataFlavorSupported(DataFlavor flavor);
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException;
}

Usage Example:

// Add crosshair overlay
CrosshairOverlay overlay = new CrosshairOverlay();

// Create domain crosshair
Crosshair domainCrosshair = new Crosshair(0.0, Color.RED, new BasicStroke(1.0f));
domainCrosshair.setLabelVisible(true);
domainCrosshair.setLabel("Current Time");
overlay.addDomainCrosshair(domainCrosshair);

// Create range crosshair
Crosshair rangeCrosshair = new Crosshair(100.0, Color.BLUE, new BasicStroke(1.0f));
rangeCrosshair.setLabelVisible(true);
rangeCrosshair.setLabel("Target Value");
overlay.addRangeCrosshair(rangeCrosshair);

// Add overlay to chart panel
chartPanel.addOverlay(overlay);

// Enable mouse wheel zooming
MouseWheelHandler wheelHandler = new MouseWheelHandler(chartPanel);
wheelHandler.setZoomFactor(0.1);
chartPanel.addMouseWheelListener(wheelHandler);

// Configure for printing
chartPanel.print(graphics, pageFormat, 0);

// Copy chart to clipboard
Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
ChartTransferable transferable = new ChartTransferable(chart, 800, 600);
clipboard.setContents(transferable, null);

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