diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/XChartDemo.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/XChartDemo.java index 35b46b9b5e4fd1ac351e5f8fe27e24b54168c38e..3d78d702d909a6c1329fc4f2e6f63e6979d07451 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/XChartDemo.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/XChartDemo.java @@ -61,6 +61,7 @@ import org.knowm.xchart.demo.charts.line.LineChart06; import org.knowm.xchart.demo.charts.line.LineChart07; import org.knowm.xchart.demo.charts.pie.PieChart01; import org.knowm.xchart.demo.charts.pie.PieChart02; +import org.knowm.xchart.demo.charts.pie.PieChart03; import org.knowm.xchart.demo.charts.realtime.RealtimeChart01; import org.knowm.xchart.demo.charts.realtime.RealtimeChart02; import org.knowm.xchart.demo.charts.realtime.RealtimeChart03; @@ -228,7 +229,10 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("PieChart01 - Pie Chart with 4 Slices", new PieChart01().getChart())); category.add(defaultMutableTreeNode); - defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("PieChart012 - Pie Chart Custom Color Palette", new PieChart02().getChart())); + defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("PieChart02 - Pie Chart Custom Color Palette", new PieChart02().getChart())); + category.add(defaultMutableTreeNode); + + defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("PieChart03 - Pie Chart Custom GGPlot2 Theme", new PieChart03().getChart())); category.add(defaultMutableTreeNode); // Line category 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 new file mode 100644 index 0000000000000000000000000000000000000000..c2f94f4044fad530b751953fc445a37aaa3537da --- /dev/null +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart03.java @@ -0,0 +1,57 @@ +/** + * Copyright 2015-2016 Knowm Inc. (http://knowm.org) and contributors. + * Copyright 2011-2015 Xeiam LLC (http://xeiam.com) and contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.knowm.xchart.demo.charts.pie; + +import org.knowm.xchart.ChartBuilder_Pie; +import org.knowm.xchart.Chart_Pie; +import org.knowm.xchart.SwingWrapper; +import org.knowm.xchart.demo.charts.ExampleChart; +import org.knowm.xchart.internal.style.Styler.ChartTheme; + +/** + * Pie Chart with 5 Slices and customization + * <p> + * Demonstrates the following: + * <ul> + * <li>Pie Chart + * <li>ChartBuilderPie + * <li>GGPlot2 Theme + */ +public class PieChart03 implements ExampleChart<Chart_Pie> { + + public static void main(String[] args) { + + ExampleChart<Chart_Pie> exampleChart = new PieChart03(); + Chart_Pie chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart_Pie getChart() { + + // Create Chart + Chart_Pie chart = new ChartBuilder_Pie().width(400).height(500).title(getClass().getSimpleName()).theme(ChartTheme.GGPlot2).build(); + chart.addSeries("Prague", 2); + chart.addSeries("Dresden", 4); + chart.addSeries("Munich", 34); + chart.addSeries("Hamburg", 22); + chart.addSeries("Berlin", 29); + + 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 22449798aa1bb01588b8362d5d9b06d5aad37ed0..e0b7900ce434c50f03dfc43ca03f72072610070c 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 @@ -136,17 +136,50 @@ public class PlotContent_Pie<ST extends Styler, S extends Series> extends PlotCo double xOffset = xCenter + Math.cos(Math.toRadians(angle)) * (pieBounds.getWidth() / 2 * stylerPie.getAnnotationDistance()); double yOffset = yCenter - Math.sin(Math.toRadians(angle)) * (pieBounds.getHeight() / 2 * stylerPie.getAnnotationDistance()); - g.setColor(stylerPie.getChartFontColor()); - g.setFont(stylerPie.getChartTitleFont()); - + // get annotation width Shape shape = textLayout.getOutline(null); - AffineTransform orig = g.getTransform(); - AffineTransform at = new AffineTransform(); - at.translate(xOffset, yOffset); - g.transform(at); - g.fill(shape); - g.setTransform(orig); - + Rectangle2D annotationBounds = shape.getBounds2D(); + double annotationWidth = annotationBounds.getWidth(); + // System.out.println("annotationWidth= " + annotationWidth); + double annotationHeight = annotationBounds.getHeight(); + // System.out.println("annotationHeight= " + annotationHeight); + + // get slice area + double xOffset1 = xCenter + Math.cos(Math.toRadians(startAngle)) * (pieBounds.getWidth() / 2 * stylerPie.getAnnotationDistance()); + double yOffset1 = yCenter - Math.sin(Math.toRadians(startAngle)) * (pieBounds.getHeight() / 2 * stylerPie.getAnnotationDistance()); + double xOffset2 = xCenter + Math.cos(Math.toRadians((arcAngle + startAngle))) * (pieBounds.getWidth() / 2 * stylerPie.getAnnotationDistance()); + double yOffset2 = yCenter - Math.sin(Math.toRadians((arcAngle + startAngle))) * (pieBounds.getHeight() / 2 * stylerPie.getAnnotationDistance()); + // System.out.println("xOffset1= " + xOffset1); + // System.out.println("yOffset1= " + yOffset1); + // System.out.println("xOffset2= " + xOffset2); + // System.out.println("yOffset2= " + yOffset2); + double xDiff = Math.abs(xOffset1 - xOffset2); + double yDiff = Math.abs(yOffset1 - yOffset2); + // System.out.println("xDiff= " + xDiff); + // System.out.println("yDiff= " + yDiff); + // double max = Math.max(xDiff, yDiff); + // System.out.println(" ================== "); + boolean annotationWillFit = false; + if (xDiff > yDiff) {// assume more vertically orientated slice + if (annotationWidth < xDiff) { + annotationWillFit = true; + } + } + else if (xDiff < yDiff) {// assume more horizontally orientated slice + if (annotationHeight < yDiff) { + annotationWillFit = true; + } + } + if (annotationWillFit) { + g.setColor(stylerPie.getChartFontColor()); + g.setFont(stylerPie.getChartTitleFont()); + AffineTransform orig = g.getTransform(); + AffineTransform at = new AffineTransform(); + at.translate(xOffset, yOffset); + g.transform(at); + g.fill(shape); + g.setTransform(orig); + } // // Tick Mark // xCenter = pieBounds.getX() + pieBounds.getWidth() / 2; // yCenter = pieBounds.getY() + pieBounds.getHeight() / 2;