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

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

Introduction

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

Prototype

@Override
public int getItemCount() 

Source Link

Document

Returns the number of items in the series.

Usage

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

/**
 * Some tests to ensure that the createCopy(RegularTimePeriod,
 * RegularTimePeriod) method is functioning correctly.
 *///from   www .j  ava2 s .co  m
public void testCreateCopy1() {

    TimeSeries series = new TimeSeries("Series");
    series.add(new Month(MonthConstants.JANUARY, 2003), 45.0);
    series.add(new Month(MonthConstants.FEBRUARY, 2003), 55.0);
    series.add(new Month(MonthConstants.JUNE, 2003), 35.0);
    series.add(new Month(MonthConstants.NOVEMBER, 2003), 85.0);
    series.add(new Month(MonthConstants.DECEMBER, 2003), 75.0);

    try {
        // copy a range before the start of the series data...
        TimeSeries result1 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
                new Month(MonthConstants.DECEMBER, 2002));
        assertEquals(0, result1.getItemCount());

        // copy a range that includes only the first item in the series...
        TimeSeries result2 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
                new Month(MonthConstants.JANUARY, 2003));
        assertEquals(1, result2.getItemCount());

        // copy a range that begins before and ends in the middle of the
        // series...
        TimeSeries result3 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
                new Month(MonthConstants.APRIL, 2003));
        assertEquals(2, result3.getItemCount());

        TimeSeries result4 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
                new Month(MonthConstants.DECEMBER, 2003));
        assertEquals(5, result4.getItemCount());

        TimeSeries result5 = series.createCopy(new Month(MonthConstants.NOVEMBER, 2002),
                new Month(MonthConstants.MARCH, 2004));
        assertEquals(5, result5.getItemCount());

        TimeSeries result6 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
                new Month(MonthConstants.JANUARY, 2003));
        assertEquals(1, result6.getItemCount());

        TimeSeries result7 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
                new Month(MonthConstants.APRIL, 2003));
        assertEquals(2, result7.getItemCount());

        TimeSeries result8 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
                new Month(MonthConstants.DECEMBER, 2003));
        assertEquals(5, result8.getItemCount());

        TimeSeries result9 = series.createCopy(new Month(MonthConstants.JANUARY, 2003),
                new Month(MonthConstants.MARCH, 2004));
        assertEquals(5, result9.getItemCount());

        TimeSeries result10 = series.createCopy(new Month(MonthConstants.MAY, 2003),
                new Month(MonthConstants.DECEMBER, 2003));
        assertEquals(3, result10.getItemCount());

        TimeSeries result11 = series.createCopy(new Month(MonthConstants.MAY, 2003),
                new Month(MonthConstants.MARCH, 2004));
        assertEquals(3, result11.getItemCount());

        TimeSeries result12 = series.createCopy(new Month(MonthConstants.DECEMBER, 2003),
                new Month(MonthConstants.DECEMBER, 2003));
        assertEquals(1, result12.getItemCount());

        TimeSeries result13 = series.createCopy(new Month(MonthConstants.DECEMBER, 2003),
                new Month(MonthConstants.MARCH, 2004));
        assertEquals(1, result13.getItemCount());

        TimeSeries result14 = series.createCopy(new Month(MonthConstants.JANUARY, 2004),
                new Month(MonthConstants.MARCH, 2004));
        assertEquals(0, result14.getItemCount());
    } catch (CloneNotSupportedException e) {
        assertTrue(false);
    }

}

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

/**
 * Some checks for the addOrUpdate() method.
 *//*from w w w . j  a v  a  2 s . co m*/
@Test
public void testAddOrUpdate() {
    TimeSeries s1 = new TimeSeries("S1", Year.class);
    s1.setMaximumItemCount(2);
    s1.addOrUpdate(new Year(2000), 100.0);
    assertEquals(1, s1.getItemCount());
    s1.addOrUpdate(new Year(2001), 101.0);
    assertEquals(2, s1.getItemCount());
    s1.addOrUpdate(new Year(2001), 102.0);
    assertEquals(2, s1.getItemCount());
    s1.addOrUpdate(new Year(2002), 103.0);
    assertEquals(2, s1.getItemCount());
}

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

/**
 * Check that the item bounds are determined correctly when there is a
 * maximum item count./*from  ww  w .  jav a 2  s. c om*/
 */
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);
}

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

/**
 * Test the setMaximumItemCount() method to ensure that it removes items
 * from the series if necessary.// w w w . j  a  va2s . c  o  m
 */
