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

cleaned up legend code relating to vertical spacing

parent 8d1c885c
Branches
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ import com.xeiam.xchart.demo.charts.ExampleChart; ...@@ -32,6 +32,7 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
* <ul> * <ul>
* <li>ChartType.Scatter * <li>ChartType.Scatter
* <li>Series data as a Set * <li>Series data as a Set
* <li>Setting marker size
*/ */
public class ScatterChart01 implements ExampleChart { public class ScatterChart01 implements ExampleChart {
...@@ -61,6 +62,7 @@ public class ScatterChart01 implements ExampleChart { ...@@ -61,6 +62,7 @@ public class ScatterChart01 implements ExampleChart {
// Customize Chart // Customize Chart
chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setLegendPosition(LegendPosition.InsideSW); chart.getStyleManager().setLegendPosition(LegendPosition.InsideSW);
chart.getStyleManager().setMarkerSize(16);
// Series // Series
chart.addSeries("Gaussian Blob", xData, yData); chart.addSeries("Gaussian Blob", xData, yData);
......
...@@ -24,6 +24,10 @@ import com.xeiam.xchart.demo.charts.ExampleChart; ...@@ -24,6 +24,10 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
/** /**
* Default XChart Theme * Default XChart Theme
* <p>
* Demonstrates the following:
* <ul>
* <li>Setting marker size
*/ */
public class ThemeChart01 implements ExampleChart { public class ThemeChart01 implements ExampleChart {
...@@ -58,6 +62,7 @@ public class ThemeChart01 implements ExampleChart { ...@@ -58,6 +62,7 @@ public class ThemeChart01 implements ExampleChart {
String seriesName = "y=" + 2 * i + "x-" + i * b + "b"; String seriesName = "y=" + 2 * i + "x-" + i * b + "b";
chart.addSeries(seriesName, xData, yData); chart.addSeries(seriesName, xData, yData);
chart.getStyleManager().setMarkerSize(11);
} }
return chart; return chart;
......
...@@ -76,13 +76,15 @@ public class Legend implements ChartPart { ...@@ -76,13 +76,15 @@ public class Legend implements ChartPart {
// determine legend text content max width // determine legend text content max width
double legendTextContentMaxWidth = 0; double legendTextContentMaxWidth = 0;
double legendTextContentMaxHeight = 0; // double legendTextContentMaxHeight = 0;
// determine legend content height // determine legend content height
double legendContentHeight = 0; double legendContentHeight = 0;
for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) {
List<Map.Entry<String, Rectangle2D>> seriesBounds = getSeriesBounds(series, g);
List<Map.Entry<String, Rectangle2D>> seriesBounds = getSeriesNameBounds(series, g);
double blockHeight = 0; double blockHeight = 0;
for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { for (Map.Entry<String, Rectangle2D> entry : seriesBounds) {
blockHeight += entry.getValue().getHeight(); blockHeight += entry.getValue().getHeight();
...@@ -91,14 +93,9 @@ public class Legend implements ChartPart { ...@@ -91,14 +93,9 @@ public class Legend implements ChartPart {
blockHeight = Math.max(blockHeight, isBar ? BOX_SIZE : getChartPainter().getStyleManager().getMarkerSize()); blockHeight = Math.max(blockHeight, isBar ? BOX_SIZE : getChartPainter().getStyleManager().getMarkerSize());
legendTextContentMaxHeight = Math.max(legendTextContentMaxHeight, blockHeight); legendContentHeight += blockHeight + styleManager.getLegendPadding();
legendContentHeight += blockHeight;
} }
// vertical padding between items
double paddingSize = isBar ? styleManager.getLegendPadding() : fontMetrics.getDescent();
legendContentHeight += paddingSize * (chartPainter.getAxisPair().getSeriesMap().size() - 1);
// determine legend content width // determine legend content width
double legendContentWidth = 0; double legendContentWidth = 0;
if (!isBar) { if (!isBar) {
...@@ -109,11 +106,11 @@ public class Legend implements ChartPart { ...@@ -109,11 +106,11 @@ public class Legend implements ChartPart {
} }
// Legend Box // Legend Box
double legendBoxWidth = legendContentWidth + 2 * styleManager.getLegendPadding(); double legendBoxWidth = legendContentWidth + 2 * styleManager.getLegendPadding();
double legendBoxHeight = legendContentHeight + 2 * styleManager.getLegendPadding(); double legendBoxHeight = legendContentHeight + 1 * styleManager.getLegendPadding();
return new double[] { legendBoxWidth, legendBoxHeight }; return new double[] { legendBoxWidth, legendBoxHeight };
} }
private List<Map.Entry<String, Rectangle2D>> getSeriesBounds(Series series, Graphics2D g) { private List<Map.Entry<String, Rectangle2D>> getSeriesNameBounds(Series series, Graphics2D g) {
String lines[] = series.getName().split("\\n"); String lines[] = series.getName().split("\\n");
List<Map.Entry<String, Rectangle2D>> stringBounds = new ArrayList<Map.Entry<String, Rectangle2D>>(lines.length); List<Map.Entry<String, Rectangle2D>> stringBounds = new ArrayList<Map.Entry<String, Rectangle2D>>(lines.length);
...@@ -188,12 +185,16 @@ public class Legend implements ChartPart { ...@@ -188,12 +185,16 @@ public class Legend implements ChartPart {
double starty = yOffset + styleManager.getLegendPadding(); double starty = yOffset + styleManager.getLegendPadding();
for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) {
List<Map.Entry<String, Rectangle2D>> seriesBounds = getSeriesBounds(series, g);
List<Map.Entry<String, Rectangle2D>> seriesNameBounds = getSeriesNameBounds(series, g);
float blockHeight = 0; float blockHeight = 0;
for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { for (Map.Entry<String, Rectangle2D> entry : seriesNameBounds) {
blockHeight += entry.getValue().getHeight(); blockHeight += entry.getValue().getHeight();
} }
blockHeight = Math.max(blockHeight, styleManager.getChartType() == ChartType.Bar ? BOX_SIZE : getChartPainter().getStyleManager().getMarkerSize());
if (styleManager.getChartType() != ChartType.Bar) { if (styleManager.getChartType() != ChartType.Bar) {
// paint line // paint line
if (styleManager.getChartType() != ChartType.Scatter && series.getStroke() != null) { if (styleManager.getChartType() != ChartType.Scatter && series.getStroke() != null) {
...@@ -224,22 +225,21 @@ public class Legend implements ChartPart { ...@@ -224,22 +225,21 @@ public class Legend implements ChartPart {
float itemOffsetY = -fontMetrics.getDescent(); float itemOffsetY = -fontMetrics.getDescent();
if (styleManager.getChartType() != ChartType.Bar) { if (styleManager.getChartType() != ChartType.Bar) {
final float x = (float) (startx + styleManager.getLegendSeriesLineLength() + styleManager.getLegendPadding()); final float x = (float) (startx + styleManager.getLegendSeriesLineLength() + styleManager.getLegendPadding());
for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { for (Map.Entry<String, Rectangle2D> entry : seriesNameBounds) {
g.drawString(entry.getKey(), x, (float) (starty + entry.getValue().getHeight()) + itemOffsetY); g.drawString(entry.getKey(), x, (float) (starty + entry.getValue().getHeight()) + itemOffsetY);
itemOffsetY += entry.getValue().getHeight(); itemOffsetY += entry.getValue().getHeight();
} }
itemOffsetY = Math.max(itemOffsetY, getChartPainter().getStyleManager().getMarkerSize()); starty += blockHeight + styleManager.getLegendPadding();
starty += blockHeight + fontMetrics.getDescent();
} }
else { else {
final float x = (float) (startx + BOX_SIZE + styleManager.getLegendPadding()); final float x = (float) (startx + BOX_SIZE + styleManager.getLegendPadding());
for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { for (Map.Entry<String, Rectangle2D> entry : seriesNameBounds) {
double height = entry.getValue().getHeight(); double height = entry.getValue().getHeight();
double centerOffsetY = (Math.max(BOX_SIZE, height) - height) / 2.0; double centerOffsetY = (Math.max(BOX_SIZE, height) - height) / 2.0;
g.drawString(entry.getKey(), x, (float) (starty + height + itemOffsetY + centerOffsetY)); g.drawString(entry.getKey(), x, (float) (starty + height + itemOffsetY + centerOffsetY));
itemOffsetY += height; itemOffsetY += height;
} }
starty += Math.max(BOX_SIZE, blockHeight) + styleManager.getLegendPadding(); starty += blockHeight + styleManager.getLegendPadding();
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment