Reputation: 841
While resizing the chart, font size of axis tick labels is scaled too. For example if the chart is too small text is narrowed, but if the chart is large captions are enlarged.
For example, if I make two similar charts with the same dataset and font size properties but with different chart sizes, the effective size of drawn captions differs.
Is it possible to disable this feature. I would like to set fixed font size of axis tick labels or at least not getting bigger.
Here is the code that presents this issue:
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);
});
}
}
Upvotes: 1
Views: 966
Reputation: 205785
JFreeChart always tries to scale the prescribed chart elements to fill the space available. Some ways to control that space are examined here. In the particular case of BoxLayout
, as described in Box Layout Features, note how vertical space is distributed in a top-to-bottom box layout based on the preferred height of the enclosed components.
In the variation of your example below, I've moved the legend to the RIGHT
to maximize the available vertical space. I've also specified an arbitrary, acceptable minimum size and pinned the preferred size to a value no smaller than the minimum. Because you want "the size of axis tick labels [to] at least not [get] bigger," createChart()
also conditionally clips the maximum height when grow
is false
Resize the enclosing frame to see the effect.
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);
});
}
}
Upvotes: 1