org.jfree.data.time.TimeSeriesDataItem Java Examples

The following examples show how to use org.jfree.data.time.TimeSeriesDataItem. 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: TimeSeriesDataItemTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the equals() method.
 */
public void testEquals() {
    TimeSeriesDataItem item1 = new TimeSeriesDataItem(
        new Day(23, 9, 2001), 99.7
    );
    TimeSeriesDataItem item2 = new TimeSeriesDataItem(
        new Day(23, 9, 2001), 99.7
    );
    assertTrue(item1.equals(item2));
    assertTrue(item2.equals(item1));

    item1.setValue(new Integer(5));
    assertFalse(item1.equals(item2));
    item2.setValue(new Integer(5));
    assertTrue(item1.equals(item2));
}
 
Example #3
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 #4
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Some more checks for the addOrUpdate() method.
 */
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)));
}
 
Example #5
Source File: TimeSeriesDataItemTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test the equals() method.
 */
public void testEquals() {
    TimeSeriesDataItem item1 = new TimeSeriesDataItem(
        new Day(23, 9, 2001), 99.7
    );
    TimeSeriesDataItem item2 = new TimeSeriesDataItem(
        new Day(23, 9, 2001), 99.7
    );
    assertTrue(item1.equals(item2));
    assertTrue(item2.equals(item1));
    
    item1.setValue(new Integer(5));
    assertFalse(item1.equals(item2));
    item2.setValue(new Integer(5));
    assertTrue(item1.equals(item2));
}
 
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: TimeSeriesGraphUpdater.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
private TimeSeries computeSingleTimeSeries(final List<Band> bandList, int pixelX, int pixelY, int currentLevel, String positionName) {
    final Band firstBand = bandList.get(0);
    final String firstBandName = firstBand.getName();
    final int lastUnderscore = firstBandName.lastIndexOf("_");
    final String suffix = positionName.isEmpty()?positionName: "_" + positionName;
    final String timeSeriesName = firstBandName.substring(0, lastUnderscore);
    final TimeSeries timeSeries = new TimeSeries(timeSeriesName + suffix);
    for (Band band : bandList) {
        final TimeCoding timeCoding = this.timeSeries.getRasterTimeMap().get(band);
        if (timeCoding != null) {
            final ProductData.UTC startTime = timeCoding.getStartTime();
            final Millisecond timePeriod = new Millisecond(startTime.getAsDate(),
                                                           ProductData.UTC.UTC_TIME_ZONE,
                                                           Locale.getDefault());
            final double value = getValue(band, pixelX, pixelY, currentLevel);
            timeSeries.add(new TimeSeriesDataItem(timePeriod, value));
        }
    }
    return timeSeries;
}
 
Example #8
Source File: TimeSeriesOutputter.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Outputs the given DataPoint to the current TimeSeries.
 * @param dataPoint the DataPoint to output to the current TimeSeries.
 */
private void output( DataPoint dataPoint, String timeVariable )
    throws InstantiationException, IllegalAccessException,
    InvocationTargetException, InstantiationException
{
    long timeValue = (long)dataPoint.getIndependentValue(timeVariable);
    
    Object[] args = new Object[1];
    args[0] = new Date( timeValue );
    RegularTimePeriod period
        = (RegularTimePeriod)timePeriodConstructor.newInstance(args);
    
    double value = dataPoint.getDependentValue();
    
    timeSeries.add( new TimeSeriesDataItem(period,value) );
}
 
Example #9
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 #10
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 #11
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 #12
Source File: TimeSeriesDataItemTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test that an instance is equal to itself.
 *
 * SourceForge Bug ID: 558850.
 */
public void testEqualsSelf() {
    TimeSeriesDataItem item = new TimeSeriesDataItem(
        new Day(23, 9, 2001), 99.7
    );
    assertTrue(item.equals(item));
}
 
Example #13
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a TimeSeriesDataItem, add it to a TimeSeries.  Now, modifying
 * the original TimeSeriesDataItem should NOT affect the TimeSeries.
 */
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)));
}
 
Example #14
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 #15
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;
}
 
Example #16
Source File: PricePanel.java    From hazelcast-jet-demos with Apache License 2.0 5 votes vote down vote up
/**
* <p>Update the panel with a new price on a date, from
* current, 50-point or 200-point averages.
* </p>
* 
* @param name Current, 50 point average or 200 point average
* @param day The day that price occurred
* @param rate In US Dollars, the price of Bitcoin
*/
  public void update(String name, LocalDate localDate, double rate) {
Day day = new Day(localDate.getDayOfMonth(), localDate.getMonthValue(), localDate.getYear());

  	// Add the relevant price into the time series array
  	for (int i=0 ; i < MyConstants.CHART_LINES.length; i++) {
  		if (MyConstants.CHART_LINES[i].equals(name)) {
		this.timeSeries[i].addOrUpdate(new TimeSeriesDataItem(day, rate));
   		}
  	}
  }
 
Example #17
Source File: TimeSeriesDataItemTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test that an instance is equal to itself.
 *
 * SourceForge Bug ID: 558850.
 */
public void testEqualsSelf() {
    TimeSeriesDataItem item = new TimeSeriesDataItem(
        new Day(23, 9, 2001), 99.7
    );
    assertTrue(item.equals(item));
}
 
Example #18
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 5 votes vote down vote up
@Override
public List getItems() {
    return new AbstractList<TimeSeriesDataItem>() {
        @Override
        public TimeSeriesDataItem get(int index) {
            return getDataItem(index);
        }

        @Override
        public int size() {
            return getItemCount();
        }  
    };
}
 
Example #19
Source File: TimeSeriesBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Builds a DataPoint from the given TimeSeriesDataItem. The name used for
 * the independent, time variable can be changed using the setTimeVariable
 * method.
 * @param dataItem the TimeSeriesDataItem from which the values are to be
 * read and used to construct a new DataPoint.
 * @return a DataPoint object with values as specified by the given
 * TimeSeriesDataItem.
 */
private DataPoint build( TimeSeriesDataItem dataItem )
{
    DataPoint dataPoint
        = new Observation( dataItem.getValue().doubleValue() );
    
    // Get time value (at middle of time period)
    double timeValue = dataItem.getPeriod().getMiddleMillisecond();
    
    // Store time value as independent variable
    dataPoint.setIndependentValue( getVariableName(0),
                                   timeValue );
    
    return dataPoint;
}
 
Example #20
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public TimeSeriesDataItem getDataItem(RegularTimePeriod period) {
    return new TimeSeriesDataItem(period, series.getData().get(getIndex(period)));
}
 
Example #21
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public TimeSeriesDataItem getDataItem(int index) {
    return new TimeSeriesDataItem(getTimePeriod(index), series.getData().get(index));
}
 
Example #22
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public TimeSeriesDataItem addOrUpdate(TimeSeriesDataItem arg0) {
    throw new UnsupportedOperationException();
}
 
Example #23
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period, Number value) {
    throw new UnsupportedOperationException();
}
 
Example #24
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public TimeSeriesDataItem addOrUpdate(RegularTimePeriod period, double value) {
    throw new UnsupportedOperationException();
}
 
Example #25
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public void add(TimeSeriesDataItem item) {
    throw new UnsupportedOperationException();
}
 
Example #26
Source File: SeriesWrapper.java    From swift-k with Apache License 2.0 4 votes vote down vote up
@Override
public void add(TimeSeriesDataItem arg0, boolean arg1) {
    throw new UnsupportedOperationException();
}
 
