将JFreeChart TimeSeries系列与Day数据转换为Week或Month数据? [英] Converting a JFreeChart TimeSeries series with Day data to Week or Month data?

查看:133
本文介绍了将JFreeChart TimeSeries系列与Day数据转换为Week或Month数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这可能是一个愚蠢的问题,我知道可以通过确定每个数据点的周或月等来完成,但是,我正在寻找一种避免编码的方法。如果它是在一个库中完成的(大概是所有的陷阱都得到了)我宁愿使用它。

I realize this may be a silly question, and I know it could be done by determining what week or month each data point is in, etc. but, I'm looking for a way to avoid coding that. If it's been done in a library (presumably all the gotchas gotten) I would rather use that.

原始数据存储在Excel电子表格中,但我不能操纵电子表格直。如果它是唯一的解决方案,我可以制作副本并操纵它,但这将是最后的手段。我目前正在将这些数据用于JFreeChart图表,如果这有任何区别的话。我也愿意使用任何图书馆。

The raw data is stored in Excel spreadsheets, but I cannot manipulate the spreadsheets directly. I could make a copy and manipulate that if it's the only solution, but this would be the last resort. I am currently using this data for JFreeChart charts, if that makes any difference. I am also open to using any library.

非常感谢您提出的任何建议。

Thank you very much for any advice you can offer.

推荐答案

没有什么说你必须使用 TimeSeries RegularTimePeriod 。工厂方法 ChartFactory.createTimeSeriesChart()将接受任何 XYDataset ,其中域表示来自Java 纪元。以下示例扩展了 AbstractXYDataset 以合成一系列日期。您的实现必须规范化Excel日期,这些日期使用不同的纪元

Nothing says you have to use a TimeSeries or RegularTimePeriod. The factory method ChartFactory.createTimeSeriesChart() will accept any XYDataset in which the domain represents milliseconds from the Java epoch. The example below extends AbstractXYDataset to synthesize a series of days. Your implementation would have to normalize the Excel dates, which use a different epoch.

附录: Apache POI 包括静态 DateUtil.getJavaDate()执行转换的方法。生成的时间可以直接用于您选择的数据集。可以在此处找到完整示例。

Addendum: Apache POI includes static DateUtil.getJavaDate() methods to perform the conversion. The resulting times can be used directly in your chosen data set. A complete example may be found here.

import java.awt.Dimension;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.data.xy.XYDataset;

/** @see https://stackoverflow.com/a/12481509/230513 */
public class AbstractXYTest {

    private static XYDataset createDataset() {
        return new AbstractXYDataset() {

            private static final int N = 16;
            private final long t = new Date().getTime();

            @Override
            public int getSeriesCount() {
                return 1;
            }

            @Override
            public Comparable getSeriesKey(int series) {
                return "Data";
            }

            @Override
            public int getItemCount(int series) {
                return N;
            }

            @Override
            public Number getX(int series, int item) {
                return Double.valueOf(t + item * 1000 * 3600 * 24);
            }

            @Override
            public Number getY(int series, int item) {
                return Math.pow(item, 1.61);
            }
        };
    }

    private static 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;
    }

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart) {

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

这篇关于将JFreeChart TimeSeries系列与Day数据转换为Week或Month数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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