ValueMarker 标签被切割
ValueMarker label is cut
我正在用一些 ValueMarkers 做直方图,它在上侧被切掉了。我试图添加“\n”来降低它的位置,但我还是一样。如图所示:
这里有一个 MCVE 来说明这个问题。它需要一些图像(为简单起见,相同的图像)并显示直方图。它从那里放置了一些标记。为了便于每 50 个 bin 看到问题,但这并不重要。
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();
}
}
}
有什么方法可以解决这个问题,让它始终可见,而不是在外框下?
非常感谢。
我可以通过添加这两个属性来解决它:
marker.setLabelAnchor(RectangleAnchor.TOP);
marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
我正在用一些 ValueMarkers 做直方图,它在上侧被切掉了。我试图添加“\n”来降低它的位置,但我还是一样。如图所示:
这里有一个 MCVE 来说明这个问题。它需要一些图像(为简单起见,相同的图像)并显示直方图。它从那里放置了一些标记。为了便于每 50 个 bin 看到问题,但这并不重要。
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();
}
}
}
有什么方法可以解决这个问题,让它始终可见,而不是在外框下? 非常感谢。
我可以通过添加这两个属性来解决它:
marker.setLabelAnchor(RectangleAnchor.TOP);
marker.setLabelTextAnchor(TextAnchor.TOP_LEFT);