Example usage for org.jfree.data.time TimeSeries getValue

List of usage examples for org.jfree.data.time TimeSeries getValue

Introduction

In this page you can find the example usage for org.jfree.data.time TimeSeries getValue.

Prototype

public Number getValue(RegularTimePeriod period) 

Source Link

Document

Returns the value for a time period.

Usage

From source file:de.fau.amos.ChartRenderer.java

/**
 * Calculates difference between actual date and date the forecast ends (end of year). Is used for forecast.
 * //from  w w  w  .  ja  v a  2  s  .  com
 * @param planned Timeperiod of forecast.
 * @param is Timeperiod until real values exist.
 * @return Returns number of time elements between the end of the two timeperiods.
 */
private double calculateDifferenz(TimeSeries planned, TimeSeries is) {
    double factor = 0;

    int invalidValues = 0;
    for (int i = 0; i < is.getItemCount(); i++) {
        if (planned.getItemCount() > i && planned.getValue(i).doubleValue() != 0) {
            factor += is.getValue(i).doubleValue() / planned.getValue(i).doubleValue();
        } else {
            invalidValues++;
        }
    }
    if (is.getItemCount() != 0 && is.getItemCount() - invalidValues != 0) {
        factor /= (is.getItemCount() - invalidValues);
    } else {
        factor = 1;
    }
    return factor;
}

From source file:org.jfree.data.time.junit.TimeSeriesTest.java

/**
 * Some more checks for the addOrUpdate() method.
 */// w  ww  .j  a v a 2  s. c  o m
public void testAddOrUpdate4() {
    TimeSeries ts = new TimeSeries("S");
    TimeSeriesDataItem overwritten = ts.addOrUpdate(new Year(2009), 20.09);
    assertNull(overwritten);
    overwritten = ts.addOrUpdate(new Year(2009), 1.0);
    assertEquals(new Double(20.09), overwritten.getValue());
    assertEquals(new Double(1.0), ts.getValue(new Year(2009)));

    // changing the overwritten record shouldn't affect the series
    overwritten.setValue(null);
    assertEquals(new Double(1.0), ts.getValue(new Year(2009)));

    TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2010), 20.10);
    overwritten = ts.addOrUpdate(item);
    assertNull(overwritten);
    assertEquals(new Double(20.10), ts.getValue(new Year(2010)));
    // changing the item that was added should not change the series
    item.setValue(null);
    assertEquals(new Double(20.10), ts.getValue(new Year(2010)));
}

From source file:org.jfree.data.time.junit.TimeSeriesTest.java

/**
 * Basic tests for the delete() method.// www .j a  va  2 s.c  o m
 */
public void testDelete2() {
    TimeSeries s1 = new TimeSeries("Series");
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.addChangeListener(this);
    this.gotSeriesChangeEvent = false;
    s1.delete(new Year(2001));
    assertTrue(this.gotSeriesChangeEvent);
    assertEquals(2, s1.getItemCount());
    assertEquals(null, s1.getValue(new Year(2001)));

    // try deleting a time period that doesn't exist...
    this.gotSeriesChangeEvent = false;
    s1.delete(new Year(2006));
    assertFalse(this.gotSeriesChangeEvent);

    // try deleting null
    try {
        s1.delete(null);
        fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        // expected
    }
}

From source file:org.jfree.data.time.TimeSeriesTest.java

/**
 * Some more checks for the addOrUpdate() method.
 *//*from   w ww . j  a  va 2 s  .c  o  m*/
@Test
public void testAddOrUpdate4() {
    TimeSeries ts = new TimeSeries("S");
    TimeSeriesDataItem overwritten = ts.addOrUpdate(new Year(2009), 20.09);
    assertNull(overwritten);
    overwritten = ts.addOrUpdate(new Year(2009), 1.0);
    assertEquals(new Double(20.09), overwritten.getValue());
    assertEquals(new Double(1.0), ts.getValue(new Year(2009)));

    // changing the overwritten record shouldn't affect the series
    overwritten.setValue(null);
    assertEquals(new Double(1.0), ts.getValue(new Year(2009)));

    TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2010), 20.10);
    overwritten = ts.addOrUpdate(item);
    assertNull(overwritten);
    assertEquals(new Double(20.10), ts.getValue(new Year(2010)));
    // changing the item that was added should not change the series
    item.setValue(null);
    assertEquals(new Double(20.10), ts.getValue(new Year(2010)));
}

