List of usage examples for org.jfree.data.xy DefaultHighLowDataset getCloseValue
@Override public double getCloseValue(int series, int item)
From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java
private Point2D.Double _getPointForCandlestick(int plotIndex, int seriesIndex, int dataIndex) { final ChartPanel chartPanel = this.chartJDialog.getChartPanel(); final XYPlot plot = this.chartJDialog.getPlot(plotIndex); final ValueAxis domainAxis = plot.getDomainAxis(); final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); final ValueAxis rangeAxis = plot.getRangeAxis(); final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); final org.jfree.data.xy.DefaultHighLowDataset defaultHighLowDataset = (org.jfree.data.xy.DefaultHighLowDataset) plot .getDataset(seriesIndex);// w ww.j a v a2 s . c o m if (dataIndex >= defaultHighLowDataset.getItemCount(0)) { /* Not ready yet. */ return null; } final double xValue = defaultHighLowDataset.getXDate(0, dataIndex).getTime(); final double yValue = defaultHighLowDataset.getCloseValue(0, dataIndex); final Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(plotIndex) .getDataArea(); final double xJava2D = domainAxis.valueToJava2D(xValue, plotArea, domainAxisEdge); final double yJava2D = rangeAxis.valueToJava2D(yValue, plotArea, rangeAxisEdge); // Use Double version, to avoid from losing precision. return new Point2D.Double(xJava2D, yJava2D); }
From source file:org.yccheok.jstock.gui.charting.ChartJDialog.java
/** * Calculate and update high low value labels, according to current displayed * time range. This is a time consuming method, and shall be called by * user thread.// w w w . ja va 2 s.c om */ private void _updateHighLowJLabels() { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ChartJDialog.this.jLabel2.setText(""); ChartJDialog.this.jLabel4.setText(""); } }); final ValueAxis valueAxis = this.getPlot().getDomainAxis(); final Range range = valueAxis.getRange(); final long lowerBound = (long) range.getLowerBound(); final long upperBound = (long) range.getUpperBound(); // Perform binary search, to located day in price time series, which // is equal or lesser than upperBound. int low = 0; int high = this.priceTimeSeries.getItemCount() - 1; long best_dist = Long.MAX_VALUE; int best_mid = -1; while (low <= high) { int mid = (low + high) >>> 1; final Day day = (Day) this.priceTimeSeries.getDataItem(mid).getPeriod(); long v = day.getFirstMillisecond(); if (v > upperBound) { high = mid - 1; } else if (v < upperBound) { low = mid + 1; long dist = upperBound - v; if (dist < best_dist) { best_dist = dist; best_mid = mid; } } else { best_dist = 0; best_mid = mid; break; } } if (best_mid < 0) { return; } double high_price = -Double.MAX_VALUE; double low_price = Double.MAX_VALUE; final DefaultHighLowDataset defaultHighLowDataset = (DefaultHighLowDataset) this.priceOHLCDataset; for (int i = best_mid; i >= 0; i--) { final TimeSeriesDataItem item = this.priceTimeSeries.getDataItem(i); final long time = ((Day) item.getPeriod()).getFirstMillisecond(); if (time < lowerBound) { break; } final double _high_price = defaultHighLowDataset.getHighValue(0, i); final double _low_price = defaultHighLowDataset.getLowValue(0, i); final double _last_price = defaultHighLowDataset.getCloseValue(0, i); high_price = Math.max(high_price, _high_price); // Prevent bad data. if (_low_price > 0) { low_price = Math.min(low_price, _low_price); } else { if (_high_price > 0) { low_price = Math.min(low_price, _high_price); } if (_last_price > 0) { low_price = Math.min(low_price, _last_price); } } } final double h = high_price; final double l = low_price; if (high_price >= low_price) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ChartJDialog.this.jLabel2.setText(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(h)); ChartJDialog.this.jLabel4.setText(org.yccheok.jstock.gui.Utils.stockPriceDecimalFormat(l)); } }); } }
From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java
private boolean _updateMainTraceInfoForCandlestick(Point2D point) { if (point == null) { return false; }/* ww w .j a va2s.c om*/ final ChartPanel chartPanel = this.chartJDialog.getChartPanel(); // Top most plot. final XYPlot plot = this.chartJDialog.getPlot(); final org.jfree.data.xy.DefaultHighLowDataset defaultHighLowDataset = (org.jfree.data.xy.DefaultHighLowDataset) plot .getDataset(); // 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)point); /* Try to get correct main chart area. */ final Rectangle2D _plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0) .getDataArea(); final ValueAxis domainAxis = plot.getDomainAxis(); final RectangleEdge domainAxisEdge = plot.getDomainAxisEdge(); final ValueAxis rangeAxis = plot.getRangeAxis(); final RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge(); // Don't use it. It will cause us to lose precision. //final double coordinateX = domainAxis.java2DToValue(p2.getX(), _plotArea, // domainAxisEdge); final double coordinateX = domainAxis.java2DToValue(point.getX(), _plotArea, domainAxisEdge); //double coordinateY = rangeAxis.java2DToValue(mousePoint2.getY(), plotArea, // rangeAxisEdge); int low = 0; int high = defaultHighLowDataset.getItemCount(0) - 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 Date d = defaultHighLowDataset.getXDate(0, mid); final long search = d.getTime(); 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 double xValue = defaultHighLowDataset.getXDate(0, bestMid).getTime(); final double yValue = defaultHighLowDataset.getCloseValue(0, bestMid); final double xJava2D = domainAxis.valueToJava2D(xValue, _plotArea, domainAxisEdge); final double yJava2D = rangeAxis.valueToJava2D(yValue, _plotArea, rangeAxisEdge); final int tmpIndex = bestMid; // translateJava2DToScreen will internally convert Point2D.Double to Point. final Point2D tmpPoint = chartPanel.translateJava2DToScreen(new Point2D.Double(xJava2D, yJava2D)); this.mainDrawArea.setRect(_plotArea); if (this.mainDrawArea.contains(tmpPoint)) { // 0 indicates main plot. this.mainTraceInfo = TraceInfo.newInstance(tmpPoint, 0, 0, tmpIndex); return true; } return false; }