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;
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);
......
......@@ -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);
......
......@@ -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
*
......
......@@ -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;
......
......@@ -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
......
......@@ -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
......
......@@ -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();
......
......@@ -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
......
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