List of usage examples for org.jfree.chart ChartPanel translateJava2DToScreen
public Point translateJava2DToScreen(Point2D java2DPoint)
From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java
private boolean _updateMainTraceInfoForCandlestick(Point2D point) { if (point == null) { return false; }// ww w .jav a2 s. c o m 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; }
From source file:org.yccheok.jstock.gui.charting.ChartLayerUI.java
private boolean updateMainTraceInfo(Point2D point) { if (point == null) { return false; }/*from w ww . j a v a 2 s . c o m*/ final ChartPanel chartPanel = this.chartJDialog.getChartPanel(); // Top most plot. final XYPlot plot = this.chartJDialog.getPlot(); if (plot.getDataset() instanceof org.jfree.data.xy.DefaultHighLowDataset) { return this._updateMainTraceInfoForCandlestick(point); } final TimeSeriesCollection timeSeriesCollection = (TimeSeriesCollection) plot.getDataset(); // 0 are the main chart. 1, 2, 3... are TA. 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)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 = 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; // 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; }