Home >Java >javaTutorial >How to Annotate a PNG File with Axes and Labels in Java?

How to Annotate a PNG File with Axes and Labels in Java?

DDD
DDDOriginal
2024-11-07 05:51:02204browse

How to Annotate a PNG File with Axes and Labels in Java?

How to Annotate a PNG File with Axes and Labels in Java

Adding axes and labels to an existing PNG image can be challenging. Rather than attempting modifications that could lead to errors and inconsistencies, it's recommended to integrate the annotations during the chart creation process.

Customizing Axes and Labels Using JFreeChart

JFreeChart provides a comprehensive API for customizing the look of charts, including axes and labels. The following example illustrates how to create a custom chart with tailored annotations:

<code class="java">import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ResponseTime {

    private static final int N = 600;
    private static final String title = "ResponseTime";
    private static final Random random = new Random();
    private static final Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
    private static final Color line = Color.gray;

    private ChartPanel createPanel() {
        JFreeChart chart = ChartFactory.createXYLineChart(
            title, "Elapsed Time (secs)", "Response Time (secs)",
            createDataset(), PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();
        MyRenderer renderer = new MyRenderer(true, true, N);
        plot.setRenderer(renderer);
        renderer.setSeriesShape(0, circle);
        renderer.setSeriesPaint(0, line);
        renderer.setUseFillPaint(true);
        renderer.setSeriesShapesFilled(0, true);
        renderer.setSeriesShapesVisible(0, true);
        renderer.setUseOutlinePaint(true);
        renderer.setSeriesOutlinePaint(0, line);
        ValueAxis range = plot.getRangeAxis();
        range.setLowerBound(0.5);
        return new ChartPanel(chart);
    }

    private static class MyRenderer extends XYLineAndShapeRenderer {

        private List<Color> clut;

        public MyRenderer(boolean lines, boolean shapes, int n) {
            super(lines, shapes);
            clut = new ArrayList<>(n);
            for (int i = 0; i < n; i++) {
                clut.add(Color.getHSBColor((float) i / n, 1, 1));
            }
        }

        @Override
        public Paint getItemFillPaint(int row, int column) {
            return clut.get(column);
        }
    }

    private XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("Series 1");
        for (double x = 0; x < N - 1; x++) {
            series.add(x, f(x));
        }
        series.add(25, 1.75); // outlier
        result.addSeries(series);
        return result;
    }

    private double f(double x) {
        double y = 0.004 * x + .75;
        return y + random.nextGaussian() * y / 10;
    }

    private void display() {
        JFrame f = new JFrame(title);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(createPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ResponseTime().display();
            }
        });
    }
}</code>

This approach allows you to control every aspect of the chart, including the style and placement of the axes, labels, and plot elements.

Color-Coding Individual Items

To color-code individual items, JFreeChart provides the getItemFillPaint() method in the XYLineAndShapeRenderer class. Here's how to use it:

<code class="java">private static class MyRenderer extends XYLineAndShapeRenderer {

    private List<Color> clut;

    public MyRenderer(boolean lines, boolean shapes, int n) {
        super(lines, shapes);
        clut = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            clut.add(Color.getHSBColor((float) i / n, 1, 1));
        }
    }

    @Override
    public Paint getItemFillPaint(int row, int column) {
        return clut.get(column);
    }
}</code>

By overriding this method, you can specify a custom color for each item in the chart. The Color.getHSBColor() method is used to generate a full spectrum of colors.

By following these techniques, you can create customized charts with annotations that meet your specific requirements.

The above is the detailed content of How to Annotate a PNG File with Axes and Labels in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn