diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart03.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart03.java index c2f94f4044fad530b751953fc445a37aaa3537da..b66536d22ae6f62591b80417b6f2cd8e5b24dd93 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart03.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart03.java @@ -18,6 +18,7 @@ package org.knowm.xchart.demo.charts.pie; import org.knowm.xchart.ChartBuilder_Pie; import org.knowm.xchart.Chart_Pie; +import org.knowm.xchart.Styler_Pie.AnnotationType; import org.knowm.xchart.SwingWrapper; import org.knowm.xchart.demo.charts.ExampleChart; import org.knowm.xchart.internal.style.Styler.ChartTheme; @@ -51,6 +52,10 @@ public class PieChart03 implements ExampleChart<Chart_Pie> { chart.addSeries("Hamburg", 22); chart.addSeries("Berlin", 29); + chart.getStyler().setLegendVisible(false); + chart.getStyler().setAnnotationType(AnnotationType.LabelAndPercentage); + chart.getStyler().setAnnotationDistance(1.1); + return chart; } diff --git a/xchart/src/main/java/org/knowm/xchart/Styler_Pie.java b/xchart/src/main/java/org/knowm/xchart/Styler_Pie.java index 7b1d1dc0614ab9d623ce250264e2a7f86776b168..2a683c9ed89876e1fff501d9a19828c5cfd8a027 100644 --- a/xchart/src/main/java/org/knowm/xchart/Styler_Pie.java +++ b/xchart/src/main/java/org/knowm/xchart/Styler_Pie.java @@ -29,11 +29,17 @@ public class Styler_Pie extends Styler { private ChartPieSeriesRenderStyle chartPieSeriesRenderStyle; + public enum AnnotationType { + + Percentage, Label, LabelAndPercentage + } + private double pieSize; private boolean isCircular; private double startAngleInDegrees; private Font annotationFont; private double annotationDistance; + private AnnotationType annotationType; /** * Constructor @@ -52,6 +58,7 @@ public class Styler_Pie extends Styler { isCircular = theme.isCircular(); annotationFont = theme.getPieFont(); annotationDistance = theme.getAnnotationDistance(); + annotationType = theme.getAnnotationType(); } public ChartPieSeriesRenderStyle getChartPieSeriesRenderStyle() { @@ -144,6 +151,21 @@ public class Styler_Pie extends Styler { this.annotationDistance = annotationDistance; } + public AnnotationType getAnnotationType() { + + return annotationType; + } + + /** + * Sets the Pie chart's annotation type + * + * @param annotationType + */ + public void setAnnotationType(AnnotationType annotationType) { + + this.annotationType = annotationType; + } + /** * Set the theme the styler should use * diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java index e0b7900ce434c50f03dfc43ca03f72072610070c..eceefd3359e0609ea1064338fed8f488823e1184 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContent_Pie.java @@ -16,18 +16,21 @@ */ package org.knowm.xchart.internal.chartpart; +import java.awt.BasicStroke; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.AffineTransform; import java.awt.geom.Arc2D; +import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.text.DecimalFormat; import java.util.Map; import org.knowm.xchart.Series_Pie; import org.knowm.xchart.Styler_Pie; +import org.knowm.xchart.Styler_Pie.AnnotationType; import org.knowm.xchart.internal.Series; import org.knowm.xchart.internal.style.Styler; @@ -124,10 +127,21 @@ public class PlotContent_Pie<ST extends Styler, S extends Series> extends PlotCo g.draw(new Arc2D.Double(pieBounds.getX(), pieBounds.getY(), pieBounds.getWidth(), pieBounds.getHeight(), startAngle, arcAngle, Arc2D.PIE)); // curValue += y.doubleValue(); - // draw percentage on slice - double percentage = y.doubleValue() / total * 100; + // draw annotation + String annotation = ""; + if (stylerPie.getAnnotationType() == AnnotationType.Label) { + annotation = series.getName(); + } + else if (stylerPie.getAnnotationType() == AnnotationType.LabelAndPercentage) { + double percentage = y.doubleValue() / total * 100; + annotation = series.getName() + " (" + df.format(percentage) + "%)"; + } + else if (stylerPie.getAnnotationType() == AnnotationType.Percentage) { + double percentage = y.doubleValue() / total * 100; + annotation = df.format(percentage) + "%"; + } - TextLayout textLayout = new TextLayout(df.format(percentage) + "%", stylerPie.getAnnotationFont(), new FontRenderContext(null, true, false)); + TextLayout textLayout = new TextLayout(annotation, stylerPie.getAnnotationFont(), new FontRenderContext(null, true, false)); Rectangle2D percentageRectangle = textLayout.getBounds(); double xCenter = pieBounds.getX() + pieBounds.getWidth() / 2 - percentageRectangle.getWidth() / 2; @@ -160,12 +174,12 @@ public class PlotContent_Pie<ST extends Styler, S extends Series> extends PlotCo // double max = Math.max(xDiff, yDiff); // System.out.println(" ================== "); boolean annotationWillFit = false; - if (xDiff > yDiff) {// assume more vertically orientated slice + if (xDiff > yDiff) { // assume more vertically orientated slice if (annotationWidth < xDiff) { annotationWillFit = true; } } - else if (xDiff < yDiff) {// assume more horizontally orientated slice + else if (xDiff < yDiff) { // assume more horizontally orientated slice if (annotationHeight < yDiff) { annotationWillFit = true; } @@ -180,17 +194,18 @@ public class PlotContent_Pie<ST extends Styler, S extends Series> extends PlotCo g.fill(shape); g.setTransform(orig); } - // // Tick Mark - // xCenter = pieBounds.getX() + pieBounds.getWidth() / 2; - // yCenter = pieBounds.getY() + pieBounds.getHeight() / 2; - // double xOffsetStart = xCenter + Math.cos(Math.toRadians(angle)) * (pieBounds.getWidth() / 2.01); - // double xOffsetEnd = xCenter + Math.cos(Math.toRadians(angle)) * (pieBounds.getWidth() / 1.9); - // double yOffsetStart = yCenter - Math.sin(Math.toRadians(angle)) * (pieBounds.getHeight() / 2.01); - // double yOffsetEnd = yCenter - Math.sin(Math.toRadians(angle)) * (pieBounds.getHeight() / 1.9); - // - // g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); - // Shape line = new Line2D.Double(xOffsetStart, yOffsetStart, xOffsetEnd, yOffsetEnd); - // g.draw(line); + + // Tick Mark + xCenter = pieBounds.getX() + pieBounds.getWidth() / 2; + yCenter = pieBounds.getY() + pieBounds.getHeight() / 2; + double xOffsetStart = xCenter + Math.cos(Math.toRadians(angle)) * (pieBounds.getWidth() / 2.01); + double xOffsetEnd = xCenter + Math.cos(Math.toRadians(angle)) * (pieBounds.getWidth() / 1.95); + double yOffsetStart = yCenter - Math.sin(Math.toRadians(angle)) * (pieBounds.getHeight() / 2.01); + double yOffsetEnd = yCenter - Math.sin(Math.toRadians(angle)) * (pieBounds.getHeight() / 1.95); + + g.setStroke(new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); + Shape line = new Line2D.Double(xOffsetStart, yOffsetStart, xOffsetEnd, yOffsetEnd); + g.draw(line); startAngle += arcAngle; } @@ -199,32 +214,4 @@ public class PlotContent_Pie<ST extends Styler, S extends Series> extends PlotCo } - // private double[] getPercentageVector(Collection<? extends Number> collection) { - // - // float total = 0.0f; - // - // double[] vectorCenters = new double[collection.size()]; - // Iterator<? extends Number> yItr = collection.iterator(); - // - // int counter = 0; - // while (yItr.hasNext()) { - // - // Number next = yItr.next(); - // - // double y = next.doubleValue(); - // System.out.println(y); - // vectorCenters[counter] = vectorCenters[counter] + y; - // - // total += vectorCenters[counter++]; - // } - // - // double[] vectorPercentages = new double[vectorCenters.length]; - // - // for (int i = 0; i < vectorPercentages.length; i++) { - // vectorPercentages[i] = vectorCenters[i] / total; - // } - // - // return vectorPercentages; - // } - } diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_.java b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_.java index 2ec970b97bc2c1bc682aa628ca15ea46af9bf337..f02254749beb23a9473687885448efc55871a074 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_.java @@ -20,6 +20,7 @@ import java.awt.Color; import java.awt.Font; import java.awt.Stroke; +import org.knowm.xchart.Styler_Pie.AnnotationType; import org.knowm.xchart.internal.style.Styler.LegendPosition; import org.knowm.xchart.internal.style.colors.SeriesColors; import org.knowm.xchart.internal.style.lines.SeriesLines; @@ -144,6 +145,8 @@ public interface Theme_ extends SeriesMarkers, SeriesLines, SeriesColors { public double getAnnotationDistance(); + AnnotationType getAnnotationType(); + // Line, Scatter, Area Charts /////////////////////////////// public int getMarkerSize(); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_GGPlot2.java b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_GGPlot2.java index 4c7b74017ffb9352a2db6c1281c7c0af6640ec74..59ef37d0720975134595359532f535c6aec40c5f 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_GGPlot2.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_GGPlot2.java @@ -21,6 +21,7 @@ import java.awt.Color; import java.awt.Font; import java.awt.Stroke; +import org.knowm.xchart.Styler_Pie.AnnotationType; import org.knowm.xchart.internal.style.Styler.LegendPosition; import org.knowm.xchart.internal.style.colors.ChartColor; import org.knowm.xchart.internal.style.colors.GGPlot2SeriesColors; @@ -366,6 +367,12 @@ public class Theme_GGPlot2 implements Theme_ { return .67; } + @Override + public AnnotationType getAnnotationType() { + + return AnnotationType.LabelAndPercentage; + } + // Line, Scatter, Area Charts /////////////////////////////// @Override diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_Matlab.java b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_Matlab.java index e8e13b326f0c9a2ffb7aa8784e0d06b2a979b4af..b42d3918f567cc18d2ed5cefa41c1bfcf60e0ebe 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_Matlab.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_Matlab.java @@ -21,6 +21,7 @@ import java.awt.Color; import java.awt.Font; import java.awt.Stroke; +import org.knowm.xchart.Styler_Pie.AnnotationType; import org.knowm.xchart.internal.style.Styler.LegendPosition; import org.knowm.xchart.internal.style.colors.ChartColor; import org.knowm.xchart.internal.style.colors.MatlabSeriesColors; @@ -370,6 +371,12 @@ public class Theme_Matlab implements Theme_ { return .67; } + @Override + public AnnotationType getAnnotationType() { + + return AnnotationType.Label; + } + // Line, Scatter, Area Charts /////////////////////////////// @Override diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_XChart.java b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_XChart.java index d61cc6b257d340f0e3ddbcb5ca72b8bb85b08591..78cde489553fabdd16e7974baa3eb2ef153b45c8 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_XChart.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/Theme_XChart.java @@ -21,6 +21,7 @@ import java.awt.Color; import java.awt.Font; import java.awt.Stroke; +import org.knowm.xchart.Styler_Pie.AnnotationType; import org.knowm.xchart.internal.style.Styler.LegendPosition; import org.knowm.xchart.internal.style.colors.ChartColor; import org.knowm.xchart.internal.style.colors.XChartSeriesColors; @@ -366,6 +367,12 @@ public class Theme_XChart implements Theme_ { return .67; } + @Override + public AnnotationType getAnnotationType() { + + return AnnotationType.Percentage; + } + // Line, Scatter, Area Charts /////////////////////////////// @Override