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

add to theme for pie charts: percetage fill, isCircle

parent 79720cfc
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ import java.awt.Color; ...@@ -20,6 +20,7 @@ import java.awt.Color;
import org.knowm.xchart.ChartBuilderPie; import org.knowm.xchart.ChartBuilderPie;
import org.knowm.xchart.Chart_Pie; import org.knowm.xchart.Chart_Pie;
import org.knowm.xchart.Series_Pie;
import org.knowm.xchart.SwingWrapper; import org.knowm.xchart.SwingWrapper;
import org.knowm.xchart.demo.charts.ExampleChart; import org.knowm.xchart.demo.charts.ExampleChart;
...@@ -48,7 +49,8 @@ public class PieChart02 implements ExampleChart<Chart_Pie> { ...@@ -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_Pie chart = new ChartBuilderPie().width(800).height(600).title(getClass().getSimpleName()).build();
chart.addSeries("Gold", 24); chart.addSeries("Gold", 24);
chart.addSeries("Silver", 21); chart.addSeries("Silver", 21);
chart.addSeries("Platinum", 39); Series_Pie series = chart.addSeries("Platinum", 39);
chart.addSeries("Copper", 17); chart.addSeries("Copper", 17);
chart.addSeries("Zinc", 40); chart.addSeries("Zinc", 40);
......
...@@ -91,7 +91,7 @@ public class Chart_Pie extends Chart<StyleManagerPie, Series_Pie> { ...@@ -91,7 +91,7 @@ public class Chart_Pie extends Chart<StyleManagerPie, Series_Pie> {
* @param value * @param value
* @return * @return
*/ */
public Series addSeries(String seriesName, Number value) { public Series_Pie addSeries(String seriesName, Number value) {
Series_Pie series = new Series_Pie(seriesName, value); Series_Pie series = new Series_Pie(seriesName, value);
......
...@@ -27,6 +27,9 @@ public class StyleManagerPie extends StyleManager { ...@@ -27,6 +27,9 @@ public class StyleManagerPie extends StyleManager {
private ChartPieSeriesRenderStyle chartPieSeriesRenderStyle; private ChartPieSeriesRenderStyle chartPieSeriesRenderStyle;
private double pieFillPercentage;
private boolean isCircular;
/** /**
* Constructor * Constructor
*/ */
...@@ -39,7 +42,9 @@ public class StyleManagerPie extends StyleManager { ...@@ -39,7 +42,9 @@ public class StyleManagerPie extends StyleManager {
@Override @Override
protected void setAllStyles() { 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() { public ChartPieSeriesRenderStyle getChartPieSeriesRenderStyle() {
...@@ -57,6 +62,36 @@ public class StyleManagerPie extends StyleManager { ...@@ -57,6 +62,36 @@ public class StyleManagerPie extends StyleManager {
this.chartPieSeriesRenderStyle = chartPieSeriesRenderStyle; 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 * Set the theme the style manager should use
* *
......
...@@ -66,10 +66,27 @@ public class PlotContent_Pie<SM extends StyleManager, S extends Series> extends ...@@ -66,10 +66,27 @@ public class PlotContent_Pie<SM extends StyleManager, S extends Series> extends
g.setClip(bounds.createIntersection(rectangle)); g.setClip(bounds.createIntersection(rectangle));
// pie bounds // 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; double halfBorderPercentage = (1 - percentage) / 2.0;
Rectangle2D pieBounds = new Rectangle2D.Double(bounds.getX() + bounds.getWidth() * halfBorderPercentage, bounds.getY() + bounds.getHeight() * halfBorderPercentage, bounds.getWidth() * percentage, double width = styleManagerPie.isCircular() ? Math.min(bounds.getWidth(), bounds.getHeight()) : bounds.getWidth();
bounds.getHeight() * percentage); 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.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
// g.setColor(Color.black); // g.setColor(Color.black);
// g.draw(pieBounds); // g.draw(pieBounds);
......
...@@ -334,6 +334,20 @@ public class GGPlot2Theme implements Theme { ...@@ -334,6 +334,20 @@ public class GGPlot2Theme implements Theme {
return true; return true;
} }
// Pie Charts ///////////////////////////////
@Override
public double getPieFillPercentage() {
return .90;
}
@Override
public boolean isCircular() {
return true;
}
// Line, Scatter, Area Charts /////////////////////////////// // Line, Scatter, Area Charts ///////////////////////////////
@Override @Override
......
...@@ -338,6 +338,20 @@ public class MatlabTheme implements Theme { ...@@ -338,6 +338,20 @@ public class MatlabTheme implements Theme {
return true; return true;
} }
// Pie Charts ///////////////////////////////
@Override
public double getPieFillPercentage() {
return .90;
}
@Override
public boolean isCircular() {
return true;
}
// Line, Scatter, Area Charts /////////////////////////////// // Line, Scatter, Area Charts ///////////////////////////////
@Override @Override
......
...@@ -132,6 +132,12 @@ public interface Theme extends SeriesMarkers, SeriesLines, SeriesColors { ...@@ -132,6 +132,12 @@ public interface Theme extends SeriesMarkers, SeriesLines, SeriesColors {
public boolean isBarFilled(); public boolean isBarFilled();
// Pie Charts ///////////////////////////////
public double getPieFillPercentage();
public boolean isCircular();
// Line, Scatter, Area Charts /////////////////////////////// // Line, Scatter, Area Charts ///////////////////////////////
public int getMarkerSize(); public int getMarkerSize();
......
...@@ -334,6 +334,20 @@ public class XChartTheme implements Theme { ...@@ -334,6 +334,20 @@ public class XChartTheme implements Theme {
return true; return true;
} }
// Pie Charts ///////////////////////////////
@Override
public double getPieFillPercentage() {
return .90;
}
@Override
public boolean isCircular() {
return true;
}
// Line, Scatter, Area Charts /////////////////////////////// // Line, Scatter, Area Charts ///////////////////////////////
@Override @Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment