Java Code Examples for org.jfree.data.time.TimeSeries#getDataItem()

The following examples show how to use org.jfree.data.time.TimeSeries#getDataItem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSeriesStatistics.java    From incubator-iotdb with Apache License 2.0 6 votes vote down vote up
TimeSeriesStatistics(TimeSeries timeSeries) {
  Date lastDate = null;
  name = (String) timeSeries.getKey();
  for (int i = 0; i < timeSeries.getItemCount(); i++) {
    TimeSeriesDataItem dataItem = timeSeries.getDataItem(i);
    Date currDate = dataItem.getPeriod().getStart();
    double value = dataItem.getValue().doubleValue();
    if (lastDate == null) {
      lastDate = currDate;
    } else {
      long interval = currDate.getTime() - lastDate.getTime();
      lastDate = currDate;
      meanInterval = (meanInterval * size + interval) / (size + 1);
      maxInterval = maxInterval < interval ? interval : maxInterval;
      minInterval = minInterval < interval ? minInterval : interval;
    }
    meanVal = (meanVal * size + value) / (size + 1);
    maxVal = maxVal < value ? value : maxVal;
    minVal = minVal < value ? minVal : value;
    valSum += value;
    size ++;
  }
}
 
Example 2
Source File: TimeSeriesBuilderCallbackTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddPointToSeries()
{
    TimeSeriesHolder timeSeriesHolder = new TimeSeriesHolder();

    SeriesDefinition seriesDefinition = mock(SeriesDefinition.class);
    when(seriesDefinition.getSeriesLegend()).thenReturn(SERIES_LEGEND);

    timeSeriesHolder.beginSeries(seriesDefinition);

    timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[0], _values[0]));
    timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[1], _values[1]));
    timeSeriesHolder.addDataPointToSeries(seriesDefinition, new SeriesRow(_dates[2], _values[2]));

    timeSeriesHolder.endSeries(seriesDefinition);

    TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) timeSeriesHolder.getPopulatedDataset();

    TimeSeries actualTimeSeries = timeSeriesCollection.getSeries(SERIES_LEGEND);
    for(int i = 0; i < NUMBER_OF_DATA_POINTS; i++)
    {
        TimeSeriesDataItem dataItem0 = actualTimeSeries.getDataItem(i);
        assertEquals(_dates[i].getTime(), dataItem0.getPeriod().getMiddleMillisecond());
        assertEquals(_values[i], dataItem0.getValue());
    }
}
 
Example 3
Source File: ExponentialSmoothingChartDemo.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * A helper function to convert data points (from startIndex to
 * endIndex) of a (JFreeChart) TimeSeries object into an
 * OpenForecast DataSet.
 * @param series the series of data points stored as a JFreeChart
 * TimeSeries object.
 * @param startIndex the index of the first data point required from the
 * series.
 * @param endIndex the index of the last data point required from the
 * series.
 * @return an OpenForecast DataSet representing the data points extracted
 * from the TimeSeries.
 */
private DataSet getDataSet( TimeSeries series,
                            int startIndex, int endIndex )
{
    DataSet dataSet = new DataSet();
    if ( endIndex > series.getItemCount() )
        endIndex = series.getItemCount();
    
    for ( int i=startIndex; i<endIndex; i++ )
        {
            TimeSeriesDataItem dataPair = series.getDataItem(i);
            DataPoint dp = new Observation( dataPair.getValue().doubleValue() );
            dp.setIndependentValue( "t", i );
            dataSet.add( dp );
        }
    
    return dataSet;
}
 
Example 4
Source File: ForecastingChartDemo.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * A helper function to convert data points (from startIndex to
 * endIndex) of a (JFreeChart) TimeSeries object into an
 * OpenForecast DataSet.
 * @param series the series of data points stored as a JFreeChart
 * TimeSeries object.
 * @param startIndex the index of the first data point required from the
 * series.
 * @param endIndex the index of the last data point required from the
 * series.
 * @return an OpenForecast DataSet representing the data points extracted
 * from the TimeSeries.
 */
private DataSet getDataSet( TimeSeries series,
                            int startIndex, int endIndex )
{
    DataSet dataSet = new DataSet();
    if ( endIndex > series.getItemCount() )
        endIndex = series.getItemCount();
    
    for ( int i=startIndex; i<endIndex; i++ )
        {
            TimeSeriesDataItem dataPair = series.getDataItem(i);
            DataPoint dp = new Observation( dataPair.getValue().doubleValue() );
            dp.setIndependentValue( "t", i );
            dataSet.add( dp );
        }
    
    return dataSet;
}
 
Example 5
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the setMaximumItemCount() method to ensure that it removes items
 * from the series if necessary.
 */
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);
}
 
Example 6
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the setMaximumItemCount() method to ensure that it removes items 
 * from the series if necessary.
 */
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)));

}
 
Example 7
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Some checks for the getDataItem(RegularTimePeriod) method.
 */
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);
}
 
Example 8
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Some checks for the getDataItem(RegularTimePeriod) method.
 */
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);
}
 
Example 9
Source File: TimeSeriesValidator.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public TimeSeries validate(TimeSeries timeSeries, String sourceName, TimeSeriesType type) throws ParseException {
    String qualifiedSourceName = createQualifiedSourcename(sourceName, type);
    final Symbol symbol = namespace.resolveSymbol(qualifiedSourceName);
    if (symbol == null) {
        throw new ParseException("No variable for identifier '" + qualifiedSourceName + "' registered.");
    }
    final String expression = getExpressionFor(qualifiedSourceName);
    if (expression == null || expression.trim().isEmpty()) {
        return timeSeries;
    }
    final Variable variable = (Variable) symbol;
    final Term term = parser.parse(expression, namespace);

    final int seriesCount = timeSeries.getItemCount();
    final TimeSeries validatedSeries = new TimeSeries(timeSeries.getKey());
    for (int i = 0; i < seriesCount; i++) {
        final TimeSeriesDataItem dataItem = timeSeries.getDataItem(i);
        final Number value = dataItem.getValue();
        variable.assignD(null, value.doubleValue());
        if (term.evalB(null)) {
            validatedSeries.add(dataItem);
        }
    }

    return validatedSeries;
}
 
Example 10
Source File: XYChartExpression.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected ExtTimeTableXYDataset convertToTable( final XYDataset xyDataset ) {
  final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) xyDataset;
  final ExtTimeTableXYDataset tableXYDataset = new ExtTimeTableXYDataset();
  final int count = timeSeriesCollection.getSeriesCount();
  for ( int i = 0; i < count; i++ ) {
    final Comparable key = timeSeriesCollection.getSeriesKey( i );
    final TimeSeries timeSeries = timeSeriesCollection.getSeries( i );
    final int itemCount = timeSeries.getItemCount();
    for ( int ic = 0; ic < itemCount; ic++ ) {
      final TimeSeriesDataItem seriesDataItem = timeSeries.getDataItem( ic );
      tableXYDataset.add( seriesDataItem.getPeriod(), seriesDataItem.getValue(), key, false );
    }
  }
  return tableXYDataset;
}