JFreeChart:使用java.time.LocalDate或java.time.LocalDateTime创建图表 [英] JFreeChart: create a chart with java.time.LocalDate or java.time.LocalDateTime

查看:85
本文介绍了JFreeChart:使用java.time.LocalDate或java.time.LocalDateTime创建图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.Date 非常容易出错.死了万岁 java.time.* .

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

解决方案

对于

 导入java.awt.Dimension;导入java.awt.EventQueue;导入java.text.DateFormat;导入java.time.LocalDate;导入java.util.HashMap;导入java.util.Map;导入javax.swing.JFrame;导入org.jfree.chart.ChartFactory;导入org.jfree.chart.ChartPanel;导入org.jfree.chart.JFreeChart;导入org.jfree.chart.axis.DateAxis;导入org.jfree.chart.plot.XYPlot;导入org.jfree.data.time.Day;导入org.jfree.data.time.TimeSeries;导入org.jfree.data.time.TimeSeriesCollection;导入org.jfree.data.xy.XYDataset;/*** @请参阅https://stackoverflow.com/a/12481509/230513* @请参阅https://stackoverflow.com/a/12481509/230513*/公共课程XYTest {私有静态最终整数N = 16;私人XYDataset createDataset(){长t = LocalDate.now().toEpochDay();Map< LocalDate,Integer>dateToCountMap = new HashMap<>();对于(int i = 0; i< N; i ++){dateToCountMap.put(EpochDay(t + i),(int)Math.pow(i,1.61))(LocalDate.ofEpochDay(t + i),(int)Math.pow(i,1.61));}TimeSeries series =新的TimeSeries("Data"));用于(Map.Entry< LocalDate,Integer>条目:dateToCountMap.entrySet()){本地日期ld = entry.getKey();Day d = new Day(ld.getDayOfMonth(),ld.getMonthValue(),ld.getYear());series.add(d,entry.getValue());}返回新的TimeSeriesCollection(series);}私人JFreeChart createChart(最终XYDataset数据集){JFreeChart图表= ChartFactory.createTimeSeriesChart(测试",天",值",数据集,假,假,假);XYPlot plot =(XYPlot)chart.getPlot();DateAxis域=(DateAxis)plot.getDomainAxis();domain.setDateFormatOverride(DateFormat.getDateInstance());退货图表}静态无效create(){JFrame frame = new JFrame("Bar Chart");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);XYTest xyTest =新的XYTest();XYDataset数据集= xyTest.createDataset();JFreeChart图表= xyTest.createChart(dataset);ChartPanel chartPanel =新的ChartPanel(图表){@Override公共维度getPreferredSize(){返回新的Dimension(800,300);}};frame.add(chartPanel);frame.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}公共静态void main(String [] args){EventQueue.invokeLater(XYTest :: create);}} 

java.util.Date is very error prone. It is dead. Long live java.time.*.

Given a Map<LocalDate, Integer> dateToCountMap, how do I create a JFreeChart chart that shows the count per date?

解决方案

In the case of LocalDate, you can create a time series chart by constructing the corresponding Day, as shown below.

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

If you have relevant time zone data, you can use it when constructing the Day. A similar approach can be used for LocalDateTime and any desired concrete RegularTimePeriod. See also the approach shown here and here, given an Instant. Moreover, a custom implementation of XYDataset, seen here, can simply convert the result of toEpochMilli() and return it from 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 https://stackoverflow.com/a/12481509/230513
 * @see https://stackoverflow.com/a/12481509/230513
 */
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);
    }
}

这篇关于JFreeChart:使用java.time.LocalDate或java.time.LocalDateTime创建图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