List of usage examples for org.jfree.chart.axis NumberAxis valueToJava2D
@Override public double valueToJava2D(double value, Rectangle2D area, RectangleEdge edge)
From source file:MWC.GUI.JFreeChart.StepperXYPlot.java
/** * Draws the XY plot on a Java 2D graphics device (such as the screen or a * printer), together with a current time marker * <P>// ww w. j a va2 s .com * XYPlot relies on an XYItemRenderer to draw each item in the plot. This * allows the visual representation of the data to be changed easily. * <P> * The optional info argument collects information about the rendering of the * plot (dimensions, tooltip information etc). Just pass in null if you do not * need this information. * * @param g2 * The graphics device. * @param plotArea * The area within which the plot (including axis labels) should be * drawn. * @param info * Collects chart drawing information (null permitted). */ public final void draw(final Graphics2D g2, final Rectangle2D plotArea, final Point2D anchor, final PlotState state, final PlotRenderingInfo info) { super.draw(g2, plotArea, anchor, state, info); // do we want to view the line? if (!_showLine) return; // do we have a time? if (_currentTime != null) { // find the screen area for the dataset final Rectangle2D dataArea = info.getDataArea(); // determine the time we are plotting the line at long theTime = _currentTime.getMicros(); // hmmm, how do we format the date final CanBeRelativeToTimeStepper axis = (CanBeRelativeToTimeStepper) this.getDomainAxis(); // are we working in relative time mode? if (axis.isRelativeTimes()) { if (_myStepper != null) { // yes, we now need to offset the time theTime = theTime - _myStepper.getTimeZero().getMicros(); } } // hmm, see if we are wroking with a date or number axis double linePosition = 0; if (axis instanceof DateAxis) { // ok, now scale the time to graph units final DateAxis dateAxis = (DateAxis) axis; // find the new x value linePosition = dateAxis.dateToJava2D(new Date(theTime / 1000), dataArea, this.getDomainAxisEdge()); if (_resetAxes) { dateAxis.setAutoRange(true); _resetAxes = false; } if (isGrowWithTime()) { final long endMillis = theTime / 1000; long startMillis; if (_fixedDuration != null) { startMillis = endMillis - _fixedDuration.getMillis(); } else { startMillis = (long) dateAxis.getLowerBound(); } final Date startDate = new Date(startMillis); final Date endDate = new Date(endMillis); dateAxis.setRange(startDate, endDate); } else { } } else { if (axis instanceof NumberAxis) { final NumberAxis numberAxis = (NumberAxis) axis; linePosition = numberAxis.valueToJava2D(theTime, dataArea, this.getDomainAxisEdge()); if (isGrowWithTime()) numberAxis.setRange(numberAxis.getRange().getLowerBound(), theTime); else { if (_resetAxes) { numberAxis.setAutoRange(true); _resetAxes = false; } } } } // ok, finally draw the line - if we're not showing the growing plot if (!isGrowWithTime()) plotStepperLine(g2, linePosition, dataArea); } }
From source file:ucar.unidata.idv.control.chart.ScatterPlotChartWrapper.java
/** * Handle the event/*from w ww .j a v a2 s. c o m*/ * * @param event the event * * @return Should we pass on this event */ public boolean chartPanelMouseClicked(MouseEvent event) { if (SwingUtilities.isRightMouseButton(event) || (event.getClickCount() < 2)) { return super.chartPanelMousePressed(event); } List series = ((MyScatterPlot) plot).getSeries(); //TODO: Check if click is inside data area double minDistance = 100; int minIdx = -1; double minTime = -1; for (int seriesIdx = 0; seriesIdx < series.size(); seriesIdx++) { NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(seriesIdx); NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis(seriesIdx); double[][] data = (double[][]) series.get(seriesIdx); for (int i = 0; i < data[0].length; i++) { double x = domainAxis.valueToJava2D(data[0][i], getChartPanel().getScreenDataArea(), plot.getDomainAxisEdge()); double y = rangeAxis.valueToJava2D(data[1][i], getChartPanel().getScreenDataArea(), plot.getRangeAxisEdge()); double distance = Math .sqrt((x - event.getX()) * (x - event.getX()) + (y - event.getY()) * (y - event.getY())); if (distance < minDistance) { minDistance = distance; minIdx = i; } } if (minIdx >= 0) { minTime = timeValues1[minIdx]; } } if (minIdx < 0) { return EVENT_PASSON; } firePropertyChange(PROP_SELECTEDTIME, null, new Double(minTime)); return EVENT_PASSON; }
From source file:com.stableapps.anglewraparounddemo.AngleWrapDemoMain.java
/** * Creates a sample chart./*from ww w . ja v a 2 s. c o m*/ * * @return a sample chart. */ private JFreeChart createChart() { final XYDataset direction = createAngleDataset(600); final JFreeChart chart = ChartFactory.createTimeSeriesChart("Time", "Date", "Direction", direction, true, true, false); final XYPlot plot = chart.getXYPlot(); plot.getDomainAxis().setLowerMargin(0.0); plot.getDomainAxis().setUpperMargin(0.0); // configure the range axis to provide a fix set of TickUnits depending on size of chart NumberAxis rangeAxis = new NumberAxis() { /** * */ private static final long serialVersionUID = 1L; @Override public NumberTickUnit getTickUnit() { NumberTickUnit tickUnit = super.getTickUnit(); if (tickUnit.getSize() < 15) { return tickUnit; } else if (tickUnit.getSize() < 45) { return new NumberTickUnit(45); } else if (tickUnit.getSize() < 90) { return new NumberTickUnit(90); } else if (tickUnit.getSize() < 180) { return new NumberTickUnit(180); } else { return new NumberTickUnit(360); } } }; rangeAxis.setAutoRangeIncludesZero(false); plot.setRangeAxis(rangeAxis); final OverflowCondition overflowCondition = new OverflowCondition() { @Override public boolean isOverflow(double y0, double x0, double y1, double x1) { return Math.abs(y1 - y0) > 180; } }; XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false) { /** * */ private static final long serialVersionUID = 1L; double min = 0; double max = 360; LinearInterpolator interpolator = new LinearInterpolator(); @Override protected void drawPrimaryLine(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) { if (item == 0) { return; } // get the data point... double x1 = dataset.getXValue(series, item); double y1 = dataset.getYValue(series, item); if (Double.isNaN(y1) || Double.isNaN(x1)) { return; } double x0 = dataset.getXValue(series, item - 1); double y0 = dataset.getYValue(series, item - 1); if (Double.isNaN(y0) || Double.isNaN(x0)) { return; } if (overflowCondition.isOverflow(y0, x0, y1, x1)) { boolean overflowAtMax = y1 < y0; if (overflowAtMax) { LinearFunction lf = interpolator.interpolate(new double[] { y0, y1 + (max - min) }, new double[] { x0, x1 }); double xmid = lf.value(max); drawPrimaryLine(state, g2, plot, x0, y0, xmid, max, pass, series, item, domainAxis, rangeAxis, dataArea); drawPrimaryLine(state, g2, plot, xmid, min, x1, y1, pass, series, item, domainAxis, rangeAxis, dataArea); } else { LinearFunction lf = interpolator.interpolate(new double[] { y1 - (max - min), y0 }, new double[] { x1, x0 }); double xmid = lf.value(min); drawPrimaryLine(state, g2, plot, x0, y0, xmid, min, pass, series, item, domainAxis, rangeAxis, dataArea); drawPrimaryLine(state, g2, plot, xmid, max, x1, y1, pass, series, item, domainAxis, rangeAxis, dataArea); } } else { drawPrimaryLine(state, g2, plot, x0, y0, x1, y1, pass, series, item, domainAxis, rangeAxis, dataArea); } } private void drawPrimaryLine(XYItemRendererState state, Graphics2D g2, XYPlot plot, double x0, double y0, double x1, double y1, int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) { RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); double transX0 = domainAxis.valueToJava2D(x0, dataArea, xAxisLocation); double transY0 = rangeAxis.valueToJava2D(y0, dataArea, yAxisLocation); double transX1 = domainAxis.valueToJava2D(x1, dataArea, xAxisLocation); double transY1 = rangeAxis.valueToJava2D(y1, dataArea, yAxisLocation); // only draw if we have good values if (Double.isNaN(transX0) || Double.isNaN(transY0) || Double.isNaN(transX1) || Double.isNaN(transY1)) { return; } PlotOrientation orientation = plot.getOrientation(); boolean visible; if (orientation == PlotOrientation.HORIZONTAL) { state.workingLine.setLine(transY0, transX0, transY1, transX1); } else if (orientation == PlotOrientation.VERTICAL) { state.workingLine.setLine(transX0, transY0, transX1, transY1); } visible = LineUtilities.clipLine(state.workingLine, dataArea); if (visible) { drawFirstPassShape(g2, pass, series, item, state.workingLine); } } @Override protected void drawPrimaryLineAsPath(XYItemRendererState state, Graphics2D g2, XYPlot plot, XYDataset dataset, int pass, int series, int item, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea) { // get the data point... State s = (State) state; try { double x1 = dataset.getXValue(series, item); double y1 = dataset.getYValue(series, item); if (Double.isNaN(x1) && Double.isNaN(y1)) { s.setLastPointGood(false); return; } if (!s.isLastPointGood()) { ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1); s.seriesPath.moveTo(xy.getLeft(), xy.getRight()); s.setLastPointGood(true); return; } double x0 = dataset.getXValue(series, item - 1); double y0 = dataset.getYValue(series, item - 1); if (overflowCondition.isOverflow(y0, x0, y1, x1)) { boolean overflowAtMax = y1 < y0; if (overflowAtMax) { LinearFunction lf = interpolator.interpolate(new double[] { y0, y1 + (max - min) }, new double[] { x0, x1 }); double xmid = lf.value(max); ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid, max); s.seriesPath.lineTo(xy.getLeft(), xy.getRight()); xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid, min); s.seriesPath.moveTo(xy.getLeft(), xy.getRight()); xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1); s.seriesPath.lineTo(xy.getLeft(), xy.getRight()); } else { LinearFunction lf = interpolator.interpolate(new double[] { y1 - (max - min), y0 }, new double[] { x1, x0 }); double xmid = lf.value(min); ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid, min); s.seriesPath.lineTo(xy.getLeft(), xy.getRight()); xy = translate(plot, domainAxis, rangeAxis, dataArea, xmid, max); s.seriesPath.moveTo(xy.getLeft(), xy.getRight()); xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1); s.seriesPath.lineTo(xy.getLeft(), xy.getRight()); } } else { ImmutablePair<Float, Float> xy = translate(plot, domainAxis, rangeAxis, dataArea, x1, y1); s.seriesPath.lineTo(xy.getLeft(), xy.getRight()); } s.setLastPointGood(true); } finally { // if this is the last item, draw the path ... if (item == s.getLastItemIndex()) { // draw path drawFirstPassShape(g2, pass, series, item, s.seriesPath); } } } private ImmutablePair<Float, Float> translate(XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, Rectangle2D dataArea, double x, double y) { RectangleEdge xAxisLocation = plot.getDomainAxisEdge(); RectangleEdge yAxisLocation = plot.getRangeAxisEdge(); double transX1 = domainAxis.valueToJava2D(x, dataArea, xAxisLocation); double transY1 = rangeAxis.valueToJava2D(y, dataArea, yAxisLocation); // update path to reflect latest point float xtrans = (float) transX1; float ytrans = (float) transY1; PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { xtrans = (float) transY1; ytrans = (float) transX1; } return new ImmutablePair<>(xtrans, ytrans); } }; renderer.setDrawSeriesLineAsPath(true); plot.setRenderer(0, renderer); return chart; }