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

improved layout of axes by recursively positioning Axes

parent c9c78bad
No related branches found
No related tags found
No related merge requests found
......@@ -147,32 +147,47 @@ public class Axis implements ChartPart {
// ----
double xOffset = getChartPainter().getStyleManager().getChartPadding();
double yOffset = getChartPainter().getChartTitle().getSizeHint();
double width = 80; // arbitrary, final width depends on Axis tick labels
double chartLegendWidth = 0;
if (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideE) {
chartLegendWidth = getChartPainter().getChartLegend().getSizeHint(g)[0];
}
double approximateXAxisWidth =
/////////////////////////
int i = 1; // just twice through
double width = 80; // arbitrary, final width depends on Axis tick labels
double height = 0;
do {
// System.out.println("width: " + width);
getChartPainter().getWidth()
double approximateXAxisWidth =
- width // y-axis approx. width
getChartPainter().getWidth()
- chartLegendWidth
- width // y-axis approx. width
- 2 * getChartPainter().getStyleManager().getChartPadding()
- chartLegendWidth
- (getChartPainter().getStyleManager().isYAxisTicksVisible() ? (getChartPainter().getStyleManager().getPlotPadding()) : 0)
- 2 * getChartPainter().getStyleManager().getChartPadding()
- (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideE && getChartPainter().getStyleManager().isLegendVisible() ? getChartPainter().getStyleManager()
.getChartPadding() : 0)
- (getChartPainter().getStyleManager().isYAxisTicksVisible() ? (getChartPainter().getStyleManager().getPlotPadding()) : 0)
;
- (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideE && getChartPainter().getStyleManager().isLegendVisible() ? getChartPainter().getStyleManager()
.getChartPadding() : 0)
;
height = getChartPainter().getHeight() - yOffset - axisPair.getXAxis().getXAxisHeightHint(approximateXAxisWidth) - getChartPainter().getStyleManager().getPlotPadding() - getChartPainter()
.getStyleManager().getChartPadding();
width = getYAxisWidthHint(height);
// System.out.println("height: " + height);
} while (i-- > 0);
/////////////////////////
double height = getChartPainter().getHeight() - yOffset - axisPair.getXAxis().getXAxisHeightHint(approximateXAxisWidth) - getChartPainter().getStyleManager().getPlotPadding() - getChartPainter()
.getStyleManager().getChartPadding();
Rectangle2D yAxisRectangle = new Rectangle2D.Double(xOffset, yOffset, width, height);
this.paintZone = yAxisRectangle;
// g.setColor(Color.green);
......@@ -273,7 +288,7 @@ public class Axis implements ChartPart {
// System.out.println("XAxisHeightHint");
// System.out.println("workingSpace: " + workingSpace);
AxisTickCalculator axisTickCalculator = axisTick.getAxisTickCalculator(workingSpace);
String sampleLabel = " ";
String sampleLabel = "";
// find the longest String in all the labels
for (int i = 0; i < axisTickCalculator.getTickLabels().size(); i++) {
if (axisTickCalculator.getTickLabels().get(i) != null && axisTickCalculator.getTickLabels().get(i).length() > sampleLabel.length()) {
......@@ -293,6 +308,41 @@ public class Axis implements ChartPart {
return titleHeight + axisTickLabelsHeight;
}
private double getYAxisWidthHint(double workingSpace) {
// Axis title
double titleHeight = 0.0;
if (axisTitle.getText() != null && !axisTitle.getText().trim().equalsIgnoreCase("") && getChartPainter().getStyleManager().isYAxisTitleVisible()) {
TextLayout textLayout = new TextLayout(axisTitle.getText(), getChartPainter().getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false));
Rectangle2D rectangle = textLayout.getBounds();
titleHeight = rectangle.getHeight() + getChartPainter().getStyleManager().getAxisTitlePadding();
}
// Axis tick labels
double axisTickLabelsHeight = 0.0;
if (getChartPainter().getStyleManager().isYAxisTicksVisible()) {
// get some real tick labels
// System.out.println("XAxisHeightHint");
// System.out.println("workingSpace: " + workingSpace);
AxisTickCalculator axisTickCalculator = axisTick.getAxisTickCalculator(workingSpace);
String sampleLabel = " ";
// find the longest String in all the labels
for (int i = 0; i < axisTickCalculator.getTickLabels().size(); i++) {
if (axisTickCalculator.getTickLabels().get(i) != null && axisTickCalculator.getTickLabels().get(i).length() > sampleLabel.length()) {
sampleLabel = axisTickCalculator.getTickLabels().get(i);
}
}
// get the height of the label including rotation
TextLayout textLayout = new TextLayout(sampleLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), new FontRenderContext(null, true, false));
Rectangle2D rectangle = textLayout.getBounds();
axisTickLabelsHeight = rectangle.getWidth() + getChartPainter().getStyleManager().getAxisTickPadding() + getChartPainter().getStyleManager().getAxisTickMarkLength();
}
return titleHeight + axisTickLabelsHeight;
}
@Override
public ChartPainter getChartPainter() {
......
......@@ -15,7 +15,6 @@
*/
package com.xeiam.xchart.internal.chartpart;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
......@@ -96,8 +95,8 @@ public class AxisTick implements ChartPart {
);
g.setColor(Color.red);
g.draw(bounds);
// g.setColor(Color.red);
// g.draw(bounds);
}
else if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().isXAxisTicksVisible()) {
......@@ -108,8 +107,8 @@ public class AxisTick implements ChartPart {
bounds = new Rectangle2D.Double(axisTickMarks.getBounds().getX(), axisTickMarks.getBounds().getY(), axisTickLabels.getBounds().getWidth(), axisTickMarks.getBounds().getHeight()
+ getChartPainter().getStyleManager().getAxisTickPadding() + axisTickLabels.getBounds().getHeight());
g.setColor(Color.red);
g.draw(bounds);
// g.setColor(Color.red);
// g.draw(bounds);
}
......
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