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

The following examples show how to use org.jfree.data.time.TimeSeries#addOrUpdate() . 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: OffspringChartComposite.java    From offspring with MIT License 6 votes vote down vote up
private IntervalXYDataset createVolumeDataset() {
  List<IRate> rates = rateProvider.getRates(base, quote);
  TimeSeries series = new TimeSeries("Volume");
  boolean non_zero_found = false;
  if (rates != null) {
    for (IRate rate : rates) {
      Date date = new Date(rate.getTimestamp());
      double vol = rate.getTotalVol();
      series.addOrUpdate(new Hour(date), vol);
      if (vol > 0)
        non_zero_found = true;
    }
  }
  if (non_zero_found == false)
    return null;

  TimeSeriesCollection dataset = new TimeSeriesCollection(series);
  dataset.setDomainIsPointsInTime(true);
  return dataset;
}
 
Example 2
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 3
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
  * Some checks for the getMinY() method.
  */
 public void testGetMinY() {
     TimeSeries s1 = new TimeSeries("S1");
     assertTrue(Double.isNaN(s1.getMinY()));

     s1.add(new Year(2008), 1.1);
     assertEquals(1.1, s1.getMinY(), EPSILON);

     s1.add(new Year(2009), 2.2);
     assertEquals(1.1, s1.getMinY(), EPSILON);

     s1.add(new Year(2000), 99.9);
     assertEquals(1.1, s1.getMinY(), EPSILON);

     s1.add(new Year(2002), -1.1);
     assertEquals(-1.1, s1.getMinY(), EPSILON);

     s1.add(new Year(2003), null);
     assertEquals(-1.1, s1.getMinY(), EPSILON);

     s1.addOrUpdate(new Year(2002), null);
     assertEquals(1.1, s1.getMinY(), EPSILON);
}
 
Example 4
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Some checks for the getMaxY() method.
 */
public void testGetMaxY() {
    TimeSeries s1 = new TimeSeries("S1");
    assertTrue(Double.isNaN(s1.getMaxY()));

    s1.add(new Year(2008), 1.1);
    assertEquals(1.1, s1.getMaxY(), EPSILON);

    s1.add(new Year(2009), 2.2);
    assertEquals(2.2, s1.getMaxY(), EPSILON);

    s1.add(new Year(2000), 99.9);
    assertEquals(99.9, s1.getMaxY(), EPSILON);

    s1.add(new Year(2002), -1.1);
    assertEquals(99.9, s1.getMaxY(), EPSILON);

    s1.add(new Year(2003), null);
    assertEquals(99.9, s1.getMaxY(), EPSILON);

    s1.addOrUpdate(new Year(2000), null);
    assertEquals(2.2, s1.getMaxY(), EPSILON);
}
 
Example 5
Source File: ChartVirtualSensor.java    From gsn with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method adds the specified stream elements to the timeSeries of the
 * appropriate plot.
 * 
 * @param streamElement
 */
public synchronized void addData ( StreamElement streamElement ) {
   for ( int i = 0 ; i < streamElement.getFieldNames( ).length ; i++ ) {
      TimeSeries timeSeries = dataForTheChart.get( streamElement.getFieldNames( )[ i ] );
      if ( timeSeries == null ) {
         dataForTheChart.put( streamElement.getFieldNames( )[ i ] , timeSeries = new TimeSeries( streamElement.getFieldNames( )[ i ] , org.jfree.data.time.FixedMillisecond.class ) );
         if(isTimeBased){
         	timeSeries.setMaximumItemAge(historySize);
         }else{
             timeSeries.setMaximumItemCount(historySize);
         }
         dataCollectionForTheChart.addSeries( timeSeries );
      }
      try {
         timeSeries.addOrUpdate( new FixedMillisecond( new Date( streamElement.getTimeStamp( ) ) ) , Double.parseDouble( streamElement.getData( )[ i ].toString( ) ) );
      } catch ( SeriesException e ) {
         logger.warn( e.getMessage( ) , e );
      }
      
   }
   changed = true;
}
 
