List of usage examples for org.jfree.data.time TimeSeries getDataItem
public TimeSeriesDataItem getDataItem(RegularTimePeriod period)
From source file:org.yccheok.jstock.gui.charting.InvestmentFlowLayerUI.java
private boolean updateInvestPoint(Point2D _investPoint) { if (_investPoint == null) { return false; }//from w w w . j a v a2 s . com final ChartPanel chartPanel = this.investmentFlowChartJDialog.getChartPanel(); final JFreeChart chart = chartPanel.getChart(); final XYPlot plot = (XYPlot) chart.getPlot(); final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) plot.getDataset(); final TimeSeries timeSeries = timeSeriesCollection.getSeries(0); // I also not sure why. This is what are being done in Mouse Listener Demo 4. // // Don't use it. It will cause us to lose precision. //final Point2D p2 = chartPanel.translateScreenToJava2D((Point)_investPoint); /* Try to get correct main chart area. */ final Rectangle2D _plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea(); /* Believe it? When there is another thread keep updateing time series data, * and keep calling setDirty, _plotArea can be 0 size sometimes. Ignore it. * Just assume we had processed it. */ if (_plotArea.getWidth() == 0.0 && _plotArea.getHeight() == 0.0) { /* Cheat the caller. */ return true; } final ValueAxis domainAxis = plot.getDomainAxis(); final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); final ValueAxis rangeAxis = plot.getRangeAxis(); final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); final double coordinateX = domainAxis.java2DToValue(_investPoint.getX(), _plotArea, domainAxisEdge); int low = 0; int high = timeSeries.getItemCount() - 1; Date date = new Date((long) coordinateX); final long time = date.getTime(); long bestDistance = Long.MAX_VALUE; int bestMid = 0; while (low <= high) { int mid = (low + high) >>> 1; final TimeSeriesDataItem timeSeriesDataItem = timeSeries.getDataItem(mid); final Day day = (Day) timeSeriesDataItem.getPeriod(); final long search = day.getFirstMillisecond(); final long cmp = search - time; if (cmp < 0) { low = mid + 1; } else if (cmp > 0) { high = mid - 1; } else { bestDistance = 0; bestMid = mid; break; } final long abs_cmp = Math.abs(cmp); if (abs_cmp < bestDistance) { bestDistance = abs_cmp; bestMid = mid; } } final TimeSeriesDataItem timeSeriesDataItem = timeSeries.getDataItem(bestMid); final double xValue = timeSeriesDataItem.getPeriod().getFirstMillisecond(); final double yValue = timeSeriesDataItem.getValue().doubleValue(); final double xJava2D = domainAxis.valueToJava2D(xValue, _plotArea, domainAxisEdge); final double yJava2D = rangeAxis.valueToJava2D(yValue, _plotArea, rangeAxisEdge); final int tmpIndex = bestMid; // Do not perform translation as this will cause precision losing. // We might experience unstable point. For example, // // this.investPoint is 700.9, there are 2 data points which are 700 and // 701. // During first updateInvestPoint(this.investPoint) call, data point 701 // will be chosen, and this.investPoint has been truncated to 700. // During second updateInvestPoint(this.investPoint) call, data point 700 // will be chosen. We may observe an unstable point swings between 700 // and 701. // // translateJava2DToScreen will internally convert Point2D.Double to Point. //final Point2D tmpPoint = chartPanel.translateJava2DToScreen(new Point2D.Double(xJava2D, yJava2D)); final Point2D tmpPoint = new Point2D.Double(xJava2D, yJava2D); this.drawArea.setRect(_plotArea); if (this.drawArea.contains(tmpPoint)) { this.investPointIndex = tmpIndex; this.investPoint = tmpPoint; return true; } return false; }
From source file:org.lmn.fc.frameworks.starbase.plugins.observatory.ui.tabs.charts.ChartHelper.java
/*********************************************************************************************** * Dump the (partial) contents of each Series in an XYdatset. * * @param dump//from w w w . j a va 2 s . c o m * @param calendar * @param dataset * @param dumprowcount * @param title */ public static void dumpXYDataset(final boolean dump, final Calendar calendar, final XYDataset dataset, final int dumprowcount, final String title) { final String SOURCE = "ChartHelper.dumpXYDataset() "; if (dump) { LOGGER.log(title); if ((dataset != null) && (dataset instanceof XYSeriesCollection)) { final XYSeriesCollection seriesCollection; seriesCollection = (XYSeriesCollection) dataset; LOGGER.log("XYSeriesCollection"); LOGGER.log(" [series.count=" + seriesCollection.getSeriesCount() + "]"); LOGGER.log(" [domain.lowerbound.interval.true=" + (long) seriesCollection.getDomainLowerBound(true) + "]"); LOGGER.log(" [domain.lowerbound.interval.false=" + (long) seriesCollection.getDomainLowerBound(false) + "]"); LOGGER.log(" [domain.upperbound.interval.true=" + (long) seriesCollection.getDomainUpperBound(true) + "]"); LOGGER.log(" [domain.upperbound.interval.false=" + (long) seriesCollection.getDomainUpperBound(false) + "]"); LOGGER.log(" [domain.order=" + seriesCollection.getDomainOrder() + "]"); for (int intSeriesIndex = 0; intSeriesIndex < seriesCollection.getSeriesCount(); intSeriesIndex++) { final XYSeries xySeries; LOGGER.log(""); LOGGER.log(" [xyseries.index=" + intSeriesIndex + "]"); xySeries = seriesCollection.getSeries(intSeriesIndex); LOGGER.log(" [xyseries.itemcount=" + xySeries.getItemCount() + "]"); LOGGER.log(" [xyseries.key=" + xySeries.getKey() + "]"); LOGGER.log(" [xyseries.xmin=" + xySeries.getMinX() + "]"); LOGGER.log(" [xyseries.xmax=" + xySeries.getMaxX() + "]"); LOGGER.log(" [xyseries.ymin=" + xySeries.getMinY() + "]"); LOGGER.log(" [xyseries.ymax=" + xySeries.getMaxY() + "]"); LOGGER.log(" [xyseries.description=" + xySeries.getDescription() + "]"); LOGGER.log(" [xyseries.autosort=" + xySeries.getAutoSort() + "]"); LOGGER.log(" [xyseries.allowduplicatex=" + xySeries.getAllowDuplicateXValues() + "]"); // Dump the first chunk for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, xySeries.getItemCount())); intItemIndex++) { final XYDataItem item; item = xySeries.getDataItem(intItemIndex); LOGGER.log(" [item.index=" + intItemIndex + "] [item.x=" + item.getXValue() + "] [item.y=" + item.getYValue() + "]"); } LOGGER.log(" ..."); // Dump the last chunk for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, xySeries.getItemCount())); intItemIndex++) { final XYDataItem item; final int intIndex; intIndex = Math.max(0, xySeries.getItemCount() - dumprowcount) + intItemIndex; item = xySeries.getDataItem(intIndex); LOGGER.log(" [item.index=" + intIndex + "] [item.x=" + item.getXValue() + "] [item.y=" + item.getYValue() + "]"); } } } else if ((dataset != null) && (dataset instanceof TimeSeriesCollection)) { final TimeSeriesCollection seriesCollection; seriesCollection = (TimeSeriesCollection) dataset; LOGGER.log("TimeSeriesCollection"); LOGGER.log(" [series.count=" + seriesCollection.getSeriesCount() + "]"); LOGGER.log(" [domain.lowerbound.interval.true=" + (long) seriesCollection.getDomainLowerBound(true) + "]"); LOGGER.log(" [domain.lowerbound.interval.false=" + (long) seriesCollection.getDomainLowerBound(false) + "]"); LOGGER.log(" [domain.upperbound.interval.true=" + (long) seriesCollection.getDomainUpperBound(true) + "]"); LOGGER.log(" [domain.upperbound.interval.false=" + (long) seriesCollection.getDomainUpperBound(false) + "]"); LOGGER.log(" [domain.order=" + seriesCollection.getDomainOrder() + "]"); for (int intSeriesIndex = 0; intSeriesIndex < seriesCollection.getSeriesCount(); intSeriesIndex++) { final TimeSeries timeSeries; LOGGER.log(""); LOGGER.log(" [timeseries.index=" + intSeriesIndex + "]"); timeSeries = seriesCollection.getSeries(intSeriesIndex); LOGGER.log(" [timeseries.itemcount=" + timeSeries.getItemCount() + "]"); LOGGER.log(" [timeseries.key=" + timeSeries.getKey() + "]"); LOGGER.log(" [timeseries.ymin=" + timeSeries.getMinY() + "]"); LOGGER.log(" [timeseries.ymax=" + timeSeries.getMaxY() + "]"); LOGGER.log(" [timeseries.domain=" + timeSeries.getDomainDescription() + "]"); LOGGER.log(" [timeseries.range=" + timeSeries.getRangeDescription() + "]"); LOGGER.log( " [timeseries.timeperiodclass=" + timeSeries.getTimePeriodClass().getName() + "]"); for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, timeSeries.getItemCount())); intItemIndex++) { final TimeSeriesDataItem item; item = timeSeries.getDataItem(intItemIndex); LOGGER.log(" [item.index=" + intItemIndex + "] [item.period.serialindex=" + item.getPeriod().getSerialIndex() + "] [item.period.firstmillis=" + item.getPeriod().getFirstMillisecond(calendar) + "] [item.value=" + item.getValue() + "]"); } LOGGER.log(" ..."); for (int intItemIndex = 0; intItemIndex < (Math.min(dumprowcount, timeSeries.getItemCount())); intItemIndex++) { final TimeSeriesDataItem item; final int intIndex; intIndex = Math.max(0, timeSeries.getItemCount() - dumprowcount) + intItemIndex; item = timeSeries.getDataItem(intIndex); LOGGER.log(" [item.index=" + intIndex + "] [item.period.serialindex=" + item.getPeriod().getSerialIndex() + "] [item.period.firstmillis=" + item.getPeriod().getFirstMillisecond(calendar) + "] [item.value=" + item.getValue() + "]"); } } } else { LOGGER.error(SOURCE + "Unsupported XYDataset type"); } } }
From source file:org.lmn.fc.frameworks.starbase.plugins.observatory.ui.tabs.charts.ChartHelper.java
/*********************************************************************************************** * Preserve, Truncate or Decimate an XYDataset to try to achieve the requested display limit. * Decimation requested overrides all other modes. * Transform the dataset in accordance with the ChannelSelection Modes. * TRUNCATE is not currently used.//ww w . ja va 2s . c om * * ChartUpdate isdecimated Outcome * * PRESERVE N Always PRESERVE, don't use displaylimit * Y ditto * * DECIMATE N Do same as PRESERVE * Y Skip enough to leave displaylimit * * TRUNCATE N Use only displaylimit, ignore isdecimated * Y ditto * * * @param dataset * @param timezone * @param chartupdatetype * @param displaylimit * @param isdecimated * @param selectionmodes * * @return XYDataset */ private static XYDataset copyTransformedXYDataset(final XYDataset dataset, final TimeZone timezone, final DataUpdateType chartupdatetype, final int displaylimit, final boolean isdecimated, final List<ChannelSelectionMode> selectionmodes) { final String SOURCE = "ChartHelper.copyTransformedXYDataset() "; final DataUpdateType updateTypeToTransform; final XYDataset xyResult; // If decimation is NOT requested by the user for a DECIMATE chart then just do PRESERVE // Otherwise do exactly as the Chart configuration requests if ((DataUpdateType.DECIMATE.equals(chartupdatetype)) && (!isdecimated)) { updateTypeToTransform = DataUpdateType.PRESERVE; } else { updateTypeToTransform = chartupdatetype; } // System.out.println(SOURCE + "displaylimit=" + displaylimit // + " isdecimated=" + isdecimated // + " chartupdatetype=" + chartupdatetype.getName() // + " updateTypeToTransform=" + updateTypeToTransform.getName()); //----------------------------------------------------------------------------------------- // Use the whole dataset immediately if requested if (DataUpdateType.PRESERVE.equals(updateTypeToTransform)) { //System.out.println(SOURCE + "Preserve whole dataset"); xyResult = dataset; return (xyResult); } //----------------------------------------------------------------------------------------- // Now do the transformation: TRUNCATE or DECIMATE if (dataset instanceof TimeSeriesCollection) { xyResult = new TimeSeriesCollection(timezone); // Process each Series in turn for (int intSeriesIndex = 0; intSeriesIndex < dataset.getSeriesCount(); intSeriesIndex++) { final TimeSeries timeSeriesInput; final TimeSeries timeSeriesOutput; timeSeriesInput = ((TimeSeriesCollection) dataset).getSeries(intSeriesIndex); // Make a TimeSeries based on Seconds... // whose name is the ChannelName timeSeriesOutput = new TimeSeries(timeSeriesInput.getKey(), timeSeriesInput.getTimePeriodClass()); switch (updateTypeToTransform) { case TRUNCATE: { final int intStart; //System.out.println("truncate time series"); if (timeSeriesInput.getItemCount() > displaylimit) { intStart = timeSeriesInput.getItemCount() - displaylimit; } else { intStart = 0; } // Modify each Series in exactly the same way! // timeSeriesOutput = timeSeriesInput.createCopy(intStart, timeSeriesInput.getItemCount()-1); for (int item = intStart; item < timeSeriesInput.getItemCount(); item++) { timeSeriesOutput.add(timeSeriesInput.getDataItem(item)); } break; } case DECIMATE: { final int intSkipCount; //System.out.println("decimate time series index=" + intSeriesIndex); if (timeSeriesInput.getItemCount() > displaylimit) { intSkipCount = (timeSeriesInput.getItemCount() / displaylimit) - 1; } else { // Show all of the data items, i.e. insufficient data to decimate intSkipCount = 0; } for (int item = 0; item < timeSeriesInput.getItemCount(); item = item + intSkipCount + 1) { timeSeriesOutput.add(timeSeriesInput.getDataItem(item)); } break; } default: { LOGGER.error(SOURCE + MSG_UNSUPPORTED_UPDATE_TYPE); } } // Accumulate each Series in the output ((TimeSeriesCollection) xyResult).addSeries(timeSeriesOutput); } } else if (dataset instanceof XYSeriesCollection) { xyResult = new XYSeriesCollection(); //System.out.println(SOURCE + "XYSeriesCollection for " + displaylimit + " samples"); // Process each Series in turn for (int intSeriesIndex = 0; intSeriesIndex < dataset.getSeriesCount(); intSeriesIndex++) { final XYSeries xySeriesInput; final XYSeries xySeriesOutput; xySeriesInput = ((XYSeriesCollection) dataset).getSeries(intSeriesIndex); xySeriesOutput = new XYSeries(dataset.getSeriesKey(intSeriesIndex)); switch (updateTypeToTransform) { case TRUNCATE: { final int intStart; //System.out.println("truncate xy"); if (xySeriesInput.getItemCount() > displaylimit) { intStart = xySeriesInput.getItemCount() - displaylimit; } else { intStart = 0; } // Modify each Series in exactly the same way! // xySeriesOutput = xySeriesInput.createCopy(intStart, xySeriesInput.getItemCount()-1); for (int item = intStart; item < xySeriesInput.getItemCount(); item++) { xySeriesOutput.add(xySeriesInput.getDataItem(item)); } break; } case DECIMATE: { final int intSkipCount; //System.out.println("decimate xy series index=" + intSeriesIndex); if (xySeriesInput.getItemCount() > displaylimit) { intSkipCount = (xySeriesInput.getItemCount() / displaylimit) - 1; } else { // Show all of the data items, i.e. insufficient data to decimate intSkipCount = 0; } for (int item = 0; item < xySeriesInput.getItemCount(); item = item + intSkipCount + 1) { xySeriesOutput.add(xySeriesInput.getDataItem(item)); } break; } default: { LOGGER.error(SOURCE + MSG_UNSUPPORTED_UPDATE_TYPE); } } // Accumulate each Series in the output ((XYSeriesCollection) xyResult).addSeries(xySeriesOutput); } } else { LOGGER.error(SOURCE + "Unsupported XYDataset type"); xyResult = new XYSeriesCollection(); } return (xyResult); }