From: David G. <mu...@us...> - 2005-11-10 11:20:06
|
Update of /cvsroot/jfreechart/jfreechart/source/org/jfree/chart/plot In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27066/source/org/jfree/chart/plot Modified Files: Tag: STABLE_1-0-0 MeterPlot.java Log Message: 2005-11-10 David Gilbert <dav...@ob...> * source/org/jfree/chart/plot/MeterPlot.java (tickSize): New field, (tickPaint): New field, (tickLabelPaint): New field, (MeterPlot(ValueDataset)): Initialise new fields, (getTickSize): New method, (setTickSize): New method, (getTickPaint): New method, (setTickPaint): New method, (getTickLabelPaint): New method, (setTickLabelPaint): New method, (getLegendItems): Use background color for each interval, (draw): Call new drawValueLabel() method, (drawArcForInterval): Updated for refactored methods, (fillArc): Changed from private to protected, (drawTicks): Make use of new tickSize field, (drawTick(Graphics2D, Rectangle2D, double)): Updated for refactored methods, (drawTick(Graphics2D, Rectangle2D, double, boolean, Paint, boolean, String)): Removed special case code for drawing the value label, and the now redundant parameters, replaced with drawValueLabel(), (drawValueLabel): New method, (equals): Updated for new fields. Index: MeterPlot.java =================================================================== RCS file: /cvsroot/jfreechart/jfreechart/source/org/jfree/chart/plot/MeterPlot.java,v retrieving revision 1.13.2.3 retrieving revision 1.13.2.4 diff -C2 -d -r1.13.2.3 -r1.13.2.4 *** MeterPlot.java 25 Oct 2005 20:52:07 -0000 1.13.2.3 --- MeterPlot.java 10 Nov 2005 11:19:42 -0000 1.13.2.4 *************** *** 76,79 **** --- 76,81 ---- * 05-May-2005 : Updated draw() method parameters (DG); * 08-Jun-2005 : Fixed equals() method to handle GradientPaint (DG); + * 10-Nov-2005 : Added tickPaint, tickSize and valuePaint attributes, and + * put value label drawing code into a separate method (DG); * */ *************** *** 116,119 **** --- 118,122 ---- import org.jfree.text.TextUtilities; import org.jfree.ui.RectangleInsets; + import org.jfree.ui.TextAnchor; import org.jfree.util.ObjectUtilities; import org.jfree.util.PaintUtilities; *************** *** 166,169 **** --- 169,178 ---- private Range range; + /** The tick size. */ + private double tickSize; + + /** The paint used to draw the ticks. */ + private Paint tickPaint; + /** The units displayed on the dial. */ private String units; *************** *** 193,196 **** --- 202,208 ---- private Font tickLabelFont; + /** The tick label paint. */ + private Paint tickLabelPaint; + /** The tick label format. */ private NumberFormat tickLabelFormat; *************** *** 224,231 **** --- 236,246 ---- this.meterAngle = DEFAULT_METER_ANGLE; this.range = new Range(0.0, 100.0); + this.tickSize = 10.0; + this.tickPaint = Color.white; this.units = "Units"; this.needlePaint = MeterPlot.DEFAULT_NEEDLE_PAINT; this.tickLabelsVisible = true; this.tickLabelFont = MeterPlot.DEFAULT_LABEL_FONT; + this.tickLabelPaint = Color.black; this.tickLabelFormat = NumberFormat.getInstance(); this.valueFont = MeterPlot.DEFAULT_VALUE_FONT; *************** *** 313,316 **** --- 328,377 ---- notifyListeners(new PlotChangeEvent(this)); } + + /** + * Returns the tick size (the interval between ticks on the dial). + * + * @return The tick size. + */ + public double getTickSize() { + return this.tickSize; + } + + /** + * Sets the tick size and sends a {@link PlotChangeEvent} to all + * registered listeners. + * + * @param size the tick size (must be > 0). + */ + public void setTickSize(double size) { + if (size <= 0) { + throw new IllegalArgumentException("Requires 'size' > 0."); + } + this.tickSize = size; + notifyListeners(new PlotChangeEvent(this)); + } + + /** + * Returns the paint used to draw the ticks around the dial. + * + * @return The paint used to draw the ticks around the dial (never + * <code>null</code>). + */ + public Paint getTickPaint() { + return this.tickPaint; + } + + /** + * Sets the paint used to draw the tick labels around the dial. + * + * @param paint the paint (<code>null</code> not permitted). + */ + public void setTickPaint(Paint paint) { + if (paint == null) { + throw new IllegalArgumentException("Null 'paint' argument."); + } + this.tickPaint = paint; + notifyListeners(new PlotChangeEvent(this)); + } /** *************** *** 405,408 **** --- 466,494 ---- /** + * Returns the tick label paint. + * + * @return The paint (never <code>null</code>). + */ + public Paint getTickLabelPaint() { + return this.tickLabelPaint; + } + + /** + * Sets the tick label paint and sends a {@link PlotChangeEvent} to all + * registered listeners. + * + * @param paint the paint (<code>null</code> not permitted). + */ + public void setTickLabelPaint(Paint paint) { + if (paint == null) { + throw new IllegalArgumentException("Null 'paint' argument."); + } + if (!this.tickLabelPaint.equals(paint)) { + this.tickLabelPaint = paint; + notifyListeners(new PlotChangeEvent(this)); + } + } + + /** * Returns the tick label format. * *************** *** 615,621 **** while (iterator.hasNext()) { MeterInterval mi = (MeterInterval) iterator.next(); LegendItem item = new LegendItem(mi.getLabel(), null, mi.getLabel(), null, null, new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0), ! mi.getOutlinePaint()); result.add(item); } --- 701,711 ---- while (iterator.hasNext()) { MeterInterval mi = (MeterInterval) iterator.next(); + Paint color = mi.getBackgroundPaint(); + if (color == null) { + color = mi.getOutlinePaint(); + } LegendItem item = new LegendItem(mi.getLabel(), null, mi.getLabel(), null, null, new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0), ! color); result.add(item); } *************** *** 720,727 **** if (n != null) { double value = n.doubleValue(); ! drawTick( ! g2, meterArea, value, true, this.valuePaint, true, ! getUnits() ! ); if (this.range.contains(value)) { --- 810,814 ---- if (n != null) { double value = n.doubleValue(); ! drawValueLabel(g2, meterArea); if (this.range.contains(value)) { *************** *** 808,813 **** ); } ! drawTick(g2, meterArea, minValue, true, outlinePaint); ! drawTick(g2, meterArea, maxValue, true, outlinePaint); } } --- 895,900 ---- ); } ! drawTick(g2, meterArea, minValue, true); ! drawTick(g2, meterArea, maxValue, true); } } *************** *** 857,863 **** * @param paint the background paint (<code>null</code> not permitted). */ ! private void fillArc(Graphics2D g2, Rectangle2D area, ! double minValue, double maxValue, Paint paint, ! boolean dial) { if (paint == null) { throw new IllegalArgumentException("Null 'paint' argument"); --- 944,950 ---- * @param paint the background paint (<code>null</code> not permitted). */ ! protected void fillArc(Graphics2D g2, Rectangle2D area, ! double minValue, double maxValue, Paint paint, ! boolean dial) { if (paint == null) { throw new IllegalArgumentException("Null 'paint' argument"); *************** *** 914,918 **** /** ! * Draws the 20 ticks that subdivide the overall range. * * @param g2 the graphics device. --- 1001,1005 ---- /** ! * Draws the ticks that subdivide the overall range. * * @param g2 the graphics device. *************** *** 923,929 **** protected void drawTicks(Graphics2D g2, Rectangle2D meterArea, double minValue, double maxValue) { ! int numberOfTicks = 20; ! double diff = (maxValue - minValue) / numberOfTicks; ! for (double v = minValue; v <= maxValue; v += diff) { drawTick(g2, meterArea, v); } --- 1010,1014 ---- protected void drawTicks(Graphics2D g2, Rectangle2D meterArea, double minValue, double maxValue) { ! for (double v = minValue; v <= maxValue; v += tickSize) { drawTick(g2, meterArea, v); } *************** *** 938,962 **** */ protected void drawTick(Graphics2D g2, Rectangle2D meterArea, ! double value) { ! drawTick(g2, meterArea, value, false, null, false, null); ! } ! ! /** ! * Draws a tick. ! * ! * @param g2 the graphics device. ! * @param meterArea the meter area. ! * @param value the value. ! * @param label display a label? ! * @param paint the paint. ! */ ! protected void drawTick(Graphics2D g2, Rectangle2D meterArea, double value, ! boolean label, Paint paint) { ! drawTick(g2, meterArea, value, label, paint, false, null); } /** ! * Draws a tick on the chart (also handles a special case [curValue=true] ! * that draws the value in the middle of the dial). * * @param g2 the graphics device. --- 1023,1032 ---- */ protected void drawTick(Graphics2D g2, Rectangle2D meterArea, ! double value) { ! drawTick(g2, meterArea, value, false); } /** ! * Draws a tick on the dial. * * @param g2 the graphics device. *************** *** 964,974 **** * @param value the tick value. * @param label a flag that controls whether or not a value label is drawn. - * @param labelPaint the label color. - * @param curValue a flag for the special case of the current value. - * @param units the unit-of-measure for the dial. */ protected void drawTick(Graphics2D g2, Rectangle2D meterArea, ! double value, boolean label, Paint labelPaint, ! boolean curValue, String units) { double valueAngle = valueToAngle(value); --- 1034,1040 ---- * @param value the tick value. * @param label a flag that controls whether or not a value label is drawn. */ protected void drawTick(Graphics2D g2, Rectangle2D meterArea, ! double value, boolean label) { double valueAngle = valueToAngle(value); *************** *** 977,984 **** double meterMiddleY = meterArea.getCenterY(); ! if (labelPaint == null) { ! labelPaint = Color.white; ! } ! g2.setPaint(labelPaint); g2.setStroke(new BasicStroke(2.0f)); --- 1043,1047 ---- double meterMiddleY = meterArea.getCenterY(); ! g2.setPaint(this.tickPaint); g2.setStroke(new BasicStroke(2.0f)); *************** *** 986,1028 **** double valueP2Y = 0; ! if (!curValue) { ! double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE; ! double radius1 = radius - 15; ! double valueP1X = meterMiddleX + (radius * Math.cos(Math.PI * (valueAngle / 180))); ! double valueP1Y = meterMiddleY - (radius * Math.sin(Math.PI * (valueAngle / 180))); ! valueP2X = meterMiddleX ! + (radius1 * Math.cos(Math.PI * (valueAngle / 180))); ! valueP2Y = meterMiddleY ! - (radius1 * Math.sin(Math.PI * (valueAngle / 180))); ! Line2D.Double line = new Line2D.Double( ! valueP1X, valueP1Y, valueP2X, valueP2Y ! ); ! g2.draw(line); ! } ! else { ! valueP2X = meterMiddleX; ! valueP2Y = meterMiddleY; ! valueAngle = 90; ! } if (this.tickLabelsVisible && label) { String tickLabel = this.tickLabelFormat.format(value); ! if (curValue && units != null) { ! tickLabel += " " + units; ! } ! if (curValue) { ! g2.setFont(getValueFont()); ! } ! else { ! if (this.tickLabelFont != null) { ! g2.setFont(this.tickLabelFont); ! } ! } FontMetrics fm = g2.getFontMetrics(); --- 1049,1074 ---- double valueP2Y = 0; ! double radius = (meterArea.getWidth() / 2) + DEFAULT_BORDER_SIZE; ! double radius1 = radius - 15; ! double valueP1X = meterMiddleX + (radius * Math.cos(Math.PI * (valueAngle / 180))); ! double valueP1Y = meterMiddleY - (radius * Math.sin(Math.PI * (valueAngle / 180))); ! valueP2X = meterMiddleX ! + (radius1 * Math.cos(Math.PI * (valueAngle / 180))); ! valueP2Y = meterMiddleY ! - (radius1 * Math.sin(Math.PI * (valueAngle / 180))); ! Line2D.Double line = new Line2D.Double(valueP1X, valueP1Y, valueP2X, ! valueP2Y); ! g2.draw(line); if (this.tickLabelsVisible && label) { String tickLabel = this.tickLabelFormat.format(value); ! g2.setFont(this.tickLabelFont); ! g2.setPaint(this.tickLabelPaint); FontMetrics fm = g2.getFontMetrics(); *************** *** 1032,1038 **** double x = valueP2X; double y = valueP2Y; - if (curValue) { - y += DEFAULT_CIRCLE_SIZE; - } if (valueAngle == 90 || valueAngle == 270) { x = x - tickLabelBounds.getWidth() / 2; --- 1078,1081 ---- *************** *** 1051,1054 **** --- 1094,1120 ---- } } + + /** + * Draws the value label just below the center of the dial. + * + * @param g2 the graphics device. + * @param area the plot area. + */ + protected void drawValueLabel(Graphics2D g2, Rectangle2D area) { + g2.setFont(this.valueFont); + g2.setPaint(this.valuePaint); + String valueStr = "No value"; + if (dataset != null) { + Number n = dataset.getValue(); + if (n != null) { + valueStr = this.tickLabelFormat.format(n.doubleValue()) + " " + + this.units; + } + } + float x = (float) area.getCenterX(); + float y = (float) area.getCenterY() + DEFAULT_CIRCLE_SIZE; + TextUtilities.drawAlignedString(valueStr, g2, x, y, + TextAnchor.TOP_CENTER); + } /** *************** *** 1120,1123 **** --- 1186,1195 ---- return false; } + if (!PaintUtilities.equal(this.tickPaint, that.tickPaint)) { + return false; + } + if (this.tickSize != that.tickSize) { + return false; + } if (this.tickLabelsVisible != that.tickLabelsVisible) { return false; *************** *** 1126,1129 **** --- 1198,1204 ---- return false; } + if (!PaintUtilities.equal(this.tickLabelPaint, that.tickLabelPaint)) { + return false; + } if (!ObjectUtilities.equal(this.tickLabelFormat, that.tickLabelFormat)) { |