Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #167 and issue #165 #259

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
11 changes: 4 additions & 7 deletions src/main/java/org/jfree/chart/date/SpreadsheetDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
* Represents a date using an integer, in a similar fashion to the
* implementation in Microsoft Excel. The range of dates supported is
* 1-Jan-1900 to 31-Dec-9999.
* (This year limit is removed to support years
* earlier than 1990, which is currently supported in Microsoft Excel.)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Just remove the last sentence (starting on line 43 and ending on 44) - then no need for adding lines 45 and 46.

* <P>
* Be aware that there is a deliberate bug in Excel that recognises the year
* 1900 as a leap year when in fact it is not a leap year. You can find more
Expand Down Expand Up @@ -85,13 +87,8 @@ public class SpreadsheetDate extends SerialDate {
*/
public SpreadsheetDate(int day, int month, int year) {

if ((year >= 1900) && (year <= 9999)) {
this.year = year;
}
else {
throw new IllegalArgumentException(
"The 'year' argument must be in range 1900 to 9999.");
}
// CS427 Issue link: https://github.com/jfree/jfreechart/issues/165
this.year = year;

if ((month >= MonthConstants.JANUARY)
&& (month <= MonthConstants.DECEMBER)) {
Expand Down
50 changes: 32 additions & 18 deletions src/main/java/org/jfree/chart/plot/pie/PiePlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.TreeMap;
import java.util.*;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.api.PublicCloneable;
Expand Down Expand Up @@ -2262,8 +2257,8 @@ protected void drawPie(Graphics2D g2, Rectangle2D plotArea,
}
}
if (this.simpleLabels) {
drawSimpleLabels(g2, keys, totalValue, plotArea, linkArea,
state);
drawSimpleLabels(g2, keys, totalValue, plotArea, linkArea, state);
drawLabels(g2, keys, totalValue, plotArea, linkArea, state);
}
else {
drawLabels(g2, keys, totalValue, plotArea, linkArea, state);
Expand All @@ -2275,6 +2270,15 @@ protected void drawPie(Graphics2D g2, Rectangle2D plotArea,
}
}

private double getTotal(List<K> kList){
double total = 0.0;
for (K key : kList) {
Number n = this.dataset.getValue(key);
total += n.doubleValue();
}
return total;
}

/**
* Draws a single data item.
*
Expand Down Expand Up @@ -2419,7 +2423,7 @@ protected void drawSimpleLabels(Graphics2D g2, List<K> keys,
}
String label = myLabelGenerator.generateSectionLabel(
this.dataset, key);
if (label == null) {
if (label == null||n.doubleValue()/getTotal(keys)< 0.05) {
continue;
}
g2.setFont(this.labelFont);
Expand Down Expand Up @@ -2517,12 +2521,11 @@ protected void drawLabels(Graphics2D g2, List<K> keys, double totalValue,
double gap = plotArea.getWidth() * this.labelGap;
double ww = linkArea.getX() - gap - marginX;
float labelWidth = (float) this.labelPadding.trimWidth(ww);

// draw the labels...
if (this.labelGenerator != null) {
drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth,
drawLeftLabels(leftKeys, g2, plotArea, totalValue, labelWidth,
state);
drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth,
drawRightLabels(rightKeys, g2, plotArea, totalValue, labelWidth,
state);
}
g2.setComposite(originalComposite);
Expand All @@ -2537,12 +2540,12 @@ protected void drawLabels(Graphics2D g2, List<K> keys, double totalValue,
* plot.
* @param g2 the graphics device.
* @param plotArea the plot area.
* @param linkArea the link area.
* @param totalValue the value total.
* @param maxLabelWidth the maximum label width.
* @param state the state.
*/
protected void drawLeftLabels(KeyedValues<K> leftKeys, Graphics2D g2,
Rectangle2D plotArea, Rectangle2D linkArea,
Rectangle2D plotArea, double totalValue,
float maxLabelWidth, PiePlotState state) {

this.labelDistributor.clear();
Expand All @@ -2551,6 +2554,12 @@ protected void drawLeftLabels(KeyedValues<K> leftKeys, Graphics2D g2,
for (int i = 0; i < leftKeys.getItemCount(); i++) {
String label = this.labelGenerator.generateSectionLabel(
this.dataset, leftKeys.getKey(i));
if(this.simpleLabels){
double n = getDataset().getValue(leftKeys.getKey(i)).doubleValue();
if(n/totalValue>=0.05){
label = null;
}
}
if (label != null) {
TextBlock block = TextUtils.createTextBlock(label,
this.labelFont, this.labelPaint, maxLabelWidth,
Expand All @@ -2571,12 +2580,12 @@ protected void drawLeftLabels(KeyedValues<K> leftKeys, Graphics2D g2,
double baseY = state.getPieCenterY() - Math.sin(theta)
* verticalLinkRadius;
double hh = labelBox.getHeight(g2);

this.labelDistributor.addPieLabelRecord(new PieLabelRecord(
leftKeys.getKey(i), theta, baseY, labelBox, hh,
lGap / 2.0 + lGap / 2.0 * -Math.cos(theta), 1.0
- getLabelLinkDepth()
+ getExplodePercent(leftKeys.getKey(i))));

}
}
double hh = plotArea.getHeight();
Expand All @@ -2595,12 +2604,12 @@ protected void drawLeftLabels(KeyedValues<K> leftKeys, Graphics2D g2,
* @param keys the keys.
* @param g2 the graphics device.
* @param plotArea the plot area.
* @param linkArea the link area.
* @param totalValue the value total.
* @param maxLabelWidth the maximum label width.
* @param state the state.
*/
protected void drawRightLabels(KeyedValues<K> keys, Graphics2D g2,
Rectangle2D plotArea, Rectangle2D linkArea,
Rectangle2D plotArea, double totalValue,
float maxLabelWidth, PiePlotState state) {

// draw the right labels...
Expand All @@ -2611,7 +2620,12 @@ protected void drawRightLabels(KeyedValues<K> keys, Graphics2D g2,
for (int i = 0; i < keys.getItemCount(); i++) {
String label = this.labelGenerator.generateSectionLabel(
this.dataset, keys.getKey(i));

if(this.simpleLabels){
double n = getDataset().getValue(keys.getKey(i)).doubleValue();
if(n/totalValue>=0.05){
label = null;
}
}
if (label != null) {
TextBlock block = TextUtils.createTextBlock(label,
this.labelFont, this.labelPaint, maxLabelWidth,
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/org/jfree/issues/issues165/issues165.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jfree.issues.issues165;

import org.jfree.data.time.Day;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Project: Issues165
* Description: This file fixed the issue #165 for the jfree library.
*
* <p>
* author:wm
* Date:2021/12/8
* Time:13:17
* Version:
*/
public class issues165 {

@Test
//create a test for the year before 1990
public void testOldCode() {
try {
org.jfree.data.time.Day day = new org.jfree.data.time.Day(1, 1, 1890);
}catch (Exception e){
Assertions.assertEquals(e.getClass(),IllegalArgumentException.class);
}
}

@Test
//test the year of 1700
public void testSucessCode() {
Day day = new Day(1, 1, 1700);
}
}
68 changes: 68 additions & 0 deletions src/test/java/org/jfree/issues/issues167/Issues167.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.jfree.issues.issues167;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.pie.PiePlot;
import org.jfree.chart.swing.ChartFrame;
import org.jfree.data.general.DefaultPieDataset;
import org.junit.jupiter.api.Test;

/**
* Project: Issues167
* Description: This file fixed the issue #167 for the jfree library.
*
* <p>
* author:wm
* Date:2021/11/18
* Time:17:17
* Version:
*/
public class Issues167 {

@Test
public void test1() throws InterruptedException {
//create the default pie chart
DefaultPieDataset ds = new DefaultPieDataset();
ds.setValue("test01", 2);
ds.setValue("test02", 3);
ds.setValue("test03", 40);
ds.setValue("test04", 15);
ds.setValue("test05", 20);
ds.setValue("test06", 20);

//set the parameters, including title, dataset, legend, tooltips, urls.
JFreeChart chart = ChartFactory.createPieChart("", ds, true, true, false);
//Pie plot information
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setSimpleLabels(true);

// set the title of the frame
ChartFrame chartFrame = new ChartFrame("", chart);
chartFrame.pack();
chartFrame.setVisible(true);
Thread.sleep(5000);
}

@Test
public void test2() throws InterruptedException {
//create the default pie chart
DefaultPieDataset ds = new DefaultPieDataset();
ds.setValue("test01", 2);
ds.setValue("test02", 3);
ds.setValue("test03", 80);
ds.setValue("test04", 15);


//set the parameters, including title, dataset, legend, tooltips, urls.
JFreeChart chart = ChartFactory.createPieChart("", ds, true, true, false);
//Pie plot information
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setSimpleLabels(true);

//set the title of the frame
ChartFrame chartFrame = new ChartFrame("", chart);
chartFrame.pack();
chartFrame.setVisible(true);
Thread.sleep(5000);
}
}