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

allow setting annotation type pn pie charts

parent b40966af
Branches
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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
*
......
......@@ -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;
// }
}
......@@ -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();
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment