I'm doing an histogram with some ValueMarkers and it gets cut in the upper side. I've tried to add "\n" to put it a little bit down but I still get the same. As shown in the photo:
Here is a MCVE to show this problem. It takes some images (for simplicity the same one) and shows the histogram. From there it puts some markers. To make it easy to see the problem every 50 bins, but that doesn't matter.
package mcve;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
public class HistogramRAW {
private int BINS;
private Raster raster;//Load image
private String filename;
private RenderedImage image;
private JFreeChart chart; //Create histogram
private ChartPanel panel;
private HistogramDataset dataset;
private XYBarRenderer renderer;
private JFrame f; //Frame
public HistogramRAW(String filename) {
this.filename = filename;
this.BINS = 256;
}
private void getImage(String filename) {
try {
URL url = new URL(filename);
final BufferedImage bi = ImageIO.read(url);
raster = bi.getRaster();
image = (RenderedImage) bi;
} catch (IOException e) {
e.printStackTrace(System.err);
return;
}
}
private ChartPanel createChartPanel() {
// dataset
dataset = new HistogramDataset();
getImage(filename); //Put it here to get width and height from it
final int w = image.getWidth();
final int h = image.getHeight();
double[] buffer = new double[w * h];
//R
buffer = raster.getSamples(0, 0, w, h, 0, buffer);
dataset.addSeries("red", buffer, BINS);
//G
getImage(filename);
buffer = raster.getSamples(0, 0, w, h, 0, buffer);
dataset.addSeries("green", buffer, BINS);
//B
getImage(filename);
buffer = raster.getSamples(0, 0, w, h, 0, buffer);
dataset.addSeries("blue", buffer, BINS);
//chart
chart = ChartFactory.createHistogram("Histogram", "",
"", dataset, PlotOrientation.VERTICAL, false, true, false);
//Set colors
XYPlot plot = (XYPlot) chart.getPlot();
renderer = (XYBarRenderer) plot.getRenderer();
renderer.setBarPainter(new StandardXYBarPainter());
Paint[] paintArray = { // translucent red, green blue
new Color(0x800000ff, true),//blue
new Color(0x8000ff00, true),//green
new Color(0x80ff0000, true)//red
};
plot.setDrawingSupplier(new DefaultDrawingSupplier(
paintArray,
DefaultDrawingSupplier.DEFAULT_FILL_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
//Create panel with chart
panel = new ChartPanel(chart);
panel.setMouseWheelEnabled(true);
return panel;
}
public void display() throws CloneNotSupportedException {
f = new JFrame("Histogram");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.add(createChartPanel(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private HistogramDataset updatedDataset() {
// dataset
HistogramDataset dataset = new HistogramDataset();
getImage(filename); //Put it here to get width and height from it
final int w = image.getWidth();
final int h = image.getHeight();
double[] buffer = new double[w * h];
//R
buffer = raster.getSamples(0, 0, w, h, 0, buffer);
dataset.addSeries("red", buffer, BINS);
//G
getImage(filename);
buffer = raster.getSamples(0, 0, w, h, 0, buffer);
dataset.addSeries("green", buffer, BINS);
//B
getImage(filename);
buffer = raster.getSamples(0, 0, w, h, 0, buffer);
dataset.addSeries("blue", buffer, BINS);
return dataset;
}
public void showMarkers() {
XYPlot plot = (XYPlot) chart.getPlot();
plot.clearDomainMarkers();
int i = 0;
while (i * 50 < 256) {
ValueMarker marker = new ValueMarker(i * 50);
marker.setLabel("\nEv +" + i);
plot.addDomainMarker(marker);
i++;
}
}
public static void main(String[] args) {
try {
HistogramRAW hist = new HistogramRAW("https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/245px-Wikipedia-logo-v2.svg.png");
hist.display();
hist.showMarkers();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
Is there any way to fix that so that it is always visible and not under the outer frame? Thanks a lot.
I was able to solve it by adding this two properties: