/** * Returns the data area (the area inside the axes) for the plot or subplot, with the current * scaling applied. * * @param x * the x-coordinate (for subplot selection). * @param y * the y-coordinate (for subplot selection). * * @return The scaled data area. */ @Override public Rectangle2D getScreenDataArea(int x, int y) { PlotRenderingInfo plotInfo = this.info.getPlotInfo(); Rectangle2D result; if (plotInfo.getSubplotCount() == 0) { result = getScreenDataArea(); } else { // get the origin of the zoom selection in the Java2D space used for // drawing the chart (that is, before any scaling to fit the panel) Point2D selectOrigin = translateScreenToJava2D(new Point(x, y)); int subplotIndex = plotInfo.getSubplotIndex(selectOrigin); if (subplotIndex == -1) { return null; } result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea()); } return result; }
/** * Draws the axis on a Java 2D graphics device (such as the screen or a * printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location (determines where to draw the axis). * @param plotArea the area within which the axes and plot should be drawn. * @param dataArea the area within which the data should be drawn. * @param edge the axis location (<code>null</code> not permitted). * @param plotState collects information about the plot * (<code>null</code> permitted). * * @return The axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState axisState = new AxisState(cursor); if (isAxisLineVisible()) { drawAxisLine(g2, cursor, dataArea, edge); } drawTickMarks(g2, axisState, dataArea, edge); for (int band = 0; band < this.labelInfo.length; band++) { axisState = drawTickLabels(band, g2, axisState, dataArea, edge); } // draw the axis label (note that 'state' is passed in *and* // returned)... axisState = drawLabel(getLabel(), g2, plotArea, dataArea, edge, axisState); return axisState; }
@Override public List<Pair<Integer, Range>> calculateRangeAxesZoom(double lowerPercent, double upperPercent, PlotRenderingInfo info, Point2D source, boolean zoomIn) { List<Pair<Integer, Range>> newRanges = new LinkedList<Pair<Integer, Range>>(); for (int i = 0; i < getRangeAxisCount(); i++) { ValueAxis rangeAxis = getRangeAxis(i); if (rangeAxis != null) { if (rangeAxis instanceof LinkAndBrushAxis) { Range calculateZoomRange = ((LinkAndBrushAxis) rangeAxis).calculateZoomRange(lowerPercent, upperPercent, zoomIn); newRanges.add(new Pair<Integer, Range>(i, calculateZoomRange)); } else if (zoomIn) { rangeAxis.zoomRange(lowerPercent, upperPercent); } } } return newRanges; }
/** * Draws the axis on a Java 2D graphics device (such as the screen or a * printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location. * @param plotArea the area within which the axis should be drawn * (<code>null</code> not permitted). * @param dataArea the area within which the plot is being drawn * (<code>null</code> not permitted). * @param edge the location of the axis (<code>null</code> not permitted). * @param plotState collects information about the plot * (<code>null</code> permitted). * * @return The axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { // if the axis is not visible, don't draw it... if (!isVisible()) { return new AxisState(cursor); } if (isAxisLineVisible()) { drawAxisLine(g2, cursor, dataArea, edge); } // draw the category labels and axis label AxisState state = new AxisState(cursor); state = drawCategoryLabels(g2, plotArea, dataArea, edge, state, plotState); state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state); return state; }
/** * Handle the case where a plot implements the {@link Zoomable} interface. * * @param zoomable the zoomable plot. * @param e the mouse wheel event. */ private void handleZoomable(ChartCanvas canvas, Zoomable zoomable, ScrollEvent e) { // don't zoom unless the mouse pointer is in the plot's data area ChartRenderingInfo info = canvas.getRenderingInfo(); PlotRenderingInfo pinfo = info.getPlotInfo(); Point2D p = new Point2D.Double(e.getX(), e.getY()); if (pinfo.getDataArea().contains(p)) { Plot plot = (Plot) zoomable; // do not notify while zooming each axis boolean notifyState = plot.isNotify(); plot.setNotify(false); int clicks = (int) e.getDeltaY(); double zf = 1.0 + this.zoomFactor; if (clicks < 0) { zf = 1.0 / zf; } if (canvas.isDomainZoomable()) { zoomable.zoomDomainAxes(zf, pinfo, p, true); } if (canvas.isRangeZoomable()) { zoomable.zoomRangeAxes(zf, pinfo, p, true); } plot.setNotify(notifyState); // this generates the change event too } }
/** * Initialises the renderer and returns a state object that will be passed to subsequent calls * to the drawItem method. This method gets called once at the start of the process of * drawing a chart. * * @param g2 the graphics device. * @param dataArea the area in which the data is to be plotted. * @param plot the plot. * @param rendererIndex the renderer index. * @param info collects chart rendering information for return to caller. * * @return The renderer state. */ public CategoryItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { CategoryItemRendererState state = super.initialise(g2, dataArea, plot, rendererIndex, info); // get the clipping values... ValueAxis rangeAxis = getRangeAxis(plot, rendererIndex); this.lowerClip = rangeAxis.getRange().getLowerBound(); this.upperClip = rangeAxis.getRange().getUpperBound(); // calculate the bar width calculateBarWidth(plot, dataArea, rendererIndex, state); return state; }
/** * Draws the axis on a Java 2D graphics device (such as the screen or a printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location. * @param plotArea the area within which the plot and axes should be drawn (<code>null</code> * not permitted). * @param dataArea the area within which the data should be drawn (<code>null</code> not * permitted). * @param edge the axis location (<code>null</code> not permitted). * @param plotState collects information about the plot (<code>null</code> permitted). * * @return the axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState info = new AxisState(cursor); if (isVisible()) { info = super.draw(g2, cursor, plotArea, dataArea, edge, plotState); } if (this.symbolicGridLinesVisible) { drawSymbolicGridLines(g2, plotArea, dataArea, edge, info.getTicks()); } return info; }
/** * Returns the data area (the area inside the axes) for the plot or subplot, * with the current scaling applied. * * @param x the x-coordinate (for subplot selection). * @param y the y-coordinate (for subplot selection). * * @return The scaled data area. */ public Rectangle getScreenDataArea(int x, int y) { PlotRenderingInfo plotInfo = this.info.getPlotInfo(); Rectangle result; if (plotInfo.getSubplotCount() == 0) result = getScreenDataArea(); else { // get the origin of the zoom selection in the Java2D space used for // drawing the chart (that is, before any scaling to fit the panel) Point2D selectOrigin = translateScreenToJava2D(new Point(x, y)); int subplotIndex = plotInfo.getSubplotIndex(selectOrigin); if (subplotIndex == -1) { return null; } result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea()); } return result; }
/** * Returns the data area (the area inside the axes) for the plot or subplot, * with the current scaling applied. * * @param x the x-coordinate (for subplot selection). * @param y the y-coordinate (for subplot selection). * * @return The scaled data area. */ public Rectangle2D getScreenDataArea(int x, int y) { PlotRenderingInfo plotInfo = this.info.getPlotInfo(); Rectangle2D result; if (plotInfo.getSubplotCount() == 0) { result = getScreenDataArea(); } else { // get the origin of the zoom selection in the Java2D space used for // drawing the chart (that is, before any scaling to fit the panel) Point2D selectOrigin = translateScreenToJava2D(new Point(x, y)); int subplotIndex = plotInfo.getSubplotIndex(selectOrigin); if (subplotIndex == -1) { return null; } result = scale(plotInfo.getSubplotInfo(subplotIndex).getDataArea()); } return result; }
/** * Draws all the annotations for the specified layer. * * @param g2 the graphics device. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param layer the layer. * @param info the plot rendering info. */ public void drawAnnotations(Graphics2D g2, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, Layer layer, PlotRenderingInfo info) { Iterator iterator = null; if (layer.equals(Layer.FOREGROUND)) { iterator = this.foregroundAnnotations.iterator(); } else if (layer.equals(Layer.BACKGROUND)) { iterator = this.backgroundAnnotations.iterator(); } else { // should not get here throw new RuntimeException("Unknown layer."); } while (iterator.hasNext()) { XYAnnotation annotation = (XYAnnotation) iterator.next(); annotation.draw(g2, this.plot, dataArea, domainAxis, rangeAxis, 0, info); } }
/** * Draws the visual representation of a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the plot is being drawn. * @param info collects info about the drawing. * @param plot the plot (can be used to obtain standard color * information etc). * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot * (<code>null</code> permitted). * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { PlotOrientation orientation = plot.getOrientation(); if (orientation == PlotOrientation.HORIZONTAL) { drawHorizontalItem(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass); } else if (orientation == PlotOrientation.VERTICAL) { drawVerticalItem(g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass); } }
/** * Initialises the renderer and returns a state object that will be used * for the remainder of the drawing process for a single chart. The state * object allows for the fact that the renderer may be used simultaneously * by multiple threads (each thread will work with a separate state object). * * @param g2 the graphics device. * @param dataArea the data area. * @param plot the plot. * @param rendererIndex the renderer index. * @param info an object for returning information about the structure of * the plot (<code>null</code> permitted). * * @return The renderer state. */ public CategoryItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { setPlot(plot); CategoryDataset data = plot.getDataset(rendererIndex); if (data != null) { this.rowCount = data.getRowCount(); this.columnCount = data.getColumnCount(); } else { this.rowCount = 0; this.columnCount = 0; } return createState(info); }
/** * Initialises the renderer and returns a state object that will be passed * to subsequent calls to the drawItem method. This method gets called * once at the start of the process of drawing a chart. * * @param g2 the graphics device. * @param dataArea the area in which the data is to be plotted. * @param plot the plot. * @param rendererIndex the renderer index. * @param info collects chart rendering information for return to caller. * * @return The renderer state. */ public CategoryItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { CategoryItemRendererState state = super.initialise(g2, dataArea, plot, rendererIndex, info); // get the clipping values... ValueAxis rangeAxis = plot.getRangeAxisForDataset(rendererIndex); this.lowerClip = rangeAxis.getRange().getLowerBound(); this.upperClip = rangeAxis.getRange().getUpperBound(); // calculate the bar width calculateBarWidth(plot, dataArea, rendererIndex, state); return state; }
/** * Draws the annotation. * * @param g2 the graphics device. * @param plot the plot. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param rendererIndex the renderer index. * @param info if supplied, this info object will be populated with * entity information. */ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) { PlotOrientation orientation = plot.getOrientation(); RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( plot.getDomainAxisLocation(), orientation); RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( plot.getRangeAxisLocation(), orientation); float j2DX = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge); float j2DY = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge); Rectangle2D area = new Rectangle2D.Double(j2DX - this.width / 2.0, j2DY - this.height / 2.0, this.width, this.height); this.drawable.draw(g2, area); String toolTip = getToolTipText(); String url = getURL(); if (toolTip != null || url != null) { addEntity(info, area, rendererIndex, toolTip, url); } }
/** * Returns the data area (the area inside the axes) for the plot or subplot. * * @param point the selection point (for subplot selection). * * @return The data area. */ public Rectangle2D findDataArea(Point2D point) { PlotRenderingInfo plotInfo = this.info.getPlotInfo(); Rectangle2D result; if (plotInfo.getSubplotCount() == 0) { result = plotInfo.getDataArea(); } else { int subplotIndex = plotInfo.getSubplotIndex(point); if (subplotIndex == -1) { return null; } result = plotInfo.getSubplotInfo(subplotIndex).getDataArea(); } return result; }
/** * Handles a mouse dragged event by calculating the distance panned and * updating the axes accordingly. * * @param canvas the JavaFX canvas ({@code null} not permitted). * @param e the mouse event ({@code null} not permitted). */ @Override public void handleMouseDragged(ChartCanvas canvas, MouseEvent e) { if (this.panLast == null) { //handle panning if we have a start point else unregister canvas.clearLiveHandler(); return; } JFreeChart chart = canvas.getChart(); double dx = e.getX() - this.panLast.getX(); double dy = e.getY() - this.panLast.getY(); if (dx == 0.0 && dy == 0.0) { return; } double wPercent = -dx / this.panW; double hPercent = dy / this.panH; boolean old = chart.getPlot().isNotify(); chart.getPlot().setNotify(false); Pannable p = (Pannable) chart.getPlot(); PlotRenderingInfo info = canvas.getRenderingInfo().getPlotInfo(); if (p.getOrientation().isVertical()) { p.panDomainAxes(wPercent, info, this.panLast); p.panRangeAxes(hPercent, info, this.panLast); } else { p.panDomainAxes(hPercent, info, this.panLast); p.panRangeAxes(wPercent, info, this.panLast); } this.panLast = new Point2D.Double(e.getX(), e.getY()); chart.getPlot().setNotify(old); }
/** * Draws the visual representation of a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the data is being drawn. * @param info collects information about the drawing. * @param plot the plot (can be used to obtain standard color information etc). * @param domainAxis the domain (horizontal) axis. * @param rangeAxis the range (vertical) axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot (<code>null</code> permitted). * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { if (pass == 0) { drawItemPass0( g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState ); } else if (pass == 1) { drawItemPass1( g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState ); } }
/** * Creates a new state for the renderer. * * @param info the plot rendering info. */ public StackedXYAreaRendererState(PlotRenderingInfo info) { super(info); this.seriesArea = null; this.line = null; this.lastSeriesPoints = new Stack(); this.currentSeriesPoints = null; }
/** * Initialises the renderer then returns the number of 'passes' through the data that the * renderer will require (usually just one). This method will be called before the first * item is rendered, giving the renderer an opportunity to initialise any * state information it wants to maintain. The renderer can do nothing if it chooses. * * @param g2 the graphics device. * @param dataArea the area inside the axes. * @param plot the plot. * @param dataset the data. * @param info an optional info collection object to return data back to the caller. * * @return The number of passes the renderer requires. */ public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset dataset, PlotRenderingInfo info) { // calculate the maximum allowed candle width from the axis... ValueAxis axis = plot.getDomainAxis(); double x1 = axis.getLowerBound(); double x2 = x1 + this.maxCandleWidthInMilliseconds; RectangleEdge edge = plot.getDomainAxisEdge(); double xx1 = axis.valueToJava2D(x1, dataArea, edge); double xx2 = axis.valueToJava2D(x2, dataArea, edge); this.maxCandleWidth = Math.abs(xx2 - xx1); // Absolute value, since the relative x // positions are reversed for horizontal orientation // calculate the highest volume in the dataset... if (this.drawVolume) { OHLCDataset highLowDataset = (OHLCDataset) dataset; this.maxVolume = 0.0; for (int series = 0; series < highLowDataset.getSeriesCount(); series++) { for (int item = 0; item < highLowDataset.getItemCount(series); item++) { double volume = highLowDataset.getVolumeValue(series, item); if (volume > this.maxVolume) { this.maxVolume = volume; } } } } return new XYItemRendererState(info); }
/** * Draws the visual representation of a single data item. * * @param g2 the graphics device. * @param state the renderer state. * @param dataArea the area within which the plot is being drawn. * @param info collects info about the drawing. * @param plot the plot (can be used to obtain standard color information etc). * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param dataset the dataset. * @param series the series index (zero-based). * @param item the item index (zero-based). * @param crosshairState crosshair information for the plot (<code>null</code> permitted). * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { PlotOrientation orientation = plot.getOrientation(); setPaint(getItemPaint(series, item)); if (orientation == PlotOrientation.HORIZONTAL) { drawHorizontalItem( g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass ); } else if (orientation == PlotOrientation.VERTICAL) { drawVerticalItem( g2, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass ); } }
/** * Plots the data for a given series. * * @param g2 the drawing surface. * @param dataArea the data area. * @param info collects plot rendering info. * @param plot the plot. * @param dataset the dataset. * @param seriesIndex the series index. */ public void drawSeries(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, PolarPlot plot, XYDataset dataset, int seriesIndex) { Polygon poly = new Polygon(); int numPoints = dataset.getItemCount(seriesIndex); for (int i = 0; i < numPoints; i++) { double theta = dataset.getXValue(seriesIndex, i); double radius = dataset.getYValue(seriesIndex, i); Point p = plot.translateValueThetaRadiusToJava2D(theta, radius, dataArea); poly.addPoint(p.x, p.y); } g2.setPaint(getSeriesPaint(seriesIndex)); g2.setStroke(getSeriesStroke(seriesIndex)); if (isSeriesFilled(seriesIndex)) { Composite savedComposite = g2.getComposite(); g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); g2.fill(poly); g2.setComposite(savedComposite); } else { g2.draw(poly); } }
/** * A utility method for adding an {@link XYAnnotationEntity} to * a {@link PlotRenderingInfo} instance. * * @param info the plot rendering info (<code>null</code> permitted). * @param hotspot the hotspot area. * @param rendererIndex the renderer index. * @param toolTipText the tool tip text. * @param urlText the URL text. */ protected void addEntity(PlotRenderingInfo info, Shape hotspot, int rendererIndex, String toolTipText, String urlText) { if (info == null) { return; } EntityCollection entities = info.getOwner().getEntityCollection(); if (entities == null) { return; } XYAnnotationEntity entity = new XYAnnotationEntity(hotspot, rendererIndex, toolTipText, urlText); entities.add(entity); }
/** * Test the equals() method. */ public void testEquals() { PlotRenderingInfo p1 = new PlotRenderingInfo(new ChartRenderingInfo()); PlotRenderingInfo p2 = new PlotRenderingInfo(new ChartRenderingInfo()); assertTrue(p1.equals(p2)); assertTrue(p2.equals(p1)); }
/** * Draws the axis on a Java 2D graphics device (such as the screen or a printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location. * @param plotArea the area within which the axes and data should be drawn (<code>null</code> * not permitted). * @param dataArea the area within which the data should be drawn (<code>null</code> not * permitted). * @param edge the location of the axis (<code>null</code> not permitted). * @param plotState collects information about the plot (<code>null</code> permitted). * * @return the axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState state = null; // if the axis is not visible, don't draw it... if (!isVisible()) { state = new AxisState(cursor); // even though the axis is not visible, we need ticks for the gridlines... List ticks = refreshTicks(g2, state, plotArea, dataArea, edge); state.setTicks(ticks); return state; } // draw the tick marks and labels... state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge); // // draw the marker band (if there is one)... // if (getMarkerBand() != null) { // if (edge == RectangleEdge.BOTTOM) { // cursor = cursor - getMarkerBand().getHeight(g2); // } // getMarkerBand().draw(g2, plotArea, dataArea, 0, cursor); // } // draw the axis label... state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state); return state; }
/** * Draws the axis. * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor position. * @param plotArea the plot area (<code>null</code> not permitted). * @param dataArea the data area (<code>null</code> not permitted). * @param edge the edge (<code>null</code> not permitted). * @param plotState collects information about the plot (<code>null</code> permitted). * * @return the axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState ret = super.draw(g2, cursor, plotArea, dataArea, edge, plotState); if (isAdvanceLineVisible()) { double xx = valueToJava2D(getRange().getUpperBound(), dataArea, edge); Line2D mark = null; g2.setStroke(getAdvanceLineStroke()); g2.setPaint(getAdvanceLinePaint()); if (edge == RectangleEdge.LEFT) { mark = new Line2D.Double(cursor, xx, cursor + dataArea.getWidth(), xx); } else if (edge == RectangleEdge.RIGHT) { mark = new Line2D.Double(cursor - dataArea.getWidth(), xx, cursor, xx); } else if (edge == RectangleEdge.TOP) { mark = new Line2D.Double(xx, cursor + dataArea.getHeight(), xx, cursor); } else if (edge == RectangleEdge.BOTTOM) { mark = new Line2D.Double(xx, cursor, xx, cursor - dataArea.getHeight()); } g2.draw(mark); } return ret; }
/** * Draws the axis on a Java 2D graphics device (such as the screen or a * printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location. * @param plotArea the area within which the axis should be drawn * (<code>null</code> not permitted). * @param dataArea the area within which the plot is being drawn * (<code>null</code> not permitted). * @param edge the location of the axis (<code>null</code> not permitted). * @param plotState collects information about the plot * (<code>null</code> permitted). * * @return The axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { // if the axis is not visible, don't draw it... if (!isVisible()) { return new AxisState(cursor); } if (isAxisLineVisible()) { drawAxisLine(g2, cursor, dataArea, edge); } // draw the category labels and axis label AxisState state = new AxisState(cursor); state = drawSubCategoryLabels( g2, plotArea, dataArea, edge, state, plotState ); state = drawCategoryLabels(g2, plotArea, dataArea, edge, state, plotState); state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state); return state; }
/** * Draws the axis on a Java 2D graphics device (such as the screen or a * printer). * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor location. * @param plotArea the area within which the axes and data should be drawn * (<code>null</code> not permitted). * @param dataArea the area within which the data should be drawn * (<code>null</code> not permitted). * @param edge the location of the axis (<code>null</code> not permitted). * @param plotState collects information about the plot * (<code>null</code> permitted). * * @return The axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState state = null; // if the axis is not visible, don't draw it... if (!isVisible()) { state = new AxisState(cursor); // even though the axis is not visible, we need ticks for the // gridlines... List ticks = refreshTicks(g2, state, dataArea, edge); state.setTicks(ticks); return state; } // draw the tick marks and labels... state = drawTickMarksAndLabels(g2, cursor, plotArea, dataArea, edge); // // draw the marker band (if there is one)... // if (getMarkerBand() != null) { // if (edge == RectangleEdge.BOTTOM) { // cursor = cursor - getMarkerBand().getHeight(g2); // } // getMarkerBand().draw(g2, plotArea, dataArea, 0, cursor); // } // draw the axis label... state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state); return state; }
/** * Clears the information recorded by this object. */ public void clear() { this.chartArea.setRect(0.0, 0.0, 0.0, 0.0); this.plotArea.setRect(0.0, 0.0, 0.0, 0.0); this.plotInfo = new PlotRenderingInfo(this); if (this.entities != null) { this.entities.clear(); } }
/** * Zooms in on a selected region. * * @param selection the selected region. */ public void zoom(Rectangle selection) { // get the origin of the zoom selection in the Java2D space used for // drawing the chart (that is, before any scaling to fit the panel) Point2D selectOrigin = translateScreenToJava2D( new Point(selection.x, selection.y)); PlotRenderingInfo plotInfo = this.info.getPlotInfo(); Rectangle scaledDataArea = getScreenDataArea( (int) (selection.x + selection.width)/2, (int) (selection.y + selection.height)/2); if ((selection.height > 0) && (selection.width > 0)) { double hLower = (selection.x - scaledDataArea.x) / (double) scaledDataArea.width; double hUpper = (selection.x + selection.width - scaledDataArea.x) / (double) scaledDataArea.width; double vLower = (scaledDataArea.y + scaledDataArea.height - selection.y - selection.height) / (double) scaledDataArea.height; double vUpper = (scaledDataArea.y + scaledDataArea.height - selection.y) / (double) scaledDataArea.height; Plot p = this.chart.getPlot(); if (p instanceof Zoomable) { Zoomable z = (Zoomable) p; if (z.getOrientation() == PlotOrientation.HORIZONTAL) { z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin); z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin); } else { z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin); z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin); } } } }
/** * Test the equals() method. */ public void testEquals() { PlotRenderingInfo p1 = new PlotRenderingInfo(new ChartRenderingInfo()); PlotRenderingInfo p2 = new PlotRenderingInfo(new ChartRenderingInfo()); assertTrue(p1.equals(p2)); assertTrue(p2.equals(p1)); p1.setPlotArea(new Rectangle(2, 3, 4, 5)); assertFalse(p1.equals(p2)); p2.setPlotArea(new Rectangle(2, 3, 4, 5)); assertTrue(p1.equals(p2)); p1.setDataArea(new Rectangle(2, 4, 6, 8)); assertFalse(p1.equals(p2)); p2.setDataArea(new Rectangle(2, 4, 6, 8)); assertTrue(p1.equals(p2)); p1.addSubplotInfo(new PlotRenderingInfo(null)); assertFalse(p1.equals(p2)); p2.addSubplotInfo(new PlotRenderingInfo(null)); assertTrue(p1.equals(p2)); p1.getSubplotInfo(0).setDataArea(new Rectangle(1, 2, 3, 4)); assertFalse(p1.equals(p2)); p2.getSubplotInfo(0).setDataArea(new Rectangle(1, 2, 3, 4)); assertTrue(p1.equals(p2)); }
/** * Zooms in on a selected region. * * @param selection the selected region. */ public void zoom(Rectangle2D selection) { // get the origin of the zoom selection in the Java2D space used for // drawing the chart (that is, before any scaling to fit the panel) Point2D selectOrigin = translateScreenToJava2D(new Point( (int) Math.ceil(selection.getX()), (int) Math.ceil(selection.getY()))); PlotRenderingInfo plotInfo = this.info.getPlotInfo(); Rectangle2D scaledDataArea = getScreenDataArea( (int) selection.getCenterX(), (int) selection.getCenterY()); if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) { double hLower = (selection.getMinX() - scaledDataArea.getMinX()) / scaledDataArea.getWidth(); double hUpper = (selection.getMaxX() - scaledDataArea.getMinX()) / scaledDataArea.getWidth(); double vLower = (scaledDataArea.getMaxY() - selection.getMaxY()) / scaledDataArea.getHeight(); double vUpper = (scaledDataArea.getMaxY() - selection.getMinY()) / scaledDataArea.getHeight(); Plot p = this.chart.getPlot(); if (p instanceof Zoomable) { Zoomable z = (Zoomable) p; if (z.getOrientation() == PlotOrientation.HORIZONTAL) { z.zoomDomainAxes(vLower, vUpper, plotInfo, selectOrigin); z.zoomRangeAxes(hLower, hUpper, plotInfo, selectOrigin); } else { z.zoomDomainAxes(hLower, hUpper, plotInfo, selectOrigin); z.zoomRangeAxes(vLower, vUpper, plotInfo, selectOrigin); } } } }
/** * Creates a new state for the renderer. * * @param info the plot rendering info. */ public StackedXYAreaRendererState(PlotRenderingInfo info) { super(info); this.seriesArea = null; this.line = new Line2D.Double(); this.lastSeriesPoints = new Stack(); this.currentSeriesPoints = new Stack(); }
/** * Initialises the renderer then returns the number of 'passes' through the * data that the renderer will require (usually just one). This method * will be called before the first item is rendered, giving the renderer * an opportunity to initialise any state information it wants to maintain. * The renderer can do nothing if it chooses. * * @param g2 the graphics device. * @param dataArea the area inside the axes. * @param plot the plot. * @param dataset the data. * @param info an optional info collection object to return data back to * the caller. * * @return The number of passes the renderer requires. */ public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset dataset, PlotRenderingInfo info) { // calculate the maximum allowed candle width from the axis... ValueAxis axis = plot.getDomainAxis(); double x1 = axis.getLowerBound(); double x2 = x1 + this.maxCandleWidthInMilliseconds; RectangleEdge edge = plot.getDomainAxisEdge(); double xx1 = axis.valueToJava2D(x1, dataArea, edge); double xx2 = axis.valueToJava2D(x2, dataArea, edge); this.maxCandleWidth = Math.abs(xx2 - xx1); // Absolute value, since the relative x // positions are reversed for horizontal orientation // calculate the highest volume in the dataset... if (this.drawVolume) { OHLCDataset highLowDataset = (OHLCDataset) dataset; this.maxVolume = 0.0; for (int series = 0; series < highLowDataset.getSeriesCount(); series++) { for (int item = 0; item < highLowDataset.getItemCount(series); item++) { double volume = highLowDataset.getVolumeValue(series, item); if (volume > this.maxVolume) { this.maxVolume = volume; } } } } return new XYItemRendererState(info); }
/** * Draws the block representing the specified item. * * @param g2 the graphics device. * @param state the state. * @param dataArea the data area. * @param info the plot rendering info. * @param plot the plot. * @param domainAxis the x-axis. * @param rangeAxis the y-axis. * @param dataset the dataset. * @param series the series index. * @param item the item index. * @param crosshairState the crosshair state. * @param pass the pass index. */ public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { double x = dataset.getXValue(series, item); double y = dataset.getYValue(series, item); double z = 0.0; if (dataset instanceof XYZDataset) { z = ((XYZDataset) dataset).getZValue(series, item); } Paint p = this.paintScale.getPaint(z); double xx0 = domainAxis.valueToJava2D(x + this.xOffset, dataArea, plot.getDomainAxisEdge()); double yy0 = rangeAxis.valueToJava2D(y + this.yOffset, dataArea, plot.getRangeAxisEdge()); double xx1 = domainAxis.valueToJava2D(x + this.blockWidth + this.xOffset, dataArea, plot.getDomainAxisEdge()); double yy1 = rangeAxis.valueToJava2D(y + this.blockHeight + this.yOffset, dataArea, plot.getRangeAxisEdge()); Rectangle2D block; PlotOrientation orientation = plot.getOrientation(); if (orientation.equals(PlotOrientation.HORIZONTAL)) { block = new Rectangle2D.Double(Math.min(yy0, yy1), Math.min(xx0, xx1), Math.abs(yy1 - yy0), Math.abs(xx0 - xx1)); } else { block = new Rectangle2D.Double(Math.min(xx0, xx1), Math.min(yy0, yy1), Math.abs(xx1 - xx0), Math.abs(yy1 - yy0)); } g2.setPaint(p); g2.fill(block); g2.setStroke(new BasicStroke(1.0f)); g2.draw(block); }
/** * Plots the data for a given series. * * @param g2 the drawing surface. * @param dataArea the data area. * @param info collects plot rendering info. * @param plot the plot. * @param dataset the dataset. * @param seriesIndex the series index. */ public void drawSeries(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, PolarPlot plot, XYDataset dataset, int seriesIndex) { Polygon poly = new Polygon(); int numPoints = dataset.getItemCount(seriesIndex); for (int i = 0; i < numPoints; i++) { double theta = dataset.getXValue(seriesIndex, i); double radius = dataset.getYValue(seriesIndex, i); Point p = plot.translateValueThetaRadiusToJava2D(theta, radius, dataArea); poly.addPoint(p.x, p.y); } g2.setPaint(getSeriesPaint(seriesIndex)); g2.setStroke(getSeriesStroke(seriesIndex)); if (isSeriesFilled(seriesIndex)) { Composite savedComposite = g2.getComposite(); g2.setComposite(AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 0.5f)); g2.fill(poly); g2.setComposite(savedComposite); } else { g2.draw(poly); } }
/** * Initialises the renderer and returns a state object that will be passed * to subsequent calls to the drawItem method. This method gets called * once at the start of the process of drawing a chart. * * @param g2 the graphics device. * @param dataArea the area in which the data is to be plotted. * @param plot the plot. * @param rendererIndex the renderer index. * @param info collects chart rendering information for return to caller. * * @return The renderer state. */ public CategoryItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, CategoryPlot plot, int rendererIndex, PlotRenderingInfo info) { Rectangle2D adjusted = new Rectangle2D.Double(dataArea.getX(), dataArea.getY() + getYOffset(), dataArea.getWidth() - getXOffset(), dataArea.getHeight() - getYOffset()); CategoryItemRendererState state = super.initialise(g2, adjusted, plot, rendererIndex, info); return state; }
/** * Draws the block representing the specified item. * * @param g2 * the graphics device. * @param state * the state. * @param dataArea * the data area. * @param info * the plot rendering info. * @param plot * the plot. * @param domainAxis * the x-axis. * @param rangeAxis * the y-axis. * @param dataset * the dataset. * @param series * the series index. * @param item * the item index. * @param crosshairState * the crosshair state. * @param pass * the pass index. */ @Override public void drawItem(Graphics2D g2, XYItemRendererState state, Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item, CrosshairState crosshairState, int pass) { Shape hotspot = null; EntityCollection entities = null; if (info != null) { entities = info.getOwner().getEntityCollection(); } double x = dataset.getXValue(series, item); double y = dataset.getYValue(series, item); double colorValue = ((XYZDataset) dataset).getZValue(series, item); double normalized = (colorValue - minColor) / (maxColor - minColor); if (Double.isNaN(x) || Double.isNaN(y)) { // can't draw anything return; } double transX = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge()); double transY = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge()); PlotOrientation orientation = plot.getOrientation(); Shape shape = getItemShape(series, item); if (orientation == PlotOrientation.HORIZONTAL) { shape = ShapeUtilities.createTranslatedShape(shape, transY, transX); } else if (orientation == PlotOrientation.VERTICAL) { shape = ShapeUtilities.createTranslatedShape(shape, transX, transY); } hotspot = shape; if (shape.intersects(dataArea)) { g2.setPaint(colorProvider.getPointColor(normalized)); g2.fill(shape); if (getDrawOutlines()) { if (getUseOutlinePaint()) { g2.setPaint(getItemOutlinePaint(series, item)); } else { g2.setPaint(getItemPaint(series, item)); } g2.setStroke(getItemOutlineStroke(series, item)); g2.draw(shape); } } // add an entity for the item... if (entities != null) { addEntity(entities, hotspot, dataset, series, item, transX, transY); } }
@Override public void zoom(Rectangle2D selection) { // get the origin of the zoom selection in the Java2D space used for // drawing the chart (that is, before any scaling to fit the panel) Point2D selectOrigin = translateScreenToJava2D(new Point((int) Math.ceil(selection.getX()), (int) Math.ceil(selection.getY()))); PlotRenderingInfo plotInfo = getChartRenderingInfo().getPlotInfo(); Rectangle2D scaledDataArea = getScreenDataArea((int) selection.getCenterX(), (int) selection.getCenterY()); if ((selection.getHeight() > 0) && (selection.getWidth() > 0)) { double hLower = (selection.getMinX() - scaledDataArea.getMinX()) / scaledDataArea.getWidth(); double hUpper = (selection.getMaxX() - scaledDataArea.getMinX()) / scaledDataArea.getWidth(); double vLower = (scaledDataArea.getMaxY() - selection.getMaxY()) / scaledDataArea.getHeight(); double vUpper = (scaledDataArea.getMaxY() - selection.getMinY()) / scaledDataArea.getHeight(); Plot p = getChart().getPlot(); if (p instanceof LinkAndBrushPlot) { PlotOrientation orientation = null; if (p instanceof XYPlot) { XYPlot xyPlot = (XYPlot) p; orientation = xyPlot.getOrientation(); } if (p instanceof CategoryPlot) { CategoryPlot categoryPlot = (CategoryPlot) p; orientation = categoryPlot.getOrientation(); } // here we tweak the notify flag on the plot so that only // one notification happens even though we update multiple // axes... boolean savedNotify = p.isNotify(); p.setNotify(false); LinkAndBrushPlot LABPlot = (LinkAndBrushPlot) p; List<Pair<Integer, Range>> zoomedDomainAxisRanges = new LinkedList<Pair<Integer, Range>>(); List<Pair<Integer, Range>> zoomedRangeAxisRanges = new LinkedList<Pair<Integer, Range>>(); if (orientation == PlotOrientation.HORIZONTAL) { zoomedDomainAxisRanges.addAll(LABPlot.calculateDomainAxesZoom(vLower, vUpper, zoomOnLinkAndBrushSelection)); zoomedRangeAxisRanges.addAll(LABPlot.calculateRangeAxesZoom(hLower, hUpper, plotInfo, selectOrigin, zoomOnLinkAndBrushSelection)); } else { zoomedDomainAxisRanges.addAll(LABPlot.calculateDomainAxesZoom(hLower, hUpper, zoomOnLinkAndBrushSelection)); zoomedRangeAxisRanges.addAll(LABPlot.calculateRangeAxesZoom(vLower, vUpper, plotInfo, selectOrigin, zoomOnLinkAndBrushSelection)); } p.setNotify(savedNotify); if (zoomOnLinkAndBrushSelection) { informLinkAndBrushSelectionListeners(new LinkAndBrushSelection(SelectionType.ZOOM_IN, zoomedDomainAxisRanges, zoomedRangeAxisRanges)); } else { informLinkAndBrushSelectionListeners(new LinkAndBrushSelection(SelectionType.SELECTION, zoomedDomainAxisRanges, zoomedRangeAxisRanges)); } } else { super.zoom(selection); } } }
/** * Draws the annotation. This method is called by the drawing code in the * {@link XYPlot} class, you don't normally need to call this method * directly. * * @param g2 the graphics device. * @param plot the plot. * @param dataArea the data area. * @param domainAxis the domain axis. * @param rangeAxis the range axis. * @param rendererIndex the renderer index. * @param info if supplied, this info object will be populated with * entity information. */ public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) { PlotOrientation orientation = plot.getOrientation(); AxisLocation domainAxisLocation = plot.getDomainAxisLocation(); AxisLocation rangeAxisLocation = plot.getRangeAxisLocation(); RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(domainAxisLocation, orientation); RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(rangeAxisLocation, orientation); float j2DX = (float) domainAxis.valueToJava2D(this.x, dataArea, domainEdge); float j2DY = (float) rangeAxis.valueToJava2D(this.y, dataArea, rangeEdge); float xx = 0.0f; float yy = 0.0f; if (orientation == PlotOrientation.HORIZONTAL) { xx = j2DY; yy = j2DX; } else if (orientation == PlotOrientation.VERTICAL) { xx = j2DX; yy = j2DY; } int w = this.image.getWidth(null); int h = this.image.getHeight(null); Rectangle2D imageRect = new Rectangle2D.Double(0, 0, w, h); Point2D anchorPoint = (Point2D) RectangleAnchor.coordinates(imageRect, this.anchor); xx = xx - (float) anchorPoint.getX(); yy = yy - (float) anchorPoint.getY(); g2.drawImage(this.image, (int) xx, (int) yy, null); String toolTip = getToolTipText(); String url = getURL(); if (toolTip != null || url != null) { addEntity(info, new Rectangle2D.Float(xx, yy, w, h), rendererIndex, toolTip, url); } }
/** * Draws the axis. * * @param g2 the graphics device (<code>null</code> not permitted). * @param cursor the cursor position. * @param plotArea the plot area (<code>null</code> not permitted). * @param dataArea the data area (<code>null</code> not permitted). * @param edge the edge (<code>null</code> not permitted). * @param plotState collects information about the plot * (<code>null</code> permitted). * * @return The axis state (never <code>null</code>). */ public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, Rectangle2D dataArea, RectangleEdge edge, PlotRenderingInfo plotState) { AxisState ret = super.draw( g2, cursor, plotArea, dataArea, edge, plotState ); if (isAdvanceLineVisible()) { double xx = valueToJava2D( getRange().getUpperBound(), dataArea, edge ); Line2D mark = null; g2.setStroke(getAdvanceLineStroke()); g2.setPaint(getAdvanceLinePaint()); if (edge == RectangleEdge.LEFT) { mark = new Line2D.Double( cursor, xx, cursor + dataArea.getWidth(), xx ); } else if (edge == RectangleEdge.RIGHT) { mark = new Line2D.Double( cursor - dataArea.getWidth(), xx, cursor, xx ); } else if (edge == RectangleEdge.TOP) { mark = new Line2D.Double( xx, cursor + dataArea.getHeight(), xx, cursor ); } else if (edge == RectangleEdge.BOTTOM) { mark = new Line2D.Double( xx, cursor, xx, cursor - dataArea.getHeight() ); } g2.draw(mark); } return ret; }