/** * Tests the equals method. */ public void testEquals() { final TimePeriodValues s1 = new TimePeriodValues("Time Series 1"); final TimePeriodValues s2 = new TimePeriodValues("Time Series 2"); final boolean b1 = s1.equals(s2); assertFalse("b1", b1); s2.setName("Time Series 1"); final boolean b2 = s1.equals(s2); assertTrue("b2", b2); final RegularTimePeriod p1 = new Day(); final RegularTimePeriod p2 = p1.next(); s1.add(p1, 100.0); s1.add(p2, 200.0); final boolean b3 = s1.equals(s2); assertFalse("b3", b3); s2.add(p1, 100.0); s2.add(p2, 200.0); final boolean b4 = s1.equals(s2); assertTrue("b4", b4); }
/** * Creates a sample dataset. * * @param name the dataset name. * @param base the starting value. * @param start the starting period. * @param count the number of values to generate. * * @return The dataset. */ private static XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) { TimeSeries series = new TimeSeries(name, start.getClass()); RegularTimePeriod period = start; double value = base; for (int i = 0; i < count; i++) { series.add(period, value); period = period.next(); value = value * (1 + (Math.random() - 0.495) / 10.0); } TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); return dataset; }
/** * Creates a new axis. * * @param label the axis label (<code>null</code> permitted). * @param first the first time period in the axis range * (<code>null</code> not permitted). * @param last the last time period in the axis range * (<code>null</code> not permitted). * @param timeZone the time zone (<code>null</code> not permitted). */ public PeriodAxis(String label, RegularTimePeriod first, RegularTimePeriod last, TimeZone timeZone) { super(label, null); this.first = first; this.last = last; this.timeZone = timeZone; this.calendar = Calendar.getInstance(timeZone); this.autoRangeTimePeriodClass = first.getClass(); this.majorTickTimePeriodClass = first.getClass(); this.minorTickMarksVisible = false; this.minorTickTimePeriodClass = RegularTimePeriod.downsize( this.majorTickTimePeriodClass); setAutoRange(true); this.labelInfo = new PeriodAxisLabelInfo[2]; this.labelInfo[0] = new PeriodAxisLabelInfo(Month.class, new SimpleDateFormat("MMM")); this.labelInfo[1] = new PeriodAxisLabelInfo(Year.class, new SimpleDateFormat("yyyy")); }
/** * Generates a tool tip text item for a particular item within a series. * * @param data the dataset. * @param series the series number (zero-based index). * @param item the item number (zero-based index). * * @return The tool tip text (possibly <code>null</code>). */ public String generateToolTip(XYDataset data, int series, int item) { String xStr, yStr; if (data instanceof YisSymbolic) { yStr = ((YisSymbolic) data).getYSymbolicValue(series, item); } else { double y = data.getYValue(series, item); yStr = Double.toString(round(y, 2)); } if (data instanceof XisSymbolic) { xStr = ((XisSymbolic) data).getXSymbolicValue(series, item); } else if (data instanceof TimeSeriesCollection) { RegularTimePeriod p = ((TimeSeriesCollection) data).getSeries(series) .getTimePeriod(item); xStr = p.toString(); } else { double x = data.getXValue(series, item); xStr = Double.toString(round(x, 2)); } return "X: " + xStr + ", Y: " + yStr; }
/** * Creates a sample dataset. * * @param name the dataset name. * @param base the starting value. * @param start the starting period. * @param count the number of values to generate. * * @return The dataset. */ private static XYDataset createDataset(String name, double base, RegularTimePeriod start, int count) { TimeSeries series = new TimeSeries(name); RegularTimePeriod period = start; double value = base; for (int i = 0; i < count; i++) { series.add(period, value); period = period.next(); value = value * (1 + (Math.random() - 0.495) / 10.0); } TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); return dataset; }
/** * Serialize a time seroes chart, restore it, and check for equality. */ @Test public void testSerialization4() { RegularTimePeriod t = new Day(); TimeSeries series = new TimeSeries("Series 1"); series.add(t, 36.4); t = t.next(); series.add(t, 63.5); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); JFreeChart c1 = ChartFactory.createTimeSeriesChart("Test", "Date", "Value", dataset); JFreeChart c2 = (JFreeChart) TestUtilities.serialised(c1); assertEquals(c1, c2); }
/** * Returns a {@link java.util.Date} corresponding to the specified position * within a {@link RegularTimePeriod}. * * @param period the period. * @param position the position (<code>null</code> not permitted). * * @return A date. */ private Date calculateDateForPosition(RegularTimePeriod period, DateTickMarkPosition position) { ParamChecks.nullNotPermitted(period, "period"); Date result = null; if (position == DateTickMarkPosition.START) { result = new Date(period.getFirstMillisecond()); } else if (position == DateTickMarkPosition.MIDDLE) { result = new Date(period.getMiddleMillisecond()); } else if (position == DateTickMarkPosition.END) { result = new Date(period.getLastMillisecond()); } return result; }
/** * Returns a {@link java.util.Date} corresponding to the specified position * within a {@link RegularTimePeriod}. * * @param period the period. * @param position the position ({@code null} not permitted). * * @return A date. */ private Date calculateDateForPosition(RegularTimePeriod period, DateTickMarkPosition position) { Args.nullNotPermitted(period, "period"); Date result = null; if (position == DateTickMarkPosition.START) { result = new Date(period.getFirstMillisecond()); } else if (position == DateTickMarkPosition.MIDDLE) { result = new Date(period.getMiddleMillisecond()); } else if (position == DateTickMarkPosition.END) { result = new Date(period.getLastMillisecond()); } return result; }
/** * Serialize a time seroes chart, restore it, and check for equality. */ @Test public void testSerialization4() { RegularTimePeriod t = new Day(); TimeSeries series = new TimeSeries("Series 1"); series.add(t, 36.4); t = t.next(); series.add(t, 63.5); TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(series); JFreeChart c1 = ChartFactory.createTimeSeriesChart("Test", "Date", "Value", dataset); JFreeChart c2 = (JFreeChart) TestUtils.serialised(c1); assertEquals(c1, c2); }
@Override public JRPrintHyperlink getEntityHyperlink(ChartEntity entity) { JRPrintHyperlink printHyperlink = null; if (hasHyperlinks() && entity instanceof XYItemEntity) { XYItemEntity itemEntity = (XYItemEntity) entity; TimeSeriesCollection dataset = (TimeSeriesCollection) itemEntity.getDataset(); TimeSeries series = dataset.getSeries(itemEntity.getSeriesIndex()); Map<RegularTimePeriod, JRPrintHyperlink> serieHyperlinks = itemHyperlinks.get(series.getKey()); if (serieHyperlinks != null) { RegularTimePeriod timePeriod = series.getTimePeriod(itemEntity.getItem()); printHyperlink = serieHyperlinks.get(timePeriod); } } return printHyperlink; }
public void nodeCompleted(Object parent) { TimeSeriesCollectionBuilder tsc = null; try { if(parent != null && parent instanceof TimeSeriesCollectionBuilder) tsc = (TimeSeriesCollectionBuilder)parent; if(this.series == null) { this.series = new TimeSeries(name, domain, range, Class.forName(timePeriodClass) ); this.setProperties(this.series, beanMap ); } if(addValue != null) { boolean notify = false; if(addValue.containsKey(("notify"))) notify = Boolean.valueOf((String)addValue.get("notify")); this.series.add((RegularTimePeriod)addValue.get("period"), ((Number)addValue.get("value")).doubleValue(), notify); addValue = null; } if(tsc != null) tsc.addSeries(this.series); } catch (Throwable ex) { logger.log(Level.WARNING, ex.getMessage(), ex); } }
public static TimeSeries getForecastData(TimeSeries observationData, int forcastperc) { TimeSeries fcdataset = new TimeSeries("Time Series Forecast"); if (observationData.isEmpty() == true) return fcdataset; int size = observationData.getItemCount(); long fcsize = size + Math.round( (double)size * ((double)forcastperc/100)); // new data set try { fcdataset = observationData.createCopy(0,size-1); for (int i=size; i < fcsize; i++) { RegularTimePeriod nexttp = fcdataset.getNextTimePeriod(); fcdataset.add(nexttp,0.00D); } } catch (Exception e) { System.out.println("Exception:" + e.getLocalizedMessage()); System.out.println("Could not create forecasted data sample"); return fcdataset; } return fcdataset; }
/** * 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) ); }
/** * Returns a {@link java.util.Date} corresponding to the specified position * within a {@link RegularTimePeriod}. * * @param period the period. * @param position the position (<code>null</code> not permitted). * * @return A date. */ private Date calculateDateForPosition(RegularTimePeriod period, DateTickMarkPosition position) { if (position == null) { throw new IllegalArgumentException("Null 'position' argument."); } Date result = null; if (position == DateTickMarkPosition.START) { result = new Date(period.getFirstMillisecond()); } else if (position == DateTickMarkPosition.MIDDLE) { result = new Date(period.getMiddleMillisecond()); } else if (position == DateTickMarkPosition.END) { result = new Date(period.getLastMillisecond()); } return result; }
/** * Creates a new axis. * * @param label the axis label (<code>null</code> permitted). * @param first the first time period in the axis range * (<code>null</code> not permitted). * @param last the last time period in the axis range * (<code>null</code> not permitted). * @param timeZone the time zone (<code>null</code> not permitted). */ public PeriodAxis(String label, RegularTimePeriod first, RegularTimePeriod last, TimeZone timeZone) { super(label, null); this.first = first; this.last = last; this.timeZone = timeZone; // FIXME: this calendar may need a locale as well this.calendar = Calendar.getInstance(timeZone); this.autoRangeTimePeriodClass = first.getClass(); this.majorTickTimePeriodClass = first.getClass(); this.minorTickMarksVisible = false; this.minorTickTimePeriodClass = RegularTimePeriod.downsize( this.majorTickTimePeriodClass); setAutoRange(true); this.labelInfo = new PeriodAxisLabelInfo[2]; this.labelInfo[0] = new PeriodAxisLabelInfo(Month.class, new SimpleDateFormat("MMM")); this.labelInfo[1] = new PeriodAxisLabelInfo(Year.class, new SimpleDateFormat("yyyy")); }
private TimeSeriesCollection buildDataSet(SortedMap<DataSetBarDescr, SortedMap<Date,Double>> series, AbstractXYItemRenderer renderer) { TimeSeriesCollection dataset = new TimeSeriesCollection(); int seriesIdx = 0; for (DataSetBarDescr serieDef : series.keySet()) { TimeSeries timeSerie = new TimeSeries(serieDef.getSerieName()); SortedMap<Date, Double> serie = series.get(serieDef); for (Date date : serie.keySet()) { RegularTimePeriod period = new Day(date); Number value = serie.get(date); TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); timeSerie.add(item, false); } dataset.addSeries(timeSerie); renderer.setSeriesPaint(seriesIdx, serieDef.getSerieColor()); renderer.setSeriesFillPaint(seriesIdx, serieDef.getSerieColor()); renderer.setSeriesStroke(seriesIdx, new BasicStroke(serieDef.getSerieStrokeSize())); seriesIdx++; } return dataset; }
@Override public TimeSeries buildBarTimeSeries(String serieName, SortedMap<Date, BarChart> barSerie, TimeSeries lineSerie) { TimeSeries timeSerie = new TimeSeries(serieName); int cpt = 0; //int gap = Math.max(1, barSerie.size()/208); int gap = 1; for (Date date : barSerie.keySet()) { if (cpt % gap == 0) { RegularTimePeriod period = new Day(date); Number value = barSerie.get(date).getValue(); TimeSeriesDataItem item = new TimeSeriesDataItem(period, value); timeSerie.add(item, false); } cpt++; } return timeSerie; }
/** * Creates a new axis. * * @param label the axis label (<code>null</code> permitted). * @param first the first time period in the axis range (<code>null</code> not permitted). * @param last the last time period in the axis range (<code>null</code> not permitted). * @param timeZone the time zone (<code>null</code> not permitted). */ public PeriodAxis(String label, RegularTimePeriod first, RegularTimePeriod last, TimeZone timeZone) { super(label, null); this.first = first; this.last = last; this.timeZone = timeZone; this.autoRangeTimePeriodClass = Day.class; setAutoRange(true); this.labelInfo = new PeriodAxisLabelInfo[2]; this.labelInfo[0] = new PeriodAxisLabelInfo(Month.class, "MMM"); this.labelInfo[1] = new PeriodAxisLabelInfo(Year.class, "yyyy"); }
/** * Sets the first time period in the axis range and sends an {@link AxisChangeEvent} to * all registered listeners. * * @param first the time period (<code>null</code> not permitted). */ public void setFirst(RegularTimePeriod first) { if (first == null) { throw new IllegalArgumentException("Null 'first' argument."); } this.first = first; notifyListeners(new AxisChangeEvent(this)); }
/** * Sets the last time period in the axis range and sends an {@link AxisChangeEvent} to * all registered listeners. * * @param last the time period (<code>null</code> not permitted). */ public void setLast(RegularTimePeriod last) { if (last == null) { throw new IllegalArgumentException("Null 'last' argument."); } this.last = last; notifyListeners(new AxisChangeEvent(this)); }