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
14 changes: 7 additions & 7 deletions src/main/java/org/jfree/chart/date/SpreadsheetDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public class SpreadsheetDate extends SerialDate {
* @param month the month (in the range 1 to 12).
* @param year the year (in the range 1900 to 9999).
*/

/**
* Fixed issue #165 by modifying the year limit to support years
* earlier than 1990, which is currently supported in Microsoft Excel.
*/
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
55 changes: 37 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 @@ -110,6 +105,11 @@
import org.jfree.data.general.DatasetChangeEvent;
import org.jfree.data.general.DatasetUtils;
import org.jfree.data.general.PieDataset;
/**
* Fixed issue #167 by calculating the percentage of the categories in a pie
* chart. For categories with less than 5% weight, their labels are
* created outside the pie chat to prevent overlapping.
*/

/**
* A plot that displays data in the form of a pie chart, using data from any
Expand Down Expand Up @@ -2262,8 +2262,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 @@ -2274,6 +2274,15 @@ protected void drawPie(Graphics2D g2, Rectangle2D plotArea,
drawNoDataMessage(g2, plotArea);
}
}
// CS427 Issue link: https://github.com/jfree/jfreechart/issues/167
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 +2428,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 +2526,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 +2545,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 +2559,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 +2585,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 +2609,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 +2625,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 {
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() {
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);
}
}