Skip to content
Snippets Groups Projects
Commit b40966af authored by Tim Molter's avatar Tim Molter
Browse files

don't annotate pie chart slice if not enough room

parent 3abfe57e
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
/**
* 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;
}
}
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment