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

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

Introduction

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

Prototype

public void add(RegularTimePeriod period, Number value) 

Source Link

Document

Adds a new data item to the series and sends a org.jfree.data.general.SeriesChangeEvent to all registered listeners.

Usage

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

/**
 * Serialize an instance, restore it, and check for equality.
 *//*from  w  w w .  jav a 2s .c o  m*/
@Test
public void testSerialization() {
    TimeSeries s1 = new TimeSeries("A test");
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.add(new Year(2005), 19.32);
    s1.add(new Year(2007), 16.89);
    TimeSeries s2 = (TimeSeries) TestUtilities.serialised(s1);
    assertTrue(s1.equals(s2));
}

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

/**
 * Some checks for the update(RegularTimePeriod...method).
 *//*from   w w  w .  jav a  2  s .  c  om*/
@Test
public void testUpdate_RegularTimePeriod() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.add(new Year(2010), 1.1);
    s1.add(new Year(2011), 2.2);
    s1.add(new Year(2012), 3.3);
    s1.update(new Year(2012), 4.4);
    assertEquals(4.4, s1.getMaxY(), EPSILON);
    s1.update(new Year(2010), 0.5);
    assertEquals(0.5, s1.getMinY(), EPSILON);
    s1.update(new Year(2012), null);
    assertEquals(2.2, s1.getMaxY(), EPSILON);
    s1.update(new Year(2010), null);
    assertEquals(2.2, s1.getMinY(), EPSILON);
}

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

/**
 * Some checks for the delete(int, int) method.
 *///from w w w. j a  va2  s .  c  o m
@Test
public void testDelete3() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.add(new Year(2011), 1.1);
    s1.add(new Year(2012), 2.2);
    s1.add(new Year(2013), 3.3);
    s1.add(new Year(2014), 4.4);
    s1.add(new Year(2015), 5.5);
    s1.add(new Year(2016), 6.6);
    s1.delete(2, 5);
    assertEquals(2, s1.getItemCount());
    assertEquals(new Year(2011), s1.getTimePeriod(0));
    assertEquals(new Year(2012), s1.getTimePeriod(1));
    assertEquals(1.1, s1.getMinY(), EPSILON);
    assertEquals(2.2, s1.getMaxY(), EPSILON);
}

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

/**
 * A test for the bug report 1075255.//from  ww w  .j a v  a2  s  . c om
 */
@Test
public void testBug1075255() {
    TimeSeries ts = new TimeSeries("dummy");
    ts.add(new FixedMillisecond(0L), 0.0);
    TimeSeries ts2 = new TimeSeries("dummy2");
    ts2.add(new FixedMillisecond(0L), 1.0);
    try {
        ts.addAndOrUpdate(ts2);
    } catch (Exception e) {
        fail("No exceptions should be thrown.");
    }
    assertEquals(1, ts.getItemCount());
}

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

/**
 * Checks that the min and max y values are updated correctly when copying
 * a subset.//from   ww w . j  av a2s  .  c o  m
 *
 * @throws java.lang.CloneNotSupportedException
 */
@Test
public void testCreateCopy3() throws CloneNotSupportedException {
    TimeSeries s1 = new TimeSeries("S1");
    s1.add(new Year(2009), 100.0);
    s1.add(new Year(2010), 101.0);
    s1.add(new Year(2011), 102.0);
    assertEquals(100.0, s1.getMinY(), EPSILON);
    assertEquals(102.0, s1.getMaxY(), EPSILON);

    TimeSeries s2 = s1.createCopy(0, 1);
    assertEquals(100.0, s2.getMinY(), EPSILON);
    assertEquals(101.0, s2.getMaxY(), EPSILON);

    TimeSeries s3 = s1.createCopy(1, 2);
    assertEquals(101.0, s3.getMinY(), EPSILON);
    assertEquals(102.0, s3.getMaxY(), EPSILON);
}

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

/**
 * Creates a new {@link TimeSeries} containing moving average values for
 * the given series.  If the series is empty (contains zero items), the
 * result is an empty series./*from   ww  w .j  a  va  2 s .  c  om*/
 *
 * @param source  the source series.
 * @param name  the name of the new series.
 * @param periodCount  the number of periods used in the average
 *                     calculation.
 * @param skip  the number of initial periods to skip.
 *
 * @return The moving average series.
 */
public static TimeSeries createMovingAverage(TimeSeries source, String name, int periodCount, int skip) {

    ParamChecks.nullNotPermitted(source, "source");
    if (periodCount < 1) {
        throw new IllegalArgumentException("periodCount must be greater " + "than or equal to 1.");
    }

    TimeSeries result = new TimeSeries(name);

    if (source.getItemCount() > 0) {

        // if the initial averaging period is to be excluded, then
        // calculate the index of the
        // first data item to have an average calculated...
        long firstSerial = source.getTimePeriod(0).getSerialIndex() + skip;

        for (int i = source.getItemCount() - 1; i >= 0; i--) {

            // get the current data item...
            RegularTimePeriod period = source.getTimePeriod(i);
            long serial = period.getSerialIndex();

            if (serial >= firstSerial) {
                // work out the average for the earlier values...
                int n = 0;
                double sum = 0.0;
                long serialLimit = period.getSerialIndex() - periodCount;
                int offset = 0;
                boolean finished = false;

                while ((offset < periodCount) && (!finished)) {
                    if ((i - offset) >= 0) {
                        TimeSeriesDataItem item = source.getRawDataItem(i - offset);
                        RegularTimePeriod p = item.getPeriod();
                        Number v = item.getValue();
                        long currentIndex = p.getSerialIndex();
                        if (currentIndex > serialLimit) {
                            if (v != null) {
                                sum = sum + v.doubleValue();
                                n = n + 1;
                            }
                        } else {
                            finished = true;
                        }
                    }
                    offset = offset + 1;
                }
                if (n > 0) {
                    result.add(period, sum / n);
                } else {
                    result.add(period, null);
                }
            }

        }
    }

    return result;

}

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

/**
 * Another test of the clone() method./*from   w w  w  .  j  a  v  a 2  s.c  om*/
 */
@Test
public void testClone2() throws CloneNotSupportedException {
    TimeSeries s1 = new TimeSeries("S1", Year.class);
    s1.add(new Year(2007), 100.0);
    s1.add(new Year(2008), null);
    s1.add(new Year(2009), 200.0);
    TimeSeries s2 = (TimeSeries) s1.clone();
    assertTrue(s1.equals(s2));

    // check independence
    s2.addOrUpdate(new Year(2009), 300.0);
    assertFalse(s1.equals(s2));
    s1.addOrUpdate(new Year(2009), 300.0);
    assertTrue(s1.equals(s2));
}

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

/**
 * Test the setMaximumItemCount() method to ensure that it removes items
 * from the series if necessary.//  w w w . ja v a  2s .c om
 */
@Test
public void testSetMaximumItemCount() {
    TimeSeries s1 = new TimeSeries("S1", Year.class);
    s1.add(new Year(2000), 13.75);
    s1.add(new Year(2001), 11.90);
    s1.add(new Year(2002), null);
    s1.add(new Year(2005), 19.32);
    s1.add(new Year(2007), 16.89);
    assertTrue(s1.getItemCount() == 5);

    s1.setMaximumItemCount(3);
    assertTrue(s1.getItemCount() == 3);
    TimeSeriesDataItem item = s1.getDataItem(0);
    assertTrue(item.getPeriod().equals(new Year(2002)));
    assertEquals(16.89, s1.getMinY(), EPSILON);
    assertEquals(19.32, s1.getMaxY(), EPSILON);
}

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

/**
 * Basic tests for the delete() method.//w w  w  . j a  v  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.TimeSeriesTest.java

/**
 * Check that the item bounds are determined correctly when there is a
 * maximum item count./*from w ww.  j a  v a 2s .  c  o  m*/
 */
@Test
public void testRemoveAgedItems4() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.setMaximumItemAge(2);
    s1.add(new Year(2010), 1.1);
    s1.add(new Year(2011), 2.2);
    s1.add(new Year(2012), 3.3);
    s1.add(new Year(2013), 2.5);
    assertEquals(3, s1.getItemCount());
    assertEquals(2.2, s1.getMinY(), EPSILON);
    assertEquals(3.3, s1.getMaxY(), EPSILON);
}