Example 6
Source File: OffspringChartComposite.java    From offspring with MIT License 5 votes vote down vote up
private XYDataset createPriceDataset() {
  List<IRate> rates = rateProvider.getRates(base, quote);
  TimeSeries series = new TimeSeries("Price");
  if (rates != null) {
    for (IRate rate : rates) {
      Date date = new Date(rate.getTimestamp());
      series.addOrUpdate(new Hour(date), rate.getPrice());
    }
  }
  TimeSeriesCollection dataset = new TimeSeriesCollection(series);
  dataset.setDomainIsPointsInTime(true);
  return dataset;
}
 
Example 7
Source File: AEChartComposite.java    From offspring with MIT License 5 votes vote down vote up
private XYDataset createPriceDataset() {
  TimeSeries series = new TimeSeries("Price");
  List<Trade> trades = Trade.getTrades(asset.getId());
  for (Trade trade : trades) {
    Date date = new Date(((trade.getTimestamp()) * 1000l)
        + (Constants.EPOCH_BEGINNING - 500L));
    series.addOrUpdate(new Minute(date),
        Double.valueOf(trade.getPrice()) / 100);
  }
  TimeSeriesCollection dataset = new TimeSeriesCollection(series);
  dataset.setDomainIsPointsInTime(true);
  return dataset;
}
 
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 addOrUpdate() method.
 */
public void testAddOrUpdate() {
    TimeSeries s1 = new TimeSeries("S1");
    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());
}
 
Example 9
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test the add branch of the addOrUpdate() method.
 */
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);
}
 
Example 10
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test that the addOrUpdate() method won't allow multiple time period
 * classes.
 */
public void testAddOrUpdate3() {
    TimeSeries s1 = new TimeSeries("S1");
    s1.addOrUpdate(new Year(2010), 1.1);
    assertEquals(Year.class, s1.getTimePeriodClass());

    boolean pass = false;
    try {
        s1.addOrUpdate(new Month(1, 2009), 0.0);
    }
    catch (SeriesException e) {
        pass = true;
    }
    assertTrue(pass);
}
 
Example 11
Source File: TimeSeriesTests.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Some checks for the addOrUpdate() method.
 */
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());
}
 
Example 12
Source File: TimeSeriesGraphUpdater.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private TimeSeries computeSingleTimeSeries(InsituRecord[] insituRecords, String insituName) {
    TimeSeries timeSeries = new TimeSeries(insituName);
    for (InsituRecord insituRecord : insituRecords) {
        final ProductData.UTC startTime = ProductData.UTC.create(insituRecord.time, 0);
        final Millisecond timePeriod = new Millisecond(startTime.getAsDate(),
                                                       ProductData.UTC.UTC_TIME_ZONE,
                                                       Locale.getDefault());
        timeSeries.addOrUpdate(timePeriod, insituRecord.value);
    }
    return timeSeries;
}
 
Example 13
Source File: GsnChartJfreechart.java    From gsn with GNU General Public License v3.0 5 votes vote down vote up
public JFreeChart createChart(Collection<Data> datas) {
    TimeSeries t1 = new TimeSeries("S1");
    Iterator<Data> iter = datas.iterator() ; 
    Data data ;
    while (iter.hasNext()) {
        data = iter.next();
        t1.addOrUpdate(RegularTimePeriod.createInstance(Millisecond.class, new Date((Long)data.getP2()), TimeZone.getDefault()), data.getValue());
    }
    XYDataset dataset = new TimeSeriesCollection(t1);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            null,
            null, 
            null, 
            dataset, 
            false,
            false, 
            false
    );
    chart.setAntiAlias(true);
    chart.setTextAntiAlias(true);
    chart.setBackgroundPaint(Color.WHITE);
    //
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setNoDataMessage("No Data to Display");
    plot.setDomainGridlinesVisible(true);
    plot.setBackgroundPaint(Color.WHITE);
    plot.setInsets(new RectangleInsets(5,14,0,5));
    //
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(ssdf);
    axis.setTickLabelFont(TICK_FONT);
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setTickLabelFont(TICK_FONT);
    //
    return chart;
}
 