From source file:org.jfree.data.time.TimeSeriesTest.java

/**
 * Check that cloning works.//from   www. j  a  v  a2s  . co  m
 */
@Test
public void testClone() throws CloneNotSupportedException {
    TimeSeries series = new TimeSeries("Test Series");
    RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
    series.add(jan1st2002, new Integer(42));

    TimeSeries clone;
    clone = (TimeSeries) series.clone();
    clone.setKey("Clone Series");
    clone.update(jan1st2002, new Integer(10));

    int seriesValue = series.getValue(jan1st2002).intValue();
    int cloneValue = clone.getValue(jan1st2002).intValue();

    assertEquals(42, seriesValue);
    assertEquals(10, cloneValue);
    assertEquals("Test Series", series.getKey());
    assertEquals("Clone Series", clone.getKey());
}

From source file:org.jfree.data.time.TimeSeriesTest.java

/**
 * Basic tests for the delete() method./*from www.  jav  a  2  s. co m*/
 */
@Test
public void testDelete2() {
    TimeSeries s1 = new TimeSeries("Series", Year.class);
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.addChangeListener(this);
    this.gotSeriesChangeEvent = false;
    s1.delete(new Year(2001));
    assertTrue(this.gotSeriesChangeEvent);
    assertEquals(2, s1.getItemCount());
    assertEquals(null, s1.getValue(new Year(2001)));

    // try deleting a time period that doesn't exist...
    this.gotSeriesChangeEvent = false;
    s1.delete(new Year(2006));
    assertFalse(this.gotSeriesChangeEvent);

    // try deleting null
    try {
        s1.delete(null);
        fail("Expected IllegalArgumentException.");
    } catch (IllegalArgumentException e) {
        // expected
    }
}

From source file:org.jfree.data.time.junit.TimeSeriesTest.java

/**
 * Check that cloning works.//  w w w  .  j  a  va 2s . c o  m
 */
public void testClone() {

    TimeSeries series = new TimeSeries("Test Series");

    RegularTimePeriod jan1st2002 = new Day(1, MonthConstants.JANUARY, 2002);
    try {
        series.add(jan1st2002, new Integer(42));
    } catch (SeriesException e) {
        System.err.println("Problem adding to series.");
    }

    TimeSeries clone = null;
    try {
        clone = (TimeSeries) series.clone();
        clone.setKey("Clone Series");
        try {
            clone.update(jan1st2002, new Integer(10));
        } catch (SeriesException e) {
            e.printStackTrace();
        }
    } catch (CloneNotSupportedException e) {
        assertTrue(false);
    }

    int seriesValue = series.getValue(jan1st2002).intValue();
    int cloneValue = Integer.MAX_VALUE;
    if (clone != null) {
        cloneValue = clone.getValue(jan1st2002).intValue();
    }

    assertEquals(42, seriesValue);
    assertEquals(10, cloneValue);
    assertEquals("Test Series", series.getKey());
    if (clone != null) {
        assertEquals("Clone Series", clone.getKey());
    } else {
        assertTrue(false);
    }

}

From source file:org.sakaiproject.sitestats.impl.chart.ChartServiceImpl.java

private AbstractDataset getTimeSeriesCollectionDataset(Report report) {
    List<Stat> reportData = report.getReportData();

    // fill dataset
    TimeSeriesCollection dataSet = new TimeSeriesCollection();
    String dataSource = report.getReportDefinition().getReportParams().getHowChartSource();
    String seriesFrom = report.getReportDefinition().getReportParams().getHowChartSeriesSource();
    if (StatsManager.T_TOTAL.equals(seriesFrom) || StatsManager.T_NONE.equals(seriesFrom)) {
        seriesFrom = null;/* w w  w  .  ja  va2s.c  o  m*/
    }
    Class periodGrouping = null;
    if (StatsManager.CHARTTIMESERIES_DAY
            .equals(report.getReportDefinition().getReportParams().getHowChartSeriesPeriod())
            || StatsManager.CHARTTIMESERIES_WEEKDAY
                    .equals(report.getReportDefinition().getReportParams().getHowChartSeriesPeriod())) {
        periodGrouping = org.jfree.data.time.Day.class;
    } else if (StatsManager.CHARTTIMESERIES_MONTH
            .equals(report.getReportDefinition().getReportParams().getHowChartSeriesPeriod())) {
        periodGrouping = org.jfree.data.time.Month.class;
    } else if (StatsManager.CHARTTIMESERIES_YEAR
            .equals(report.getReportDefinition().getReportParams().getHowChartSeriesPeriod())) {
        periodGrouping = org.jfree.data.time.Year.class;
    }
    boolean visitsTotalsChart = ReportManager.WHAT_VISITS_TOTALS
            .equals(report.getReportDefinition().getReportParams().getWhat())
            || report.getReportDefinition().getReportParams().getHowTotalsBy().contains(StatsManager.T_VISITS)
            || report.getReportDefinition().getReportParams().getHowTotalsBy()
                    .contains(StatsManager.T_UNIQUEVISITS);
    Set<RegularTimePeriod> keys = new HashSet<RegularTimePeriod>();
    if (!visitsTotalsChart && seriesFrom == null) {
        // without additional series
        String name = msgs.getString("th_total");
        TimeSeries ts = new TimeSeries(name, periodGrouping);
        for (Stat s : reportData) {
            RegularTimePeriod key = (RegularTimePeriod) getStatValue(s, dataSource, periodGrouping);
            if (key != null) {
                Number existing = null;
                if ((existing = ts.getValue(key)) == null) {
                    ts.add(key, getTotalValue(s, report));
                } else {
                    ts.addOrUpdate(key, getTotalValue(existing, s, report));
                }
                keys.add(key);
            }
        }
        dataSet.addSeries(ts);
    } else if (!visitsTotalsChart && seriesFrom != null) {
        // with additional series
        Map<Comparable, TimeSeries> series = new HashMap<Comparable, TimeSeries>();
        //TimeSeries ts = new TimeSeries(dataSource, org.jfree.data.time.Day.class);
        for (Stat s : reportData) {
            RegularTimePeriod key = (RegularTimePeriod) getStatValue(s, dataSource, periodGrouping);
            Comparable serie = (Comparable) getStatValue(s, seriesFrom);

            if (key != null && serie != null) {
                // determine appropriate serie
                TimeSeries ts = null;
                if (!series.containsKey(serie)) {
                    ts = new TimeSeries(serie.toString(), periodGrouping);
                    series.put(serie, ts);
                } else {
                    ts = series.get(serie);
                }

                Number existing = null;
                if ((existing = ts.getValue(key)) == null) {
                    ts.add(key, getTotalValue(s, report));
                } else {
                    ts.addOrUpdate(key, getTotalValue(existing, s, report));
                }
                keys.add(key);
            }
        }

        // add series
        for (TimeSeries ts : series.values()) {
            dataSet.addSeries(ts);
        }
    } else if (visitsTotalsChart) {
        // 2 series: visits & unique visitors
        TimeSeries tsV = new TimeSeries(msgs.getString("th_visits"), periodGrouping);
        TimeSeries tsUV = new TimeSeries(msgs.getString("th_uniquevisitors"), periodGrouping);
        for (Stat _s : reportData) {
            SiteVisits s = (SiteVisits) _s;
            RegularTimePeriod key = (RegularTimePeriod) getStatValue(s, dataSource, periodGrouping);
            if (key != null) {
                Number existing = null;
                if ((existing = tsV.getValue(key)) == null) {
                    tsV.add(key, s.getTotalVisits());
                    tsUV.add(key, s.getTotalUnique());
                } else {
                    tsV.addOrUpdate(key, s.getTotalVisits() + existing.longValue());
                    tsUV.addOrUpdate(key, s.getTotalVisits() + existing.longValue());
                }
                keys.add(key);
            }
        }
        dataSet.addSeries(tsV);
        dataSet.addSeries(tsUV);
    }

    // fill missing values with zeros
    /*for(TimeSeries ts : (List<TimeSeries>) dataSet.getSeries()) {
       for(RegularTimePeriod tp : keys) {
    if(ts.getValue(tp) == null) {
       ts.add(tp, 0.0);
    }
       }
    }*/
    dataSet.setXPosition(TimePeriodAnchor.MIDDLE);

    return dataSet;
}

From source file:de.fau.amos.ChartRenderer.java

