Skip to content

Commit

Permalink
Integrate changes to MeterPlot from #231
Browse files Browse the repository at this point in the history
  • Loading branch information
jfree committed Jun 18, 2021
1 parent e2d6118 commit 0606090
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 54 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ History
##### Version 1.5.4 (not-yet-released)
- add new methods to access maps for datasets, renderers and axes in plots ([#201](https://github.com/jfree/jfreechart/issues/201));
- fix tick label font for `LogAxis` with number format override ([#98](https://github.com/jfree/jfreechart/issues/98));
- added `valueVisible` flag to `MeterPlot` ([#231](https://github.com/jfree/jfreechart/pull/231));
- add argument checks for annotations ([#223](https://github.com/jfree/jfreechart/issues/223));


Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<artifactId>jfreechart</artifactId>
<groupId>org.jfree</groupId>
<version>1.5.3</version>
<version>1.5.4-SNAPSHOT</version>
<packaging>jar</packaging>

<organization>
Expand Down
114 changes: 71 additions & 43 deletions src/main/java/org/jfree/chart/plot/MeterPlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@

package org.jfree.chart.plot;

import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.text.TextUtils;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.SerialUtils;
import org.jfree.data.Range;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.ValueDataset;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
Expand All @@ -61,26 +74,12 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.event.PlotChangeEvent;
import org.jfree.chart.text.TextUtils;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.util.PaintUtils;
import org.jfree.chart.util.Args;
import org.jfree.chart.util.ResourceBundleWrapper;
import org.jfree.chart.util.SerialUtils;
import org.jfree.data.Range;
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.ValueDataset;

/**
* A plot that displays a single value in the form of a needle on a dial.
* Defined ranges (for example, 'normal', 'warning' and 'critical') can be
Expand Down Expand Up @@ -143,6 +142,9 @@ public class MeterPlot extends Plot implements Serializable, Cloneable {
/** The paint for the value displayed in the center of the dial. */
private transient Paint valuePaint;

/** A flag that indicates whether the value is visible. */
private boolean valueVisible = true;

/** A flag that controls whether or not the border is drawn. */
private boolean drawBorder;

Expand All @@ -169,14 +171,13 @@ public class MeterPlot extends Plot implements Serializable, Cloneable {

/** The resourceBundle for the localization. */
protected static ResourceBundle localizationResources
= ResourceBundleWrapper.getBundle(
"org.jfree.chart.plot.LocalizationBundle");
= ResourceBundle.getBundle("org.jfree.chart.plot.LocalizationBundle");

/**
* A (possibly empty) list of the {@link MeterInterval}s to be highlighted
* on the dial.
*/
private List intervals;
private List<MeterInterval> intervals;

/**
* Creates a new plot with a default range of {@code 0} to {@code 100} and
Expand Down Expand Up @@ -207,7 +208,7 @@ public MeterPlot(ValueDataset dataset) {
this.valueFont = MeterPlot.DEFAULT_VALUE_FONT;
this.valuePaint = MeterPlot.DEFAULT_VALUE_PAINT;
this.dialBackgroundPaint = MeterPlot.DEFAULT_DIAL_BACKGROUND_PAINT;
this.intervals = new java.util.ArrayList();
this.intervals = new ArrayList<>();
setDataset(dataset);
}

Expand Down Expand Up @@ -477,6 +478,33 @@ public void setTickLabelPaint(Paint paint) {
}
}

/**
* Returns the flag that controls whether or not the value is visible.
* The default value is {@code true}.
*
* @return A flag.
*
* @see #setValueVisible
* @since 1.5.4
*/
public boolean isValueVisible() {
return valueVisible;
}

/**
* Sets the flag that controls whether or not the value is visible
* and sends a change event to all registered listeners.
*
* @param valueVisible the new flag value.
*
* @see #isValueVisible()
* @since 1.5.4
*/
public void setValueVisible(boolean valueVisible) {
this.valueVisible = valueVisible;
fireChangeEvent();
}

/**
* Returns the tick label format.
*
Expand Down Expand Up @@ -675,8 +703,8 @@ public void setDataset(ValueDataset dataset) {
*
* @see #addInterval(MeterInterval)
*/
public List getIntervals() {
return Collections.unmodifiableList(this.intervals);
public List<MeterInterval> getIntervals() {
return Collections.unmodifiableList(intervals);
}

/**
Expand Down Expand Up @@ -713,9 +741,7 @@ public void clearIntervals() {
@Override
public LegendItemCollection getLegendItems() {
LegendItemCollection result = new LegendItemCollection();
Iterator iterator = this.intervals.iterator();
while (iterator.hasNext()) {
MeterInterval mi = (MeterInterval) iterator.next();
for (MeterInterval mi : intervals) {
Paint color = mi.getBackgroundPaint();
if (color == null) {
color = mi.getOutlinePaint();
Expand Down Expand Up @@ -805,9 +831,7 @@ public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,
drawArcForInterval(g2, meterArea, new MeterInterval("", this.range,
this.dialOutlinePaint, new BasicStroke(1.0f), null));

Iterator iterator = this.intervals.iterator();
while (iterator.hasNext()) {
MeterInterval interval = (MeterInterval) iterator.next();
for (MeterInterval interval : this.intervals) {
drawArcForInterval(g2, meterArea, interval);
}

Expand Down Expand Up @@ -990,7 +1014,7 @@ else if (this.shape == DialShape.CIRCLE) {
*/
public double valueToAngle(double value) {
value = value - this.range.getLowerBound();
double baseAngle = 180 + ((this.meterAngle - 180) / 2);
double baseAngle = 180 + ((this.meterAngle - 180) / 2.0);
return baseAngle - ((value / this.range.getLength()) * this.meterAngle);
}

Expand Down Expand Up @@ -1096,20 +1120,22 @@ else if (valueAngle < 90 || valueAngle > 270) {
* @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 (this.dataset != null) {
Number n = this.dataset.getValue();
if (n != null) {
valueStr = this.tickLabelFormat.format(n.doubleValue()) + " "
+ this.units;
if (valueVisible) {
g2.setFont(this.valueFont);
g2.setPaint(this.valuePaint);
String valueStr = "No value";
if (this.dataset != null) {
Number n = this.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;
TextUtils.drawAlignedString(valueStr, g2, x, y,
float x = (float) area.getCenterX();
float y = (float) area.getCenterY() + DEFAULT_CIRCLE_SIZE;
TextUtils.drawAlignedString(valueStr, g2, x, y,
TextAnchor.TOP_CENTER);
}
}

/**
Expand Down Expand Up @@ -1177,6 +1203,9 @@ public boolean equals(Object obj) {
if (!PaintUtils.equal(this.needlePaint, that.needlePaint)) {
return false;
}
if (this.valueVisible != that.valueVisible) {
return false;
}
if (!Objects.equals(this.valueFont, that.valueFont)) {
return false;
}
Expand All @@ -1198,8 +1227,7 @@ public boolean equals(Object obj) {
if (!PaintUtils.equal(this.tickLabelPaint, that.tickLabelPaint)) {
return false;
}
if (!Objects.equals(this.tickLabelFormat,
that.tickLabelFormat)) {
if (!Objects.equals(this.tickLabelFormat, that.tickLabelFormat)) {
return false;
}
if (this.drawBorder != that.drawBorder) {
Expand Down Expand Up @@ -1265,7 +1293,7 @@ public Object clone() throws CloneNotSupportedException {
MeterPlot clone = (MeterPlot) super.clone();
clone.tickLabelFormat = (NumberFormat) this.tickLabelFormat.clone();
// the following relies on the fact that the intervals are immutable
clone.intervals = new java.util.ArrayList(this.intervals);
clone.intervals = new ArrayList<>(this.intervals);
if (clone.dataset != null) {
clone.dataset.addChangeListener(clone);
}
Expand Down
18 changes: 8 additions & 10 deletions src/test/java/org/jfree/chart/plot/MeterPlotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2020, by Object Refinery Limited and Contributors.
* (C) Copyright 2000-2021, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
Expand All @@ -27,18 +27,10 @@
* ------------------
* MeterPlotTest.java
* ------------------
* (C) Copyright 2003-2020, by Object Refinery Limited and Contributors.
* (C) Copyright 2003-2021, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 27-Mar-2003 : Version 1 (DG);
* 12-May-2004 : Updated testEquals() (DG);
* 29-Nov-2007 : Updated testEquals() and testSerialization1() for
* dialOutlinePaint (DG)
*
*/

package org.jfree.chart.plot;
Expand Down Expand Up @@ -129,6 +121,12 @@ public void testEquals() {
7.0f, 6.0f, Color.BLUE));
assertTrue(plot1.equals(plot2));

// value visible
plot1.setValueVisible(false);
assertFalse(plot1.equals(plot2));
plot2.setValueVisible(false);
assertTrue(plot1.equals(plot2));

// value font
plot1.setValueFont(new Font("Serif", Font.PLAIN, 6));
assertFalse(plot1.equals(plot2));
Expand Down

0 comments on commit 0606090

Please sign in to comment.