Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/org/jfree/chart/plot/pie/PiePlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@
*/
public class PiePlot<K extends Comparable<K>> extends Plot implements Cloneable, Serializable {

/** Error message for negative values in the dataset. */
private static final String negativeValueExceptionMessage = "Only positive values are allowed in the dataset.";

/** For serialization. */
private static final long serialVersionUID = -795612466005590431L;

Expand Down Expand Up @@ -2396,6 +2399,9 @@ protected void drawSimpleLabels(Graphics2D g2, List<K> keys,
if (n == null) {
include = !getIgnoreNullValues();
}
else if(n != null && n.doubleValue() < 0.0){
throw new IllegalArgumentException(negativeValueExceptionMessage);
}
else {
v = n.doubleValue();
include = getIgnoreZeroValues() ? v > 0.0 : v >= 0.0;
Expand Down Expand Up @@ -2489,6 +2495,9 @@ protected void drawLabels(Graphics2D g2, List<K> keys, double totalValue,
if (n == null) {
include = !this.ignoreNullValues;
}
else if(n != null && n.doubleValue() < 0.0){
throw new IllegalArgumentException(negativeValueExceptionMessage);
}
else {
v = n.doubleValue();
include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
Expand Down Expand Up @@ -2709,6 +2718,9 @@ public LegendItemCollection getLegendItems() {
}
section++;
}
else if(n != null && n.doubleValue() < 0.0){
throw new IllegalArgumentException(negativeValueExceptionMessage);
}
else {
section++;
}
Expand Down
41 changes: 38 additions & 3 deletions src/test/java/org/jfree/chart/plot/pie/PiePlotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,8 @@ public void testSerialization() {
}

/**
* Some checks for the getLegendItems() method.
* Some checks for the getLegendItems() method
* that uses only positive values in the dataset.
*/
@Test
public void testGetLegendItems() {
Expand All @@ -541,11 +542,45 @@ public void testGetLegendItems() {
plot.setIgnoreZeroValues(true);
items = plot.getLegendItems();
assertEquals(2, items.getItemCount());
}

// check that negative items are always ignored
dataset.setValue("Item 5", -1.0);
/**
* Some checks for the getLegendItems() method
* that uses positive and negative values in the dataset.
*/
@Test
public void testGetLegendItemsThrowErrorForNegativeValues() {
DefaultPieDataset<String> dataset = new DefaultPieDataset<>();
dataset.setValue("Item 1", 1.0);
dataset.setValue("Item 2", 2.0);
dataset.setValue("Item 3", 0.0);
dataset.setValue("Item 4", null);

PiePlot plot = new PiePlot(dataset);
plot.setIgnoreNullValues(false);
plot.setIgnoreZeroValues(false);
LegendItemCollection items = plot.getLegendItems();
assertEquals(4, items.getItemCount());

// check that null items are ignored if requested
plot.setIgnoreNullValues(true);
items = plot.getLegendItems();
assertEquals(3, items.getItemCount());

// check that zero items are ignored if requested
plot.setIgnoreZeroValues(true);
items = plot.getLegendItems();
assertEquals(2, items.getItemCount());

dataset.setValue("Item 5", -1.0);
try{
items = plot.getLegendItems();
}
catch (IllegalArgumentException exception){
assertEquals("Only positive values are allowed in the dataset.", exception.getMessage());
}
// check that the item count remains the same as a negative value cannot be added to the items.
assertEquals(2, items.getItemCount());
}

/**
Expand Down