Example 14
Source File: StlPlotter.java    From stl-java with Apache License 2.0 4 votes vote down vote up
private JFreeChart createChart() {

      final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(new DateAxis("Time"));
      final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
      final ClusteredXYBarRenderer barRenderer = new ClusteredXYBarRenderer();
      final GradientPaint black = new GradientPaint(0.0f, 0.0f, Color.black, 0.0f, 0.0f, Color.black);

      final TimeSeries seriests = new TimeSeries("Series");
      final TimeSeries seasonalts = new TimeSeries("Seasonal");
      final TimeSeries trendts = new TimeSeries("Trend");
      final TimeSeries remainderts = new TimeSeries("Remainder");

      final TimeSeries[] tsArray = new TimeSeries[]{seriests, seasonalts, trendts};
      final String[] labels = new String[]{"Series", "Seasonal", "Trend"};

      for (int i = 0; i < series.length; i++) {
        final Date d = new Date((long) times[i]);
        RegularTimePeriod rtp = RegularTimePeriod.createInstance(this.timePeriod, d, TimeZone.getDefault());
        seriests.addOrUpdate(rtp, series[i]);
        seasonalts.addOrUpdate(rtp, seasonal[i]);
        trendts.addOrUpdate(rtp, trend[i]);
        remainderts.addOrUpdate(rtp, remainder[i]);
      }

      plot.setGap(10.0);
      renderer.setSeriesPaint(0, black);
      barRenderer.setSeriesPaint(0, black);
      plot.setOrientation(PlotOrientation.VERTICAL);

      for (int i = 0; i < tsArray.length; i++) {
        final XYDataset ts = new TimeSeriesCollection(tsArray[i]);
        final XYPlot p = new XYPlot(ts, new DateAxis(labels[i]), new NumberAxis(labels[i]), renderer);
        plot.add(p);
      }

      final XYDataset rts = new TimeSeriesCollection(remainderts);
      final XYDataset sts = new TimeSeriesCollection(seriests);
      final XYDataset tts = new TimeSeriesCollection(trendts);
      final XYPlot rplot = new XYPlot(rts, new DateAxis(), new NumberAxis("Remainder"), barRenderer);
      final XYPlot seriesAndTrend = new XYPlot(sts, new DateAxis(), new NumberAxis("S & T"), renderer);

      seriesAndTrend.setDataset(1, tts);
      seriesAndTrend.setRenderer(1, renderer);

      plot.add(rplot);
      plot.add(seriesAndTrend);

      return new JFreeChart(this.title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
    }
 
Example 15
Source File: ObdDataPanel.java    From AndrOBD with GNU General Public License v3.0 4 votes vote down vote up
/**
 * handle changes in the process var(s)
 *
 * @param event Process var event to be handled
 */
public void pvChanged(PvChangeEvent event)
{
	TimeSeries ts;
	EcuDataPv pv;

	switch (event.getType())
	{
		case PvChangeEvent.PV_MODIFIED:
			pv = (EcuDataPv) event.getValue();
			if ((ts = selPids.get(getPvId(pv))) != null)
				try
				{
					ts.addOrUpdate(new Second(), ((Number)pv.get(EcuDataPv.FID_VALUE)).floatValue());
				} catch (Exception e)
				{
					ProcessVar.log.log(Level.SEVERE, "", e);
				}
			break;

		case PvChangeEvent.PV_DELETED:
			// remove from selectable PIDs
			pv = (EcuDataPv) event.getValue();
			selPids.remove(pv);
			break;

		case PvChangeEvent.PV_CLEARED:
			// remove all from selectable PIDs
			selPids.clear();
			break;

		case PvChangeEvent.PV_ADDED:
			if ((event.getValue() instanceof EcuDataPv))
			{
				pv = (EcuDataPv) event.getValue();
				addDataSeries(pv);
			} else if ((event.getValue() instanceof Object[]))
			{
				for (Object currPv : (Object[]) event.getValue())
				{
					addDataSeries((ProcessVar) currPv);
				}
			}
			break;
	}
	// update table column widths
	updateColumnWidths();
}