/**
 * //from   w  ww  . ja va2  s  . c  o m
 * Creates TimeSeriesCollection that provides the basis for a Chart. Is used for forecast.
 * 
 * @param sYear Selected year.
 * @param plantId Selected plant.
 * @param method Is not used.
 * @param units Is not used.
 * @return Returns TimeSeriesCollection that provides the basis for a Chart.
 */
private TimeSeriesCollection createForecastCollection(String sYear, String plantId, String method,
        String units) {

    int year = 0;
    try {
        year = Integer.parseInt(sYear);
    } catch (NumberFormatException e) {
        return new TimeSeriesCollection();
    }

    TimeSeriesCollection collection = new TimeSeriesCollection();

    /*
     * get planned tnf
     */
    TimeSeries planned = new TimeSeries("Planned");

    double lastYearKwhPerTnf = 0;
    ResultSet rs = SQL.queryToResultSet(
            "select round((select sum(value)from measures where date_trunc('year',measure_time)='" + (year - 1)
                    + "-1-1')/(select sum(amount)from productiondata "
                    + "inner join controlpoints on productiondata.controlpoint_id=controlpoints.controlpoints_id "
                    + "where date_trunc('year',measure_time)='" + (year - 1) + "-1-1' AND plant_id='" + plantId
                    + "'),4);");

    if (rs != null) {
        try {
            if (rs.next()) {
                lastYearKwhPerTnf = rs.getDouble(1);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    String months = "";
    for (int i = 1; i <= 12; i++) {
        months += "sum(m_" + i + ")";
        if (i != 12) {
            months += ", ";
        }
    }

    rs = SQL.queryToResultSet(

            "select " + months + " from planning_values where planning_year='" + year + "' AND plant_id='"
                    + plantId + "';");

    if (rs != null) {
        try {
            while (rs.next()) {
                for (int i = 1; i <= 12; i++) {
                    planned.add(new Month((i), year),
                            rs.getDouble(i) / (lastYearKwhPerTnf != 0 ? lastYearKwhPerTnf : 1));
                }

            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    collection.addSeries(planned);

    TimeSeries is = new TimeSeries("Is");
    int passedMonths = 0;
    double lastVal = 0;
    rs = SQL.queryToResultSet(

            "select * from (select round((select sum(am) from(select sum(amount)as am,date_trunc('month',measure_time)as zeit "
                    + "from productiondata inner join controlpoints on productiondata.controlpoint_id=controlpoints.controlpoints_id "
                    + "where productiondata.measure_time >= '" + year
                    + "-01-01 00:00:00' AND productiondata.measure_time < '" + (year + 1) + "-01-01 00:00:00' "
                    + "AND reference_point='t' AND plant_id='" + plantId
                    + "' group by measure_time)as wat where zeit=gruppenZeit group by zeit order by zeit),4), "
                    + "gruppenZeit from(select sum(wert) as gruppenWert,control_point_name, zeit1 as gruppenZeit from("
                    + "select sum(value)as wert,control_point_name,date_trunc('month',measure_time)as zeit1 from measures inner join controlpoints "
                    + "on measures.controlpoint_id=controlpoints.controlpoints_id where measure_time >= '"
                    + year + "-01-01 00:00:00' AND measure_time < '" + (year + 1)
                    + "-01-01 00:00:00' AND plant_id='" + plantId
                    + "' group by measure_time,control_point_name)as data group by zeit1,control_point_name) "
                    + "as groupedByTime group by gruppenZeit)as result order by gruppenZeit;");
    if (rs != null) {
        try {
            while (rs.next()) {
                passedMonths++;
                lastVal = rs.getDouble(1) / (lastYearKwhPerTnf != 0 ? lastYearKwhPerTnf : 1);
                is.add(new Month((passedMonths), year), lastVal);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    collection.addSeries(is);

    TimeSeries forecast = new TimeSeries("Forecast");

    if (passedMonths != 0) {

        forecast.add(new Month(passedMonths, year), lastVal);
    }
    passedMonths++;

    double factor = calculateDifferenz(planned, is);

    for (int i = passedMonths; i <= 12; i++) {
        forecast.add(new Month((i), year), planned.getValue(i - 1).doubleValue() * factor);
    }
    collection.addSeries(forecast);
    return collection;
}