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

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

Introduction

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

Prototype

public TimeSeriesDataItem getDataItem(RegularTimePeriod period) 

Source Link

Document

Returns the data item for a specific period.

Usage

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

/**
 * Some checks for the getDataItem(RegularTimePeriod) method.
 *///from   ww w  .  j  av a2s.  c o m
public void testGetDataItem2() {
    TimeSeries series = new TimeSeries("S");
    assertNull(series.getDataItem(new Year(2006)));

    // try a null argument
    boolean pass = false;
    try {
        /* TimeSeriesDataItem item = */ series.getDataItem(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private int getDataIndex(TimeSeries timeSeries, Day targetDay) {
    int low = 0;//from   w  ww. j  a v a2s  .c o m
    int high = timeSeries.getItemCount() - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;

        final TimeSeriesDataItem timeSeriesDataItem = timeSeries.getDataItem(mid);
        final Day searchDay = (Day) timeSeriesDataItem.getPeriod();
        final long cmp = searchDay.compareTo(targetDay);

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        } else {
            return mid;
        }
    }
    return -1;
}

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

/**
 * Create a TimeSeriesDataItem, add it to a TimeSeries.  Now, modifying
 * the original TimeSeriesDataItem should NOT affect the TimeSeries.
 *///  w w w.j a  v  a  2  s  .  c o m
public void testAdd_TimeSeriesDataItem() {
    TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2009), 1.0);
    TimeSeries series = new TimeSeries("S1");
    series.add(item);
    assertTrue(item.equals(series.getDataItem(0)));
    item.setValue(new Double(99.9));
    assertFalse(item.equals(series.getDataItem(0)));
}

From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java

private Number getValue(int plotIndex, int seriesIndex, int dataIndex) {
    final TimeSeries timeSeries = this.getTimeSeries(plotIndex, seriesIndex);
    if (timeSeries == null) {
        return null;
    }//from w  w  w . j a v  a2s  .  c  om

    if (dataIndex >= timeSeries.getItemCount()) {
        /* Not ready yet. */
        return null;
    }

    return timeSeries.getDataItem(dataIndex).getValue();
}

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

/**
 * Some checks for the getDataItem(RegularTimePeriod) method.
 *///  w  ww.ja  va 2 s . c  om
@Test
public void testGetDataItem2() {
    TimeSeries series = new TimeSeries("S", Year.class);
    assertNull(series.getDataItem(new Year(2006)));

    // try a null argument
    boolean pass = false;
    try {
        /* TimeSeriesDataItem item = */ series.getDataItem(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Create a TimeSeriesDataItem, add it to a TimeSeries.  Now, modifying
 * the original TimeSeriesDataItem should NOT affect the TimeSeries.
 *///  w  w  w.ja v  a2  s.c  om
@Test
public void testAdd_TimeSeriesDataItem() {
    TimeSeriesDataItem item = new TimeSeriesDataItem(new Year(2009), 1.0);
    TimeSeries series = new TimeSeries("S1");
    series.add(item);
    assertTrue(item.equals(series.getDataItem(0)));
    item.setValue(new Double(99.9));
    assertFalse(item.equals(series.getDataItem(0)));
}

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

/**
 * Some checks for the getDataItem(int) method.
 *///from w w w  .  jav  a 2  s .co  m
public void testGetDataItem1() {
    TimeSeries series = new TimeSeries("S");

    // can't get anything yet...just an exception
    boolean pass = false;
    try {
        /*TimeSeriesDataItem item =*/ series.getDataItem(0);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    series.add(new Year(2006), 100.0);
    TimeSeriesDataItem item = series.getDataItem(0);
    assertEquals(new Year(2006), item.getPeriod());
    pass = false;
    try {
        /*item = */series.getDataItem(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    pass = false;
    try {
        /*item = */series.getDataItem(1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Some checks for the getDataItem(int) method.
 *//*from   w  w w .ja v a  2  s.  co  m*/
@Test
public void testGetDataItem1() {
    TimeSeries series = new TimeSeries("S", Year.class);

    // can't get anything yet...just an exception
    boolean pass = false;
    try {
        /*TimeSeriesDataItem item =*/ series.getDataItem(0);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    series.add(new Year(2006), 100.0);
    TimeSeriesDataItem item = series.getDataItem(0);
    assertEquals(new Year(2006), item.getPeriod());
    pass = false;
    try {
        /*item = */series.getDataItem(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    pass = false;
    try {
        /*item = */series.getDataItem(1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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 ww  w. j  a va 2s.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.junit.TimeSeriesTest.java

/**
 * Test the setMaximumItemCount() method to ensure that it removes items
 * from the series if necessary./*w  ww  . j  a v a  2  s . c om*/
 */
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);
}