JFreeChart:使用 java.time.LocalDate 或 java.time.LocalDateTime 创建图表

JFreeChart: create a chart with java.time.LocalDate or java.time.LocalDateTime

java.util.Date 很容易出错。它死了。万岁java.time.*.

给定一个 Map<LocalDate, Integer> dateToCountMap,我如何创建一个显示每个日期计数的 JFreeChart 图表?

LocalDate, you can create a time series chart by constructing the corresponding Day为例,如下图

LocalDate ld = entry.getKey();
Day d = new Day(ld.getDayOfMonth(), ld.getMonthValue(), ld.getYear());
series.add(d, entry.getValue());

如果你有相关的时区数据,可以在构造Day时使用。 LocalDateTime and any desired concrete RegularTimePeriod. See also the approach shown and , given an Instant. Moreover, a custom implementation of XYDataset, seen here 可以使用类似的方法,可以简单地将 toEpochMilli() 和 return 的结果从 getX().

转换为


import java.awt.Dimension;
import java.awt.EventQueue;
import java.text.DateFormat;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

/**
 * @see 
 * @see 
 */
public class XYTest {

    private static final int N = 16;

    private XYDataset createDataset() {
        long t = LocalDate.now().toEpochDay();
        Map<LocalDate, Integer> dateToCountMap = new HashMap<>();
        for (int i = 0; i < N; i++) {
            dateToCountMap.put(LocalDate.ofEpochDay(t + i), (int) Math.pow(i, 1.61));
        }
        TimeSeries series = new TimeSeries("Data)");
        for (Map.Entry<LocalDate, Integer> entry : dateToCountMap.entrySet()) {
            LocalDate ld = entry.getKey();
            Day d = new Day(ld.getDayOfMonth(), ld.getMonthValue(), ld.getYear());
            series.add(d, entry.getValue());
        }
        return new TimeSeriesCollection(series);
    }

    private JFreeChart createChart(final XYDataset dataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Test", "Day", "Value", dataset, false, false, false);
        XYPlot plot = (XYPlot) chart.getPlot();
        DateAxis domain = (DateAxis) plot.getDomainAxis();
        domain.setDateFormatOverride(DateFormat.getDateInstance());
        return chart;
    }

    static void create() {
        JFrame frame = new JFrame("Bar Chart");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        XYTest xyTest = new XYTest();
        XYDataset dataset = xyTest.createDataset();
        JFreeChart chart = xyTest.createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 300);
            }
        };
        frame.add(chartPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(XYTest::create);
    }
}