From 42a36af25ac8a96cb0df92e022b46e25019b983d Mon Sep 17 00:00:00 2001 From: Tim Molter <tim@knowm.org> Date: Sun, 10 Jan 2016 21:40:14 +0100 Subject: [PATCH] add to theme for pie charts: percetage fill, isCircle --- .../xchart/demo/charts/pie/PieChart02.java | 4 +- .../main/java/org/knowm/xchart/Chart_Pie.java | 2 +- .../org/knowm/xchart/StyleManagerPie.java | 37 ++++++++++++++++++- .../internal/chartpart/PlotContent_Pie.java | 29 ++++++++++++--- .../xchart/internal/style/GGPlot2Theme.java | 14 +++++++ .../xchart/internal/style/MatlabTheme.java | 14 +++++++ .../knowm/xchart/internal/style/Theme.java | 6 +++ .../xchart/internal/style/XChartTheme.java | 14 +++++++ 8 files changed, 111 insertions(+), 9 deletions(-) diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java index 9b4af002..268fe317 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/pie/PieChart02.java @@ -20,6 +20,7 @@ import java.awt.Color; import org.knowm.xchart.ChartBuilderPie; import org.knowm.xchart.Chart_Pie; +import org.knowm.xchart.Series_Pie; import org.knowm.xchart.SwingWrapper; import org.knowm.xchart.demo.charts.ExampleChart; @@ -48,7 +49,8 @@ public class PieChart02 implements ExampleChart<Chart_Pie> { Chart_Pie chart = new ChartBuilderPie().width(800).height(600).title(getClass().getSimpleName()).build(); chart.addSeries("Gold", 24); chart.addSeries("Silver", 21); - chart.addSeries("Platinum", 39); + Series_Pie series = chart.addSeries("Platinum", 39); + chart.addSeries("Copper", 17); chart.addSeries("Zinc", 40); diff --git a/xchart/src/main/java/org/knowm/xchart/Chart_Pie.java b/xchart/src/main/java/org/knowm/xchart/Chart_Pie.java index e6452a4c..aa2b8015 100644 --- a/xchart/src/main/java/org/knowm/xchart/Chart_Pie.java +++ b/xchart/src/main/java/org/knowm/xchart/Chart_Pie.java @@ -91,7 +91,7 @@ public class Chart_Pie extends Chart<StyleManagerPie, Series_Pie> { * @param value * @return */ - public Series addSeries(String seriesName, Number value) { + public Series_Pie addSeries(String seriesName, Number value) { Series_Pie series = new Series_Pie(seriesName, value); diff --git a/xchart/src/main/java/org/knowm/xchart/StyleManagerPie.java b/xchart/src/main/java/org/knowm/xchart/StyleManagerPie.java index 2e3c8e6d..b78b68bc 100644 --- a/xchart/src/main/java/org/knowm/xchart/StyleManagerPie.java +++ b/xchart/src/main/java/org/knowm/xchart/StyleManagerPie.java @@ -27,6 +27,9 @@ public class StyleManagerPie extends StyleManager { private ChartPieSeriesRenderStyle chartPieSeriesRenderStyle; + private double pieFillPercentage; + private boolean isCircular; + /** * Constructor */ @@ -39,7 +42,9 @@ public class StyleManagerPie extends StyleManager { @Override protected void setAllStyles() { - chartPieSeriesRenderStyle = ChartPieSeriesRenderStyle.Pie; // set default to area + chartPieSeriesRenderStyle = ChartPieSeriesRenderStyle.Pie; // set default to pie, donut may be a future one + pieFillPercentage = theme.getPieFillPercentage(); + isCircular = theme.isCircular(); } public ChartPieSeriesRenderStyle getChartPieSeriesRenderStyle() { @@ -57,6 +62,36 @@ public class StyleManagerPie extends StyleManager { this.chartPieSeriesRenderStyle = chartPieSeriesRenderStyle; } + public double getPieFillPercentage() { + + return pieFillPercentage; + } + + /** + * Sets the amount of space that the pie chart fills. Full fill is 100%, i.e. 1.0 + * + * @param pieFillPercentage + */ + public void setPieFillPercentage(double pieFillPercentage) { + + this.pieFillPercentage = pieFillPercentage; + } + + public boolean isCircular() { + + return isCircular; + } + + /** + * Sets whether or not the pie chart is forced to be circular. Otherwise it's shape is oval, matching the containing plot. + * + * @param isCircular + */ + public void setCircular(boolean isCircular) { + + this.isCircular = isCircular; + } + /** * Set the theme the style manager 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 647dd74c..768d1a0f 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 @@ -66,13 +66,30 @@ public class PlotContent_Pie<SM extends StyleManager, S extends Series> extends g.setClip(bounds.createIntersection(rectangle)); // pie bounds - double percentage = .70; + double percentage = styleManagerPie.getPieFillPercentage(); + + // if (styleManagerPie.isCircular()) { + // + // double pieDiameter = Math.min(bounds.getWidth(), bounds.getHeight()); + // } + double halfBorderPercentage = (1 - percentage) / 2.0; - Rectangle2D pieBounds = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() * halfBorderPercentage, bounds.getY() + bounds.getHeight() * halfBorderPercentage, bounds.getWidth() * percentage, - bounds.getHeight() * percentage); - // g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); - // g.setColor(Color.black); - // g.draw(pieBounds); + double width = styleManagerPie.isCircular() ? Math.min(bounds.getWidth(), bounds.getHeight()) : bounds.getWidth(); + double height = styleManagerPie.isCircular() ? Math.min(bounds.getWidth(), bounds.getHeight()) : bounds.getHeight(); + + Rectangle2D pieBounds = new Rectangle2D.Double( + + bounds.getX() + bounds.getWidth() / 2 - width / 2 + halfBorderPercentage * width, + + bounds.getY() + bounds.getHeight() / 2 - height / 2 + halfBorderPercentage * height, + + width * percentage, + + height * percentage); + + // g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); + // g.setColor(Color.black); + // g.draw(pieBounds); // get total double total = 0.0; diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java index 5909d99d..e8086333 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/GGPlot2Theme.java @@ -334,6 +334,20 @@ public class GGPlot2Theme implements Theme { return true; } + // Pie Charts /////////////////////////////// + + @Override + public double getPieFillPercentage() { + + return .90; + } + + @Override + public boolean isCircular() { + + return true; + } + // Line, Scatter, Area Charts /////////////////////////////// @Override diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java index 1749f61c..59ed862d 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/MatlabTheme.java @@ -338,6 +338,20 @@ public class MatlabTheme implements Theme { return true; } + // Pie Charts /////////////////////////////// + + @Override + public double getPieFillPercentage() { + + return .90; + } + + @Override + public boolean isCircular() { + + return true; + } + // Line, Scatter, Area Charts /////////////////////////////// @Override 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 eb8e8388..2297cfaf 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 @@ -132,6 +132,12 @@ public interface Theme extends SeriesMarkers, SeriesLines, SeriesColors { public boolean isBarFilled(); + // Pie Charts /////////////////////////////// + + public double getPieFillPercentage(); + + public boolean isCircular(); + // Line, Scatter, Area Charts /////////////////////////////// public int getMarkerSize(); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java b/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java index 888cd960..61e52800 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/style/XChartTheme.java @@ -334,6 +334,20 @@ public class XChartTheme implements Theme { return true; } + // Pie Charts /////////////////////////////// + + @Override + public double getPieFillPercentage() { + + return .90; + } + + @Override + public boolean isCircular() { + + return true; + } + // Line, Scatter, Area Charts /////////////////////////////// @Override -- GitLab