/** * Calculates the positions of the tick labels for the axis, storing the * results in the tick label list (ready for drawing). * * @param g2 * the graphics device. * @param state * the axis state. * @param dataArea * the area in which the plot should be drawn. * @param edge * the location of the axis. * * @return A list of ticks. * */ @Override public List<NumberTick> refreshTicks(Graphics2D g2, AxisState state, Rectangle2D dataArea, RectangleEdge edge) { List<NumberTick> result = new java.util.ArrayList<NumberTick>(); if (RectangleEdge.isTopOrBottom(edge)) { result = this.refreshTicksHorizontal(g2, dataArea, edge); } else if (RectangleEdge.isLeftOrRight(edge)) { result = this.refreshTicksVertical(g2, dataArea, edge); } int size = result.size(); NumberTick first = result.get(0); NumberTick last = result.get(size - 1); NumberTick newFirst = new NumberTick(0, "", first.getTextAnchor(), first.getRotationAnchor(), first.getAngle()); NumberTick newLast = new NumberTick(0, "", last.getTextAnchor(), last.getRotationAnchor(), last .getAngle()); result.set(0, newFirst); result.set(size - 1, newLast); return result; }
/** * Draw the radial gridlines - the rings. * * @param g2 the drawing surface. * @param plot the plot. * @param radialAxis the radial axis. * @param ticks the ticks. * @param dataArea the data area. */ public void drawRadialGridLines(Graphics2D g2, PolarPlot plot, ValueAxis radialAxis, List ticks, Rectangle2D dataArea) { g2.setFont(radialAxis.getTickLabelFont()); g2.setPaint(plot.getRadiusGridlinePaint()); g2.setStroke(plot.getRadiusGridlineStroke()); Point center = plot.translateValueThetaRadiusToJava2D(0.0, 0.0, dataArea); Iterator iterator = ticks.iterator(); while (iterator.hasNext()) { NumberTick tick = (NumberTick) iterator.next(); Point p = plot.translateValueThetaRadiusToJava2D( 90.0, tick.getNumber().doubleValue(), dataArea ); int r = p.x - center.x; int upperLeftX = center.x - r; int upperLeftY = center.y - r; int d = 2 * r; Ellipse2D ring = new Ellipse2D.Double(upperLeftX, upperLeftY, d, d); g2.setPaint(plot.getRadiusGridlinePaint()); g2.draw(ring); } }
/** * Returns a list of ticks for an axis at the top or bottom of the chart. * * @param g2 the graphics device. * @param dataArea the data area. * @param edge the edge. * * @return A list of ticks. */ protected List refreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) { Range range = getRange(); List ticks = new ArrayList(); double start = Math.floor(calculateLog(getLowerBound())); double end = Math.ceil(calculateLog(getUpperBound())); double current = start; while (current <= end) { double v = calculateValue(current); if (range.contains(v)) { ticks.add(new NumberTick(new Double(v), createTickLabel(v), TextAnchor.TOP_CENTER, TextAnchor.CENTER, 0.0)); } // add minor ticks (for gridlines) double next = Math.pow(this.base, current + this.tickUnit.getSize()); for (int i = 1; i < this.minorTickCount; i++) { double minorV = v + i * ((next - v) / this.minorTickCount); if (range.contains(minorV)) { ticks.add(new NumberTick(new Double(minorV), "", TextAnchor.TOP_CENTER, TextAnchor.CENTER, 0.0)); } } current = current + this.tickUnit.getSize(); } return ticks; }
/** * Returns a list of ticks for an axis at the left or right of the chart. * * @param g2 the graphics device. * @param dataArea the data area. * @param edge the edge. * * @return A list of ticks. */ protected List refreshTicksVertical(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) { Range range = getRange(); List ticks = new ArrayList(); double start = Math.floor(calculateLog(getLowerBound())); double end = Math.ceil(calculateLog(getUpperBound())); double current = start; while (current <= end) { double v = calculateValue(current); if (range.contains(v)) { ticks.add(new NumberTick(new Double(v), createTickLabel(v), TextAnchor.CENTER_RIGHT, TextAnchor.CENTER, 0.0)); } // add minor ticks (for gridlines) double next = Math.pow(this.base, current + this.tickUnit.getSize()); for (int i = 1; i < this.minorTickCount; i++) { double minorV = v + i * ((next - v) / this.minorTickCount); if (range.contains(minorV)) { ticks.add(new NumberTick(new Double(minorV), "", TextAnchor.CENTER_RIGHT, TextAnchor.CENTER, 0.0)); } } current = current + this.tickUnit.getSize(); } return ticks; }
/** * Draw the radial gridlines - the rings. * * @param g2 the drawing surface. * @param plot the plot. * @param radialAxis the radial axis. * @param ticks the ticks. * @param dataArea the data area. */ public void drawRadialGridLines(Graphics2D g2, PolarPlot plot, ValueAxis radialAxis, List ticks, Rectangle2D dataArea) { g2.setFont(radialAxis.getTickLabelFont()); g2.setPaint(plot.getRadiusGridlinePaint()); g2.setStroke(plot.getRadiusGridlineStroke()); double axisMin = radialAxis.getLowerBound(); Point center = plot.translateValueThetaRadiusToJava2D(axisMin, axisMin, dataArea); Iterator iterator = ticks.iterator(); while (iterator.hasNext()) { NumberTick tick = (NumberTick) iterator.next(); Point p = plot.translateValueThetaRadiusToJava2D(90.0, tick.getNumber().doubleValue(), dataArea); int r = p.x - center.x; int upperLeftX = center.x - r; int upperLeftY = center.y - r; int d = 2 * r; Ellipse2D ring = new Ellipse2D.Double(upperLeftX, upperLeftY, d, d); g2.setPaint(plot.getRadiusGridlinePaint()); g2.draw(ring); } }
/** * Draw the radial gridlines - the rings. * * @param g2 the drawing surface (<code>null</code> not permitted). * @param plot the plot (<code>null</code> not permitted). * @param radialAxis the radial axis (<code>null</code> not permitted). * @param ticks the ticks (<code>null</code> not permitted). * @param dataArea the data area. */ @Override public void drawRadialGridLines(Graphics2D g2, PolarPlot plot, ValueAxis radialAxis, List ticks, Rectangle2D dataArea) { ParamChecks.nullNotPermitted(radialAxis, "radialAxis"); g2.setFont(radialAxis.getTickLabelFont()); g2.setPaint(plot.getRadiusGridlinePaint()); g2.setStroke(plot.getRadiusGridlineStroke()); double centerValue; if (radialAxis.isInverted()) { centerValue = radialAxis.getUpperBound(); } else { centerValue = radialAxis.getLowerBound(); } Point center = plot.translateToJava2D(0, centerValue, radialAxis, dataArea); Iterator iterator = ticks.iterator(); while (iterator.hasNext()) { NumberTick tick = (NumberTick) iterator.next(); double angleDegrees = plot.isCounterClockwise() ? plot.getAngleOffset() : -plot.getAngleOffset(); Point p = plot.translateToJava2D(angleDegrees, tick.getNumber().doubleValue(), radialAxis, dataArea); int r = p.x - center.x; int upperLeftX = center.x - r; int upperLeftY = center.y - r; int d = 2 * r; Ellipse2D ring = new Ellipse2D.Double(upperLeftX, upperLeftY, d, d); g2.setPaint(plot.getRadiusGridlinePaint()); g2.draw(ring); } }
/** * Generates a list of tick values for the angular tick marks. * * @return A list of {@link NumberTick} instances. * * @since 1.0.10 */ protected List refreshAngleTicks() { List ticks = new ArrayList(); for (double currentTickVal = 0.0; currentTickVal < 360.0; currentTickVal += this.angleTickUnit.getSize()) { TextAnchor ta = calculateTextAnchor(currentTickVal); NumberTick tick = new NumberTick(new Double(currentTickVal), this.angleTickUnit.valueToString(currentTickVal), ta, TextAnchor.CENTER, 0.0); ticks.add(tick); } return ticks; }
/** * Draw the radial gridlines - the rings. * * @param g2 the drawing surface ({@code null} not permitted). * @param plot the plot ({@code null} not permitted). * @param radialAxis the radial axis ({@code null} not permitted). * @param ticks the ticks ({@code null} not permitted). * @param dataArea the data area. */ @Override public void drawRadialGridLines(Graphics2D g2, PolarPlot plot, ValueAxis radialAxis, List ticks, Rectangle2D dataArea) { Args.nullNotPermitted(radialAxis, "radialAxis"); g2.setFont(radialAxis.getTickLabelFont()); g2.setPaint(plot.getRadiusGridlinePaint()); g2.setStroke(plot.getRadiusGridlineStroke()); double centerValue; if (radialAxis.isInverted()) { centerValue = radialAxis.getUpperBound(); } else { centerValue = radialAxis.getLowerBound(); } Point center = plot.translateToJava2D(0, centerValue, radialAxis, dataArea); Iterator iterator = ticks.iterator(); while (iterator.hasNext()) { NumberTick tick = (NumberTick) iterator.next(); double angleDegrees = plot.isCounterClockwise() ? plot.getAngleOffset() : -plot.getAngleOffset(); Point p = plot.translateToJava2D(angleDegrees, tick.getNumber().doubleValue(), radialAxis, dataArea); int r = p.x - center.x; int upperLeftX = center.x - r; int upperLeftY = center.y - r; int d = 2 * r; Ellipse2D ring = new Ellipse2D.Double(upperLeftX, upperLeftY, d, d); g2.setPaint(plot.getRadiusGridlinePaint()); g2.draw(ring); } }
/** * Generates a list of tick values for the angular tick marks. * * @return A list of {@link NumberTick} instances. * * @since 1.0.10 */ protected List refreshAngleTicks() { List ticks = new ArrayList(); for (double currentTickVal = 0.0; currentTickVal < 360.0; currentTickVal += this.angleTickUnit.getSize()) { NumberTick tick = new NumberTick(new Double(currentTickVal), this.angleTickUnit.valueToString(currentTickVal), TextAnchor.CENTER, TextAnchor.CENTER, 0.0); ticks.add(tick); } return ticks; }
private void addHorizontalTicks(RectangleEdge edge, List ticks, double lowerBoundVal, String tickLabel, double tickVal) { if (tickVal >= lowerBoundVal - SMALL_LOG_VALUE) { //tick value not below lowest data value TextAnchor anchor; TextAnchor rotationAnchor; double angle = 0.0; if (isVerticalTickLabels()) { anchor = TextAnchor.CENTER_RIGHT; rotationAnchor = TextAnchor.CENTER_RIGHT; if (edge == RectangleEdge.TOP) { angle = Math.PI / 2.0; } else { angle = -Math.PI / 2.0; } } else { if (edge == RectangleEdge.TOP) { anchor = TextAnchor.BOTTOM_CENTER; rotationAnchor = TextAnchor.BOTTOM_CENTER; } else { anchor = TextAnchor.TOP_CENTER; rotationAnchor = TextAnchor.TOP_CENTER; } } ticks.add(new NumberTick(new Double(tickVal), tickLabel, anchor, rotationAnchor, angle)); } }
private void addVerticalTicks(RectangleEdge edge, List ticks, double lowerBoundVal, String tickLabel, double tickVal) { if (tickVal >= lowerBoundVal - SMALL_LOG_VALUE) { //tick value not below lowest data value TextAnchor anchor; TextAnchor rotationAnchor; double angle = 0.0; if (isVerticalTickLabels()) { if (edge == RectangleEdge.LEFT) { anchor = TextAnchor.BOTTOM_CENTER; rotationAnchor = TextAnchor.BOTTOM_CENTER; angle = -Math.PI / 2.0; } else { anchor = TextAnchor.BOTTOM_CENTER; rotationAnchor = TextAnchor.BOTTOM_CENTER; angle = Math.PI / 2.0; } } else { if (edge == RectangleEdge.LEFT) { anchor = TextAnchor.CENTER_RIGHT; rotationAnchor = TextAnchor.CENTER_RIGHT; } else { anchor = TextAnchor.CENTER_LEFT; rotationAnchor = TextAnchor.CENTER_LEFT; } } //create tick object and add to list: ticks.add(new NumberTick(new Double(tickVal), tickLabel, anchor, rotationAnchor, angle)); } }
@Test public void testRefreshTicksForNegativeAxisRange() throws Exception { CustomLogarithmicAxis testAxis = new CustomLogarithmicAxis("testAxis"); testAxis.setRange(-300, -3); List ticks = testAxis.refreshTicks(RectangleEdge.TOP, CustomLogarithmicAxis.HORIZONTAL); assertNotNull(ticks); assertEquals(19, ticks.size()); // we expect labels at the power of 10 values only assertEquals("", ((NumberTick) ticks.get(0)).getText()); assertEquals("", ((NumberTick) ticks.get(1)).getText()); assertEquals("-10", ((NumberTick) ticks.get(7)).getText()); assertEquals("", ((NumberTick) ticks.get(9)).getText()); assertEquals("-100", ((NumberTick) ticks.get(16)).getText()); assertEquals("", ((NumberTick) ticks.get(18)).getText()); }
@Test public void testRefreshTicksVerticalAxisOrientation() throws Exception { CustomLogarithmicAxis testAxis = new CustomLogarithmicAxis("testAxis"); testAxis.setRange(-300, -3); List ticks = testAxis.refreshTicks(RectangleEdge.LEFT, CustomLogarithmicAxis.VERTICAL); assertNotNull(ticks); assertEquals(19, ticks.size()); assertEquals("", ((NumberTick) ticks.get(0)).getText()); assertEquals("", ((NumberTick) ticks.get(1)).getText()); assertEquals("-10", ((NumberTick) ticks.get(7)).getText()); assertEquals("", ((NumberTick) ticks.get(9)).getText()); assertEquals("-100", ((NumberTick) ticks.get(16)).getText()); assertEquals("", ((NumberTick) ticks.get(18)).getText()); }
private List<NumberTick> createTicks(Graphics2D g2, Rectangle2D dataArea) { List<NumberTick> ticks = Lists.newArrayList(); for(Partition p : partitions) { ticks.addAll(createTicks(p, g2, dataArea)); } return ticks; }