I have a icon in JFreeChart:
BufferedImage torreIcon = null;
try {
torreIcon = ImageIO.read(getClass().getClassLoader().getResource(
"Resources/Imagenes/torre_control_2.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
And I have added this icon to the XYPlot
:
XYAnnotation xyannotation = new XYImageAnnotation(0, 1900, torreIcon);
this.xyplot.addAnnotation(xyannotation);
And the result in the chart is:
The problem is when I make zoom in:
I would like that the icon always be next to the x axis, without space, and with the same size of course. Is it possible?
If I put (0, 0) coordinates, the icon center is put in 0, 0 and the image is not above x axis.
Correct; unless otherwise specified, an XYImageAnnotation
is centered at the specified data space coordinates using RectangleAnchor.CENTER
.
I put manually the coordinates that makes the icon put above the x axis initially, and as we can see the y coordinate that make this is 1900.
Instead, specify the desired RectangleAnchor
when you construct the XYImageAnnotation
:
new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM)
The example below adds a blue frame to a convenient image in order to see how the image is drawn relative to the anchor. RectangleAnchor.BOTTOM
at the origin is illustrated below. The image stays anchored to the origin as you pan and zoom. Try the other RectangleAnchor
instances to see the effect.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
/**
* @see https://stackoverflow.com/q/71780485/230513
*/
public class XYImageTest {
private static final int N = 1;
private XYDataset createDataset() {
XYSeries series = new XYSeries("Series");
for (int i = -N; i <= N; i++) {
series.add(i, i);
}
return new XYSeriesCollection(series);
}
private JFreeChart createChart(final XYDataset dataset, Image image) {
JFreeChart chart = ChartFactory.createXYLineChart("Test", "X", "Y", dataset);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
plot.addAnnotation(new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM));
return chart;
}
private void display() {
Icon icon = UIManager.getIcon("OptionPane.informationIcon");
BufferedImage image = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.setColor(Color.blue);
g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
icon.paintIcon(null, g, 0, 0);
g.dispose();
JFrame f = new JFrame("XYImageTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new ChartPanel(createChart(createDataset(), image)) {
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new XYImageTest()::display);
}
}