List of usage examples for org.jfree.data.time TimeSeries getItemCount
@Override public int getItemCount()
From source file:com.bpd.jfreechart.StackedAreaChartDemo.java
/** * Test program that will display a JFreeChart showing interpolated data points. * //from www . ja va2 s .c om * @param args <b>"1"</b> to display Series 1. <b>"2"</b> to display Series 2. <b>"0"</b> to * display both series. */ public static void main(String... args) { // Check arguments. if (args.length != 1) { System.err.println("Usage: java Chart [0|1|2]\n\n -- 0: Display Series 1."); System.err.println(" -- 1: Display Series 2.\n -- 2: Display both series."); return; } String option = args[0]; if (!"0".equals(option) && !"1".equals(option) && !"2".equals(option)) { System.err.println("Invalid argument: " + option); return; } // Create some sample data. List<Point<Number, Number>> list1 = new ArrayList<Point<Number, Number>>(); list1.add(new Point<Number, Number>(50, 100.0)); list1.add(new Point<Number, Number>(150, 100)); list1.add(new Point<Number, Number>(250, 200)); list1.add(new Point<Number, Number>(350, 400)); list1.add(new Point<Number, Number>(450, 200)); list1.add(new Point<Number, Number>(550, 100)); List<Point<Number, Number>> list2 = new ArrayList<Point<Number, Number>>(); list2.add(new Point<Number, Number>(50, 100.0)); list2.add(new Point<Number, Number>(150, 200.0)); list2.add(new Point<Number, Number>(250, 400.0)); list2.add(new Point<Number, Number>(350, 600.0)); list2.add(new Point<Number, Number>(450, 400.0)); list2.add(new Point<Number, Number>(550, 200.0)); // Add data to time series. TimeSeries series1 = new TimeSeries("Series 1", FixedMillisecond.class); for (Point<Number, Number> dataPoint : list1) { if ("1".equals(option) || "0".equals(option)) { series1.add(new FixedMillisecond(dataPoint.getX().longValue()), dataPoint.getY()); } series1.setDescription("Series 1"); } TimeSeries series2 = new TimeSeries("Series 2", FixedMillisecond.class); for (Point<Number, Number> dataPoint : list2) { if ("2".equals(option) || "0".equals(option)) { series2.add(new FixedMillisecond(dataPoint.getX().longValue()), dataPoint.getY()); } series2.setDescription("Series 2"); } TimeSeriesCollection collection = new TimeSeriesCollection(); if ("1".equals(option)) { collection.addSeries(series1); } else if ("2".equals(option)) { collection.addSeries(series2); } else if ("0".equals(option)) { collection.addSeries(series1); collection.addSeries(series2); } TimeTableXYDataset dataset = new TimeTableXYDataset(); @SuppressWarnings("unchecked") List<TimeSeries> timeSeriesList = collection.getSeries(); for (TimeSeries t : timeSeriesList) { for (int index = 0; index < t.getItemCount(); index++) { TimeSeriesDataItem dataItem = (TimeSeriesDataItem) t.getItems().get(index); dataset.add(t.getTimePeriod(index), dataItem.getValue().doubleValue(), t.getDescription()); } } // Create and display chart. JFreeChart chart = ChartFactory.createStackedXYAreaChart(null, null, null, dataset, PlotOrientation.VERTICAL, false, true, false); customizeChart(chart); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); JFrame frame = new JFrame(); frame.getContentPane().add(chartPanel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:com.jaxzin.iraf.demo.GSDemo.java
private static XYDataset createData() { TimeSeriesCollection tsc = new TimeSeriesCollection(); GrowthSimulator sim = new StandardGrowthSimulator(); sim.setDomain(new GSDomainImpl()); // Locate the simulation start point sim.getDomain().setInitialInvestment(Quantity.<Money>valueOf("45000 USD")); // Setup the data about me sim.getDomain().setInitialAge(28);/* w w w. j a v a 2s . c o m*/ sim.getDomain().setRetirementAge(60); sim.getDomain().setLifespan(90); // Setup the data about my job sim.getDomain().setInitialSalary(Quantity.<Money>valueOf("95000 USD")); sim.getDomain().setBonus(Quantity.<Dimensionless>valueOf("14 %")); sim.getDomain().setRaise(Quantity.<Dimensionless>valueOf("5.5 %")); sim.getDomain().setPaychecksPerYear(1); // Setup the data about contributions to IRA sim.getDomain().setContribution(Quantity.<Dimensionless>valueOf("4 %")); sim.getDomain().setEmployerMatch(Quantity.<Dimensionless>valueOf("75 %")); // Setup data about the market sim.getDomain().setRor1(Quantity.<Dimensionless>valueOf("10 %")); sim.getDomain().setRor2(Quantity.<Dimensionless>valueOf("5 %")); sim.getDomain().setRiskFreeRate(Quantity.<Dimensionless>valueOf("3 %")); sim.getDomain().setAdjustForInflation(false); // Setup data about retirement sim.getDomain().setRetirementFactor(Quantity.<Dimensionless>valueOf("25 %")); java.util.List<Money> moneys = null; try { moneys = sim.simulate(); } catch (SimulationException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } TimeSeries ts = new TimeSeries("", Year.class); for (Money money : moneys) { if (ts.getItemCount() == 0) { ts.add(RegularTimePeriod.createInstance(Year.class, DateUtilities.createDate(2006, 1, 1), TimeZone.getDefault()), money.doubleValue()); } else { ts.add(ts.getNextTimePeriod(), money.doubleValue()); } } tsc.addSeries(ts); return tsc; }
From source file:com.dreikraft.axbo.timeseries.TimeSeriesUtil.java
/** * Create a XYZ dataset from a time series with Y. * * @param source//w ww. jav a2s . c om * @return */ public static final XYZDataset createXYZTimeSeries(final TimeSeries source) { final RegularTimePeriod lastTimePeriod = source.getTimePeriod(source.getItemCount() - 1); // process all timeperiods including empty ones RegularTimePeriod t = source.getTimePeriod(0); final List<Double> zValuesList = new LinkedList<>(); while (!(t.getFirstMillisecond() > lastTimePeriod.getFirstMillisecond())) { zValuesList.add(getValue(source, t)); t = t.next(); } final double[] xValues = new double[zValuesList.size()]; final double[] yValues = new double[zValuesList.size()]; final double[] zValues = new double[zValuesList.size()]; t = source.getTimePeriod(0); for (int i = 0; i < zValuesList.size(); i++) { xValues[i] = t.getFirstMillisecond(); yValues[i] = 0; zValues[i] = zValuesList.get(i); t = t.next(); } final DefaultXYZDataset target = new DefaultXYZDataset(); target.addSeries(0, new double[][] { xValues, yValues, zValues }); return target; }
From source file:com.dreikraft.axbo.timeseries.TimeSeriesUtil.java
/** * Create a moving average time series from a given source series. * * @param source the source timeseries/*from www. j ava2 s .c o m*/ * @param preLen number of timeperiods before current time period included in * moving average calculation * @param postLen number of timeperiods after current time period included in * moving average calculation * @return a moving average time series */ public static TimeSeries createMovingAverage(final TimeSeries source, final int preLen, final int postLen) { final int len = preLen + postLen + 1; final TimeSeries result = new TimeSeries(source.getKey()); final RegularTimePeriod lastTimePeriod = source.getTimePeriod(source.getItemCount() - 1); // process all timeperiods including empty ones RegularTimePeriod t = source.getTimePeriod(0); while (!(t.getFirstMillisecond() > lastTimePeriod.getFirstMillisecond())) { // calculate the moving avg value for the current time period double value = getValue(source, t); RegularTimePeriod ti = t; for (int i = 0; i < preLen; i++) { ti = ti.previous(); value += getValue(source, ti); } ti = t; for (int i = 0; i < postLen; i++) { ti = ti.next(); value += getValue(source, ti); } // add the moving avg value to the included time periods result.addOrUpdate(t, value / len); t = t.next(); } return result; }
From source file:edu.fullerton.viewerplugin.PluginSupport.java
public static int scaleRange(TimeSeriesCollection mtds, Double miny, Double maxy) { int exp = PluginSupport.getExp(miny, maxy); double scale = Math.pow(10, exp); for (Iterator it = mtds.getSeries().iterator(); it.hasNext();) { TimeSeries ds = (TimeSeries) it.next(); for (int item = 0; item < ds.getItemCount(); item++) { TimeSeriesDataItem dataItem = ds.getDataItem(item); RegularTimePeriod period = dataItem.getPeriod(); double y = dataItem.getValue().doubleValue(); y *= scale;//ww w . ja v a 2 s . co m ds.update(period, y); } } return exp; }
From source file:edu.fullerton.viewerplugin.PluginSupport.java
public static void getRangeLimits(TimeSeriesCollection mtds, Double rng[]) { Double minx, miny, maxx, maxy; minx = miny = Double.MAX_VALUE; maxx = maxy = -Double.MAX_VALUE; for (Iterator it = mtds.getSeries().iterator(); it.hasNext();) { TimeSeries ds = (TimeSeries) it.next(); for (int item = 1; item < ds.getItemCount() - 1; item++) { TimeSeriesDataItem dataItem = ds.getDataItem(item); RegularTimePeriod period = dataItem.getPeriod(); double y = dataItem.getValue().doubleValue(); double x = period.getFirstMillisecond(); minx = Math.min(minx, x); miny = Math.min(miny, y); maxx = Math.max(maxx, x); maxy = Math.max(maxy, y); }/*from w ww . ja v a2 s.c o m*/ } rng[0] = minx; rng[1] = miny; rng[2] = maxx; rng[3] = maxy; }
From source file:org.jfree.data.time.MovingAverage.java
/** * Creates a new {@link TimeSeries} containing moving average values for * the given series. If the series is empty (contains zero items), the * result is an empty series.//from ww w. j a va 2s . c om * * @param source the source series. * @param name the name of the new series. * @param periodCount the number of periods used in the average * calculation. * @param skip the number of initial periods to skip. * * @return The moving average series. */ public static TimeSeries createMovingAverage(TimeSeries source, String name, int periodCount, int skip) { ParamChecks.nullNotPermitted(source, "source"); if (periodCount < 1) { throw new IllegalArgumentException("periodCount must be greater " + "than or equal to 1."); } TimeSeries result = new TimeSeries(name); if (source.getItemCount() > 0) { // if the initial averaging period is to be excluded, then // calculate the index of the // first data item to have an average calculated... long firstSerial = source.getTimePeriod(0).getSerialIndex() + skip; for (int i = source.getItemCount() - 1; i >= 0; i--) { // get the current data item... RegularTimePeriod period = source.getTimePeriod(i); long serial = period.getSerialIndex(); if (serial >= firstSerial) { // work out the average for the earlier values... int n = 0; double sum = 0.0; long serialLimit = period.getSerialIndex() - periodCount; int offset = 0; boolean finished = false; while ((offset < periodCount) && (!finished)) { if ((i - offset) >= 0) { TimeSeriesDataItem item = source.getRawDataItem(i - offset); RegularTimePeriod p = item.getPeriod(); Number v = item.getValue(); long currentIndex = p.getSerialIndex(); if (currentIndex > serialLimit) { if (v != null) { sum = sum + v.doubleValue(); n = n + 1; } } else { finished = true; } } offset = offset + 1; } if (n > 0) { result.add(period, sum / n); } else { result.add(period, null); } } } } return result; }
From source file:org.jfree.data.time.MovingAverage.java
/** * Creates a new {@link TimeSeries} containing moving average values for * the given series, calculated by number of points (irrespective of the * 'age' of those points). If the series is empty (contains zero items), * the result is an empty series.//from ww w . ja v a 2s . c o m * <p> * Developed by Benoit Xhenseval (www.ObjectLab.co.uk). * * @param source the source series. * @param name the name of the new series. * @param pointCount the number of POINTS used in the average calculation * (not periods!) * * @return The moving average series. */ public static TimeSeries createPointMovingAverage(TimeSeries source, String name, int pointCount) { ParamChecks.nullNotPermitted(source, "source"); if (pointCount < 2) { throw new IllegalArgumentException("periodCount must be greater " + "than or equal to 2."); } TimeSeries result = new TimeSeries(name); double rollingSumForPeriod = 0.0; for (int i = 0; i < source.getItemCount(); i++) { // get the current data item... TimeSeriesDataItem current = source.getRawDataItem(i); RegularTimePeriod period = current.getPeriod(); // FIXME: what if value is null on next line? rollingSumForPeriod += current.getValue().doubleValue(); if (i > pointCount - 1) { // remove the point i-periodCount out of the rolling sum. TimeSeriesDataItem startOfMovingAvg = source.getRawDataItem(i - pointCount); rollingSumForPeriod -= startOfMovingAvg.getValue().doubleValue(); result.add(period, rollingSumForPeriod / pointCount); } else if (i == pointCount - 1) { result.add(period, rollingSumForPeriod / pointCount); } } return result; }
From source file:com.jbombardier.reports.ReportGenerator.java
private static void generatePathView(File folder, String phase, String distinctPath, List<CapturedStatistic> capturedStatistics) { HTMLBuilder2 builder = new HTMLBuilder2(); builder.getHead().css("box.css"); builder.getHead().css("table.css"); HTMLBuilder2.Element bodyx = builder.getBody(); final HTMLBuilder2.Element content = bodyx.div("wide-box"); TimeSeries series = new TimeSeries(distinctPath); TimeSeriesCollection data = new TimeSeriesCollection(); data.addSeries(series);/* w w w .jav a 2s .c o m*/ NumberFormat nf = NumberFormat.getInstance(); try { for (CapturedStatistic capturedStatistic : capturedStatistics) { if (capturedStatistic.getPath().equals(distinctPath)) { series.addOrUpdate(new Second(new Date(capturedStatistic.getTime())), nf.parse(capturedStatistic.getValue())); } } } catch (ParseException nfe) { // Skip this one, its not numeric } if (series.getItemCount() > 0) { final String imageName = StringUtils.format("phase-{}-path-{}.png", phase, distinctPath.replace('/', '-')); content.image(imageName); final File file = new File(folder, imageName); render(StringUtils.format("phase-{}-path-{}.html", phase, distinctPath.replace('/', '-')), data, file); } builder.toFile(new File(folder, StringUtils.format("phase-{}-path-{}.html", phase, distinctPath.replace('/', '-')))); }
From source file:diplomawork.model.JPEGSaver.java
/** * Save plot to file//w w w . jav a 2 s . c om */ public void saveDiagramToJPeG() { int startIndexForTimeSeries = (timeSeries.getItemCount() >= 5) ? timeSeries.getItemCount() - 5 : 0; TimeSeries timeSeries = this.timeSeries; try { timeSeries = this.timeSeries.createCopy(startIndexForTimeSeries, this.timeSeries.getItemCount() - 1); } catch (CloneNotSupportedException ex) { Logger.getLogger(JPEGSaver.class.getName()).log(Level.SEVERE, null, ex); } TimeSeriesCollection dataset = new TimeSeriesCollection(); dataset.addSeries(timeSeries); JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, dataset, // data false, // create legend? true, // generate tooltips? false // generate URLs? ); saveChartToFile(chart, false); }