Converting a JFreeChart TimeSeries series with Day data to Week or Month data?
Asked Answered
C

1

3

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.

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.

Chanda answered 18/9, 2012 at 14:13 Comment(0)
S
4

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.

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.

image

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://mcmap.net/q/1008616/-converting-a-jfreechart-timeseries-series-with-day-data-to-week-or-month-data */
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);
    }
}
Silber answered 18/9, 2012 at 16:50 Comment(4)
First off, thank you for taking the time to answer my question. I wanted to avoid this sort of hand coded solution. I was hoping I could be pointed to a library that I could use to simply convert, for example, the daily data points to weekly data points, with all the daily data points in a week (M-S) being added together.Chanda
Sorry, I know of no such library. I'd start by converting Excel dates to Java dates in the data model. You can start with a suitable localized default view and let the user choose alternative, as suggested here.Silber
Thank you anyway. I'll keep looking and re-post here when/if I find it.Chanda
I bit the bullet and did the conversion by hand. Thank for your answers anyway.Chanda

© 2022 - 2024 — McMap. All rights reserved.