Repository: knowm/XChart
Pull request #382 (2019-12-18 07:30:56)

add Space between piechart slicesp
#275 : add space between pie chart slices like this
![Screenshot_2019-12-18_15-22-29](https://user-images.githubusercontent.com/57353473/71065033-4e991180-21ab-11ea-80ed-d216ff360ac5.png)



Patch:
diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart07.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart07.java
new file mode 100644
index 00000000..950bc3ad
--- /dev/null
+++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart07.java
@@ -0,0 +1,46 @@
+package org.knowm.xchart.demo.charts.pie;
+
+import org.knowm.xchart.PieChart;
+import org.knowm.xchart.PieChartBuilder;
+import org.knowm.xchart.SwingWrapper;
+import org.knowm.xchart.demo.charts.ExampleChart;
+
+/**
+ * Pie Chart - oval with border
+ *
+ * <p>
+ * Demonstrates the following:
+ *
+ * <ul>
+ * <li>Pie Chart
+ * <li>ChartBuilderPie
+ * <li>Setting Non-circular to match container aspect ratio
+ */
+public class PieChart07 implements ExampleChart<PieChart> {
+
+  public static void main(String[] args) {
+
+    ExampleChart<PieChart> exampleChart = new PieChart07();
+    PieChart chart = exampleChart.getChart();
+    new SwingWrapper<PieChart>(chart).displayChart();
+  }
+
+  @Override
+  public PieChart getChart() {
+
+    // Create Chart
+    PieChart chart = new PieChartBuilder().width(800).height(600).title("Pie Chart - oval with border").build();
+
+    // Customize Chart
+    chart.getStyler().setCircular(false);
+    chart.getStyler().setBorderWidth(10);
+
+    // Series
+    chart.addSeries("Pennies", 100);
+    chart.addSeries("Nickels", 100);
+    chart.addSeries("Dimes", 100);
+    chart.addSeries("Quarters", 100);
+
+    return chart;
+  }
+}
diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart08.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart08.java
new file mode 100644
index 00000000..cb9ec48a
--- /dev/null
+++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart08.java
@@ -0,0 +1,64 @@
+package org.knowm.xchart.demo.charts.pie;
+
+import java.awt.Color;
+import org.knowm.xchart.PieChart;
+import org.knowm.xchart.PieChartBuilder;
+import org.knowm.xchart.SwingWrapper;
+import org.knowm.xchart.demo.charts.ExampleChart;
+import org.knowm.xchart.style.PieStyler.AnnotationType;
+
+/**
+ * Pie Chart - circle with border
+ *
+ * <p>Demonstrates the following:
+ *
+ * <ul>
+ *   <li>Pie Chart
+ *   <li>PieChartBuilder
+ *   <li>Custom series palette
+ *   <li>Value Annotations
+ */
+public class PieChart08 implements ExampleChart<PieChart> {
+
+  public static void main(String[] args) {
+
+    ExampleChart<PieChart> exampleChart = new PieChart08();
+    PieChart chart = exampleChart.getChart();
+    new SwingWrapper<PieChart>(chart).displayChart();
+  }
+
+  @Override
+  public PieChart getChart() {
+
+    // Create Chart
+    PieChart chart =
+        new PieChartBuilder()
+            .width(800)
+            .height(600)
+            .title("Pie Chart - circle with border")
+            .build();
+
+    // Customize Chart
+    Color[] sliceColors =
+        new Color[] {
+          new Color(224, 68, 14),
+          new Color(230, 105, 62),
+          new Color(236, 143, 110),
+          new Color(243, 180, 159),
+          new Color(246, 199, 182)
+        };
+    chart.getStyler().setSeriesColors(sliceColors);
+    chart.getStyler().setAnnotationType(AnnotationType.Value);
+    chart.getStyler().setBorderWidth(10);
+    // chart.getStyler().setDecimalPattern("#0.000");
+
+    // Series
+    chart.addSeries("Gold", 24);
+    chart.addSeries("Silver", 21);
+    chart.addSeries("Platinum", 39);
+    chart.addSeries("Copper", 17);
+    chart.addSeries("Zinc", 40);
+
+    return chart;
+  }
+}
diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart09.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart09.java
new file mode 100644
index 00000000..cb3d7b66
--- /dev/null
+++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart09.java
@@ -0,0 +1,59 @@
+package org.knowm.xchart.demo.charts.pie;
+
+import org.knowm.xchart.PieChart;
+import org.knowm.xchart.PieChartBuilder;
+import org.knowm.xchart.PieSeries.PieSeriesRenderStyle;
+import org.knowm.xchart.SwingWrapper;
+import org.knowm.xchart.demo.charts.ExampleChart;
+import org.knowm.xchart.style.PieStyler.AnnotationType;
+import org.knowm.xchart.style.Styler;
+
+/**
+ * Pie Chart - ring with border
+ *
+ * <p>Demonstrates the following:
+ *
+ * <ul>
+ *   <li>Donut Chart
+ *   <li>PieChartBuilder
+ *   <li>XChart Theme
+ *   <li>Horizontal Legend OutsideS
+ */
+public class PieChart09 implements ExampleChart<PieChart> {
+
+  public static void main(String[] args) {
+
+    ExampleChart<PieChart> exampleChart = new PieChart09();
+    PieChart chart = exampleChart.getChart();
+    new SwingWrapper<PieChart>(chart).displayChart();
+  }
+
+  @Override
+  public PieChart getChart() {
+
+    // Create Chart
+    PieChart chart =
+        new PieChartBuilder().width(800).height(600).title("Pie Chart - ring with border").build();
+    chart.getStyler().setLegendPosition(Styler.LegendPosition.OutsideS);
+    chart.getStyler().setLegendLayout(Styler.LegendLayout.Horizontal);
+
+    // Customize Chart
+    //    chart.getStyler().setLegendVisible(false);
+    chart.getStyler().setAnnotationType(AnnotationType.Label);
+    chart.getStyler().setAnnotationDistance(.82);
+    chart.getStyler().setPlotContentSize(.9);
+    chart.getStyler().setDefaultSeriesRenderStyle(PieSeriesRenderStyle.Donut);
+    chart.getStyler().setBorderWidth(10);
+    // chart.getStyler().setCircular(false);
+
+    // Series
+    chart.addSeries("A", 22);
+    chart.addSeries("B", 10);
+    chart.addSeries("C", 34);
+    chart.addSeries("D", 22);
+    chart.addSeries("E", 29);
+    chart.addSeries("F", 40);
+
+    return chart;
+  }
+}
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 3454b920..f32a6263 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
@@ -135,8 +135,11 @@ public class PlotContent_Pie<ST extends PieStyler, S extends PieSeries>
 
   private void paintSlices(Graphics2D g, Rectangle2D pieBounds, double total, double startAngle) {
     boolean toolTipsEnabled = chart.getStyler().isToolTipsEnabled();
+    double borderAngle = startAngle;
 
     Map<String, S> map = chart.getSeriesMap();
+    double xCenter = pieBounds.getX() + pieBounds.getWidth() / 2;
+    double yCenter = pieBounds.getY() + pieBounds.getHeight() / 2;
     for (S series : map.values()) {
 
       if (!series.isEnabled()) {
@@ -188,10 +191,6 @@ public class PlotContent_Pie<ST extends PieStyler, S extends PieSeries>
       // maybe another option to construct this label
       String annotation = series.getName() + " (" + df.format(y) + ")";
 
-      double xCenter =
-          pieBounds.getX() + pieBounds.getWidth() / 2; // - annotationRectangle.getWidth() / 2;
-      double yCenter =
-          pieBounds.getY() + pieBounds.getHeight() / 2; // + annotationRectangle.getHeight() / 2;
       double angle = (arcAngle + startAngle) - arcAngle / 2;
       double xOffset =
           xCenter
@@ -215,6 +214,28 @@ public class PlotContent_Pie<ST extends PieStyler, S extends PieSeries>
         startAngle += arcAngle;
       }
     }
+
+    // draw border
+    float borderWidth = chart.getStyler().getBorderWidth();
+    if (borderWidth > 0) {
+      Color color = pieStyler.getPlotBackgroundColor();
+      g.setColor(color);
+      for (S series : map.values()) {
+        if (!series.isEnabled()) {
+          continue;
+        }
+        Number y = series.getValue();
+        double arcAngle = y.doubleValue() * 360 / total;
+        borderAngle += arcAngle;
+        double xBorder = (pieBounds.getWidth() / 2.0) * Math.cos(Math.toRadians(borderAngle));
+        double yBorder = (pieBounds.getHeight() / 2.0) * Math.sin(Math.toRadians(borderAngle));
+        xBorder = xBorder + pieBounds.getX() + pieBounds.getWidth() / 2.0;
+        yBorder = pieBounds.getY() + pieBounds.getHeight() / 2.0 - yBorder;
+        Shape line = new Line2D.Double(xCenter, yCenter, xBorder, yBorder);
+        g.setStroke(new BasicStroke(borderWidth));
+        g.draw(line);
+      }
+    }
   }
 
   private void paintAnnotations(
diff --git a/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java b/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
index 4395e3a8..b9af35f0 100644
--- a/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
+++ b/xchart/src/main/java/org/knowm/xchart/style/PieStyler.java
@@ -18,6 +18,7 @@ public class PieStyler extends Styler {
   private Font sumFont;
   private String sumFormat;
   private ClockwiseDirectionType clockwiseDirectionType = ClockwiseDirectionType.COUNTER_CLOCKWISE;
+  private float borderWidth = 0;
 
   public PieStyler() {
 
@@ -252,6 +253,16 @@ public class PieStyler extends Styler {
     return this;
   }
 
+  // used to add border width
+  public PieStyler setBorderWidth(double borderWidth) {
+    this.borderWidth = (float)borderWidth;
+    return this;
+  }
+
+  public float getBorderWidth() {
+    return borderWidth;
+  }
+
   public enum AnnotationType {
     Value,
     Percentage,


Test patch:
diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/ExampleChartTester.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/ExampleChartTester.java
index 4ba3a38b..28e1f779 100644
--- a/xchart-demo/src/main/java/org/knowm/xchart/demo/ExampleChartTester.java
+++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/ExampleChartTester.java
@@ -67,6 +67,9 @@ import org.knowm.xchart.demo.charts.pie.PieChart03;
 import org.knowm.xchart.demo.charts.pie.PieChart04;
 import org.knowm.xchart.demo.charts.pie.PieChart05;
 import org.knowm.xchart.demo.charts.pie.PieChart06;
+import org.knowm.xchart.demo.charts.pie.PieChart07;
+import org.knowm.xchart.demo.charts.pie.PieChart08;
+import org.knowm.xchart.demo.charts.pie.PieChart09;
 import org.knowm.xchart.demo.charts.radar.RadarChart01;
 import org.knowm.xchart.demo.charts.realtime.RealtimeChart01;
 import org.knowm.xchart.demo.charts.realtime.RealtimeChart02;
@@ -313,6 +316,9 @@ public class ExampleChartTester extends JPanel implements TreeSelectionListener
     exList.add(new PieChart04());
     exList.add(new PieChart05());
     exList.add(new PieChart06());
+    exList.add(new PieChart07());
+    exList.add(new PieChart08());
+    exList.add(new PieChart09());
 
     // Line
     exList.add(new LineChart01());