Example #27
Source File: SparkLine.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DatasetMap calculateValue() throws Exception {
	logger.debug("IN");

	DatasetMap datasets = super.calculateValue();
	if(datasets==null || yearsDefined==null){
		logger.error("Error in TrargetCharts calculate value");
		return null;
	}

	TimeSeriesCollection dataset = new TimeSeriesCollection(); 
	if(datasets!=null && yearsDefined.isEmpty()){
		logger.warn("no rows found with dataset");
	}
	else{

		int itemCount = timeSeries.getItemCount();		
		// this is the main time series, to be linked with Line and shape
		dataset.addSeries(timeSeries);

		// Check if defining target and baseline
		Double mainTarget=null;
		Double mainBaseline=null;
		if(useTargets)mainTarget=mainThreshold;
		else mainBaseline=mainThreshold;

		// run all the years defined
		lastIndexMonth = 1;
		for (Iterator iterator = yearsDefined.iterator(); iterator.hasNext();) {
			String currentYearS = (String) iterator.next();
			int currentYear=Integer.valueOf(currentYearS).intValue();
			// get the last in l
			for(int i = 1; i < 13; i++) {
				TimeSeriesDataItem item = timeSeries.getDataItem(new Month(i, currentYear));
				if(item == null || item.getValue() == null) {
					//timeSeries.addOrUpdate(new Month(i, currentYear), null);
				} else {
					lastIndexMonth = i;
				}
			}
		}
	}
	datasets.addDataset("1",dataset);

	logger.debug("OUT");
	return datasets;
}
 
Example #28
Source File: WinLose.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DatasetMap calculateValue() throws Exception {
	logger.debug("IN");

	DatasetMap datasets = super.calculateValue();
	if(datasets==null || yearsDefined==null){
		logger.error("Error in TrargetCharts calculate value");
		return null;
	}
	DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 		
	datasets.addDataset("1",dataset);

	if(datasets!=null && yearsDefined.isEmpty()){
		logger.warn("no rows found with dataset");
	}
	else{

		// Check if defining target and baseline
		Double mainTarget=null;
		Double mainBaseline=null;
		if(mainThreshold==null){
			logger.error("No main target or baseline defined, not possible to draw the chart");
		}
		else{
			if(useTargets)mainTarget=mainThreshold;
			else mainBaseline=mainThreshold;
			nullValues=new Vector<String>();

			// run all the years defined
			for (Iterator iterator = yearsDefined.iterator(); iterator.hasNext();) {
				String currentYearS = (String) iterator.next();
				int currentYear=Integer.valueOf(currentYearS).intValue();
				boolean stop=false;
				for(int i = 1; i < 13 && stop==false; i++) {
					Month currentMonth=new Month(i,currentYear);
					// if it is the first year and th ecurrent month is previous than the first month
					if(currentYearS.equalsIgnoreCase(yearsDefined.first()) && i<firstMonth.getMonth()){
						// continue
					}
					else{
						TimeSeriesDataItem item = timeSeries.getDataItem(currentMonth);
						if(item != null && item.getValue() != null) {
							double v = item.getValue().doubleValue();
							double result = 0;
							if(mainTarget != null) {
								result = (v >= mainTarget.doubleValue())?WIN:LOSE;
							} else if(mainBaseline != null) {
								result = (v > mainBaseline.doubleValue())?LOSE:WIN;
							}
							else{
								logger.warn("could not find a threshold");
							}

							dataset.addValue(result, timeSeries.getKey(), "" + i+"-"+currentYear);   

						} else {
							if(wlt_mode.doubleValue() == 5){
								dataset.addValue(0.001, timeSeries.getKey(), "" + i+"-"+currentYear); 
								nullValues.add("" + i+"-"+currentYear);
							}
							else{
								dataset.addValue(0.0, timeSeries.getKey(), "" + i+"-"+currentYear); 							
							}
						}
						// if it is last year and current month is after the last month stop 
						if(currentYearS.equalsIgnoreCase(lastYear) && i>=lastMonth.getMonth()){
							stop=true;
						}

					} 
				}

			}
		}
	}
	logger.debug("OUT");
	return datasets;
}