public void testSetMaximumItemCount() {
    TimeSeries s1 = new TimeSeries("S1");
    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.jmxtrans.samples.graphite.GraphiteDataInjector.java

public void exportMetrics(TimeSeries timeSeries) throws IOException {
    System.out.println("Export " + timeSeries.getKey());
    Socket socket = new Socket(graphiteHost, graphitePort);
    OutputStream outputStream = socket.getOutputStream();

    if (generateDataPointsFile) {
        JFreeChart chart = ChartFactory.createXYLineChart("Purchase", "date", "Amount",
                new TimeSeriesCollection(timeSeries), PlotOrientation.VERTICAL, true, true, false);
        // chart.getXYPlot().setRenderer(new XYSplineRenderer(60));

        File file = new File("/tmp/" + timeSeries.getKey() + ".png");
        ChartUtilities.saveChartAsPNG(file, chart, 1200, 800);
        System.out.println("Exported " + file.getAbsolutePath());

        String pickleFileName = "/tmp/" + timeSeries.getKey().toString() + ".pickle";
        System.out.println("Generate " + pickleFileName);
        outputStream = new TeeOutputStream(outputStream, new FileOutputStream(pickleFileName));
    }//from w ww .  j a va 2  s .  c  om

    PyList list = new PyList();

    for (int i = 0; i < timeSeries.getItemCount(); i++) {
        if (debug)
            System.out.println(new DateTime(timeSeries.getDataItem(i).getPeriod().getStart()) + "\t"
                    + timeSeries.getDataItem(i).getValue().intValue());
        String metricName = graphiteMetricPrefix + timeSeries.getKey().toString();
        int time = (int) TimeUnit.SECONDS.convert(timeSeries.getDataItem(i).getPeriod().getStart().getTime(),
                TimeUnit.MILLISECONDS);
        int value = timeSeries.getDataItem(i).getValue().intValue();

        list.add(new PyTuple(new PyString(metricName), new PyTuple(new PyInteger(time), new PyInteger(value))));

        if (list.size() >= batchSize) {
            System.out.print("-");
            rateLimiter.acquire(list.size());
            sendDataPoints(outputStream, list);
        }
    }

    // send last data points
    if (!list.isEmpty()) {
        rateLimiter.acquire(list.size());
        sendDataPoints(outputStream, list);
    }

    Flushables.flushQuietly(outputStream);
    Closeables.close(outputStream, true);
    Closeables.close(socket, true);

    System.out.println();
    System.out.println("Exported " + timeSeries.getKey() + ": " + timeSeries.getItemCount() + " items");
}

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

/**
 * Check that the item bounds are determined correctly when there is a
 * maximum item count and a new value is added.
 *///  w  ww  .j  av  a 2s  . com
@Test
public void testAdd() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.setMaximumItemCount(2);
    s1.add(new Year(2010), 1.1);
    s1.add(new Year(2011), 2.2);
    s1.add(new Year(2012), 3.3);
    assertEquals(2, s1.getItemCount());
    assertEquals(2.2, s1.getMinY(), EPSILON);
    assertEquals(3.3, s1.getMaxY(), EPSILON);
}

From source file:org.jmxtrans.embedded.samples.graphite.GraphiteDataInjector.java

public void exportMetrics(TimeSeries timeSeries) throws IOException {
    System.out.println("Export '" + timeSeries.getKey() + "' to " + graphiteHost + " with prefix '"
            + graphiteMetricPrefix + "'");
    Socket socket = new Socket(graphiteHost, graphitePort);
    OutputStream outputStream = socket.getOutputStream();

    if (generateDataPointsFile) {
        JFreeChart chart = ChartFactory.createXYLineChart("Purchase", "date", "Amount",
                new TimeSeriesCollection(timeSeries), PlotOrientation.VERTICAL, true, true, false);
        // chart.getXYPlot().setRenderer(new XYSplineRenderer(60));

        File file = new File("/tmp/" + timeSeries.getKey() + ".png");
        ChartUtilities.saveChartAsPNG(file, chart, 1200, 800);
        System.out.println("Exported " + file.getAbsolutePath());

        String pickleFileName = "/tmp/" + timeSeries.getKey().toString() + ".pickle";
        System.out.println("Generate " + pickleFileName);
        outputStream = new TeeOutputStream(outputStream, new FileOutputStream(pickleFileName));
    }/*from ww w.j  a  va 2s.c  o m*/

    PyList list = new PyList();

    for (int i = 0; i < timeSeries.getItemCount(); i++) {
        if (debug)
            System.out.println(new DateTime(timeSeries.getDataItem(i).getPeriod().getStart()) + "\t"
                    + timeSeries.getDataItem(i).getValue().intValue());
        String metricName = graphiteMetricPrefix + timeSeries.getKey().toString();
        int time = (int) TimeUnit.SECONDS.convert(timeSeries.getDataItem(i).getPeriod().getStart().getTime(),
                TimeUnit.MILLISECONDS);
        int value = timeSeries.getDataItem(i).getValue().intValue();

        list.add(new PyTuple(new PyString(metricName), new PyTuple(new PyInteger(time), new PyInteger(value))));

        if (list.size() >= batchSize) {
            System.out.print("-");
            rateLimiter.acquire(list.size());
            sendDataPoints(outputStream, list);
        }
    }

    // send last data points
    if (!list.isEmpty()) {
        rateLimiter.acquire(list.size());
        sendDataPoints(outputStream, list);
    }

    Flushables.flushQuietly(outputStream);
    Closeables.close(outputStream, true);
    try {
        socket.close();
    } catch (Exception e) {
        // swallow exception
        e.printStackTrace();
    }

    System.out.println();
    System.out.println("Exported " + timeSeries.getKey() + ": " + timeSeries.getItemCount() + " items");
}

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

/**
 * Some checks for the delete(int, int) method.
 *//*w w  w.j  ava  2s . c om*/
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

/**
 * Test the add branch of the addOrUpdate() method.
 *///  ww w  .  j a va 2  s  .  c  o m
@Test
public void testAddOrUpdate2() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.setMaximumItemCount(2);
    s1.addOrUpdate(new Year(2010), 1.1);
    s1.addOrUpdate(new Year(2011), 2.2);
    s1.addOrUpdate(new Year(2012), 3.3);
    assertEquals(2, s1.getItemCount());
    assertEquals(2.2, s1.getMinY(), EPSILON);
    assertEquals(3.3, s1.getMaxY(), EPSILON);
}

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

/**
 * Check that the item bounds are determined correctly after a call to
 * removeAgedItems()./*from  w w  w  .  j  a v  a  2 s  . co  m*/
 */
public void testRemoveAgedItems5() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.setMaximumItemAge(4);
    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);
    s1.removeAgedItems(new Year(2015).getMiddleMillisecond(), true);
    assertEquals(3, s1.getItemCount());
    assertEquals(2.2, s1.getMinY(), EPSILON);
    assertEquals(3.3, s1.getMaxY(), EPSILON);
}