在调整图表大小时,轴刻度标签的字体大小也会缩放。例如,如果图表太小,则文本变窄,但如果图表较大,则字幕会被放大。
例如,如果我制作两个具有相同数据集和字体大小属性但具有不同图表大小的相似图表,则绘制字幕的有效大小会有所不同。
是否可以禁用此功能。我想设置轴刻度标签的固定字体大小,或者至少不要变大。
以下是介绍此问题的代码:
import java.awt.Container;
import java.awt.Dimension;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.Hour;
import org.jfree.data.time.ohlc.OHLCItem;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
public class ChartExample extends JFrame {
public ChartExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OHLCSeriesCollection dataset = createDataSet();
// First chart
JFreeChart chart = ChartFactory.createCandlestickChart("", null, null, dataset, true);
chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(800, 500));
// Second chart
JFreeChart chart2 = ChartFactory.createCandlestickChart("", null, null, dataset, true);
chart2.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
ChartPanel chartPanel2 = new ChartPanel(chart2);
chartPanel2.setPreferredSize(new Dimension(40, 100));
Container pane = getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
pane.add(chartPanel);
pane.add(chartPanel2);
}
private OHLCSeriesCollection createDataSet() {
OHLCSeries series = new OHLCSeries("series");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
for (int i = 10; i < 25; i++) {
cal.add(Calendar.HOUR_OF_DAY, 1);
series.add(new OHLCItem(new Hour(cal.getTime()), i, 2 * i, 1, i / 2));
}
OHLCSeriesCollection dataset = new OHLCSeriesCollection();
dataset.addSeries(series);
return dataset;
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
ChartExample demo = new ChartExample();
demo.pack();
demo.setVisible(true);
});
}
}
答案 0 :(得分:1)
JFreeChart总是尝试缩放规定的图表元素以填充可用空间。检查控制该空间的一些方法here。在BoxLayout
的特定情况下,如Box Layout Features中所述,请注意垂直空间如何根据所包含组件的首选高度在顶部到底部的框布局中分布。
在下面示例的变体中,我已将图例移至RIGHT
以最大化可用的垂直空间。我还指定了一个任意的,可接受的最小尺寸,并将首选尺寸固定为不小于最小值的值。因为你想要&#34;轴刻度标签[to]的大小至少不会[得]更大,&#34;当createChart()
为grow
时,false
还会有条件地剪切最大高度。调整封闭框架的大小以查看效果。
import java.awt.Dimension;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.Hour;
import org.jfree.data.time.ohlc.OHLCItem;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.ui.RectangleEdge;
/** @see https://stackoverflow.com/q/45242263/230513 */
public class ChartExample extends JFrame {
public ChartExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
add(createChart(640, 400, true));
add(createChart(640, 100, false));
}
private ChartPanel createChart(int w, int h, boolean grow) {
JFreeChart chart = ChartFactory.createCandlestickChart(
"", null, null, createDataSet(), true);
chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
chart.getLegend().setPosition(RectangleEdge.RIGHT);
ChartPanel chartPanel = new ChartPanel(chart){
private static final int H = 175;
@Override
public Dimension getPreferredSize() {
return new Dimension(w, Math.max(h, H));
}
@Override
public Dimension getMinimumSize() {
return new Dimension(w, H);
}
@Override
public Dimension getMaximumSize() {
if (!grow) return new Dimension(w, H);
return super.getMaximumSize();
}
};
return chartPanel;
}
private OHLCSeriesCollection createDataSet() {
OHLCSeries series = new OHLCSeries("series");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
for (int i = 10; i < 25; i++) {
cal.add(Calendar.HOUR_OF_DAY, 1);
series.add(new OHLCItem(new Hour(cal.getTime()), i, 2 * i, 1, i / 2));
}
OHLCSeriesCollection dataset = new OHLCSeriesCollection();
dataset.addSeries(series);
return dataset;
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
ChartExample demo = new ChartExample();
demo.pack();
demo.setVisible(true);
});
}
}