In JFreechart I have an X axis with dates (and times).
How can I ask JFreechart to optimize them and make the most out of it?
Right now it contains more label th
Your updated example creates a CategoryDataset
and uses the ChartFactory
method, createLineChart()
, to create a CategoryPlot
. You can adjust the label positions for readability as shown here and below. Moreover,
it would be nice to have a vertical grid line only when the label is visible and hide all other grid lines.
plot.getDomainAxis().setCategoryLabelPositions(
CategoryLabelPositions.UP_45);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinesVisible(false);
More generally, create a TimeSeries
and use the corresponding ChartFactory
method, createTimeSeriesChart()
. The resulting DateAxis
will automatically adjust the labels when the enclosing chart is resized. In addition,
You can adjust the date format as shown here.
To establish the chart's initial size, override getPreferredSize()
, as suggested here.
When using setRange()
, query the underlying dataset, as shown below.
Construct and manipulate Swing GUI objects only on the event dispatch thread.
import java.awt.Dimension;
import java.awt.EventQueue;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
public class TempChart extends ApplicationFrame {
public TempChart(String applicationTitle, String chartTitle) {
super(applicationTitle);
TimeSeries s = createSeries();
JFreeChart chart = ChartFactory.createTimeSeriesChart(
chartTitle, "Date", "Temperature", new TimeSeriesCollection(s));
XYPlot plot = (XYPlot) chart.getPlot();
plot.getRangeAxis().setRange(Math.floor(s.getMinY()), Math.ceil(s.getMaxY()));
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(560, 367);
}
};
add(chartPanel);
}
private TimeSeries createSeries() {
TimeSeries series = new TimeSeries("Temperature");
series.add(new Hour(0, new Day()), 26.44);
series.add(new Hour(1, new Day()), 26.2);
series.add(new Hour(2, new Day()), 25.93);
series.add(new Hour(3, new Day()), 25.71);
series.add(new Hour(4, new Day()), 25.54);
series.add(new Hour(5, new Day()), 25.42);
series.add(new Hour(6, new Day()), 25.25);
series.add(new Hour(7, new Day()), 25.19);
series.add(new Hour(8, new Day()), 25.25);
series.add(new Hour(9, new Day()), 25.36);
series.add(new Hour(10, new Day()), 25.52);
series.add(new Hour(11, new Day()), 25.86);
series.add(new Hour(12, new Day()), 26.51);
series.add(new Hour(13, new Day()), 26.82);
return series;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TempChart chart = new TempChart(
"Temperature demo", "Time Axis labels adjust on resize");
chart.pack();
chart.setLocationRelativeTo(null);
chart.setVisible(true);
}
});
}
}