diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart08.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart08.java index 643e516a0700a3c6f4c3d6266907fc100d8d1a91..431bbbfea3e0156f73f76fe46f2c9e2538926d41 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart08.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart08.java @@ -25,7 +25,6 @@ import java.util.Random; import com.xeiam.xchart.Chart; import com.xeiam.xchart.ChartBuilder; -import com.xeiam.xchart.StyleManager.TextAlignment; import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.ExampleChart; @@ -47,9 +46,11 @@ public class DateChart08 implements ExampleChart { // Create Chart Chart chart = new ChartBuilder().width(800).height(600).title("Year Scale").build(); chart.getStyleManager().setLegendVisible(false); - chart.getStyleManager().setXAxisLabelRotation(45); - // chart.getStyleManager().setYAxisLabelRotation(90); - chart.getStyleManager().setXAxisLabelAlignment(TextAlignment.Right); + // chart.getStyleManager().setXAxisLabelRotation(45); + // chart.getStyleManager().setXAxisLabelRotation(90); + // chart.getStyleManager().setXAxisLabelRotation(20); + // chart.getStyleManager().setXAxisLabelRotation(80); + // chart.getStyleManager().setXAxisLabelAlignment(TextAlignment.Right); chart.getStyleManager().setDatePattern("yyyy-MM-dd"); // generate data diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java index 630ac5662ecabbc2367d6c9a0dd149912475b03e..2a6f00e2e4d600ad61ffd8b5362f5cae6c737016 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java @@ -21,6 +21,7 @@ import java.util.List; import com.xeiam.xchart.Chart; import com.xeiam.xchart.ChartBuilder; import com.xeiam.xchart.StyleManager.LegendPosition; +import com.xeiam.xchart.StyleManager.TextAlignment; import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.ExampleChart; @@ -60,6 +61,7 @@ public class LineChart01 implements ExampleChart { chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW); chart.getStyleManager().setYAxisLogarithmic(true); + chart.getStyleManager().setXAxisLabelAlignment(TextAlignment.Right); // Series chart.addSeries("10^x", xData, yData); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java index 885732f652ab1dad002fbb7d260cadb1e498aa5a..b0694c72b3c1e7bf5ba161ce40572ce9ad092d3b 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java @@ -22,8 +22,6 @@ import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; -import java.text.SimpleDateFormat; -import java.util.Date; import com.xeiam.xchart.StyleManager.LegendPosition; @@ -152,8 +150,32 @@ public class Axis implements ChartPart { 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 = + + getChartPainter().getWidth() + + - width // y-axis approx. width + + - chartLegendWidth + + - 2 + * getChartPainter().getStyleManager().getChartPadding() + + - (getChartPainter().getStyleManager().isYAxisTicksVisible() ? (getChartPainter().getStyleManager().getPlotPadding()) : 0) + + - (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideE && getChartPainter().getStyleManager().isLegendVisible() ? getChartPainter().getStyleManager() + .getChartPadding() : 0) + + ; + double height = - getChartPainter().getHeight() - yOffset - axisPair.getXAxis().getSizeHint() - getChartPainter().getStyleManager().getPlotPadding() - getChartPainter().getStyleManager().getChartPadding(); + 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); @@ -180,7 +202,7 @@ public class Axis implements ChartPart { double xOffset = axisPair.getYAxis().getBounds().getWidth() + (getChartPainter().getStyleManager().isYAxisTicksVisible() ? getChartPainter().getStyleManager().getPlotPadding() : 0) - + getChartPainter().getStyleManager().getChartPadding(); + + getChartPainter().getStyleManager().getChartPadding(); double yOffset = axisPair.getYAxis().getBounds().getY() + axisPair.getYAxis().getBounds().getHeight() + getChartPainter().getStyleManager().getPlotPadding(); double chartLegendWidth = 0; @@ -192,35 +214,33 @@ public class Axis implements ChartPart { getChartPainter().getWidth() - - axisPair.getYAxis().getBounds().getWidth() // y-axis was already painted + - axisPair.getYAxis().getBounds().getWidth() // y-axis was already painted - - chartLegendWidth + - chartLegendWidth - - 2 - * getChartPainter().getStyleManager().getChartPadding() + - 2 + * getChartPainter().getStyleManager().getChartPadding() - - (getChartPainter().getStyleManager().isYAxisTicksVisible() ? (getChartPainter().getStyleManager().getPlotPadding()) : 0) + - (getChartPainter().getStyleManager().isYAxisTicksVisible() ? (getChartPainter().getStyleManager().getPlotPadding()) : 0) - - (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideE && getChartPainter().getStyleManager().isLegendVisible() ? getChartPainter().getStyleManager() - .getChartPadding() : 0) + - (getChartPainter().getStyleManager().getLegendPosition() == LegendPosition.OutsideE && getChartPainter().getStyleManager().isLegendVisible() ? getChartPainter().getStyleManager() + .getChartPadding() : 0) - ; + ; - double height = this.getSizeHint(); + double height = this.getXAxisHeightHint(width); Rectangle2D xAxisRectangle = new Rectangle2D.Double(xOffset, yOffset, width, height); + // the paint zone this.paintZone = xAxisRectangle; g.setColor(Color.green); g.draw(xAxisRectangle); + // now paint the X-Axis given the above paint zone axisTitle.paint(g); axisTick.paint(g); - xOffset = paintZone.getX(); - yOffset = paintZone.getY(); - width = paintZone.getWidth(); - height = (getChartPainter().getStyleManager().isXAxisTitleVisible() ? axisTitle.getBounds().getHeight() : 0) + axisTick.getBounds().getHeight(); - bounds = new Rectangle2D.Double(xOffset, yOffset, width, height); + bounds = paintZone; g.setColor(Color.yellow); g.draw(bounds); @@ -231,35 +251,34 @@ public class Axis implements ChartPart { /** * @return */ - private double getSizeHint() { - - if (direction == Direction.X) { // X-Axis - - // Axis title - double titleHeight = 0.0; - if (axisTitle.getText() != null && !axisTitle.getText().trim().equalsIgnoreCase("") && getChartPainter().getStyleManager().isXAxisTitleVisible()) { - 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().isXAxisTicksVisible()) { - String tickLabel = - getChartPainter().getAxisPair().getXAxis().getAxisType().equals(AxisType.Date) ? new SimpleDateFormat(getChartPainter().getStyleManager().getDatePattern()).format(new Date()) : "0"; - TextLayout textLayout = new TextLayout(tickLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), new FontRenderContext(null, true, false)); - AffineTransform rot = - getChartPainter().getStyleManager().getXAxisLabelRotation() == 0 ? null : AffineTransform.getRotateInstance(-Math.toRadians(getChartPainter().getStyleManager().getXAxisLabelRotation())); - Shape shape = textLayout.getOutline(rot); - Rectangle2D rectangle = shape.getBounds(); - axisTickLabelsHeight = rectangle.getHeight() + getChartPainter().getStyleManager().getAxisTickPadding() + getChartPainter().getStyleManager().getAxisTickMarkLength(); - } - return titleHeight + axisTickLabelsHeight; + private double getXAxisHeightHint(double workingSpace) { + + // Axis title + double titleHeight = 0.0; + if (axisTitle.getText() != null && !axisTitle.getText().trim().equalsIgnoreCase("") && getChartPainter().getStyleManager().isXAxisTitleVisible()) { + TextLayout textLayout = new TextLayout(axisTitle.getText(), getChartPainter().getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false)); + Rectangle2D rectangle = textLayout.getBounds(); + titleHeight = rectangle.getHeight() + getChartPainter().getStyleManager().getAxisTitlePadding(); } - else { // Y-Axis - return 0; // We layout the yAxis first depending in the xAxis height hint. We don't care about the yAxis height hint + + // Axis tick labels + double axisTickLabelsHeight = 0.0; + if (getChartPainter().getStyleManager().isXAxisTicksVisible()) { + + // get some real tick labels + AxisTickCalculator axisTickCalculator = axisTick.getAxisTickCalculator(workingSpace); + String sampleLabel = axisTickCalculator.getTickLabels().get(0); + + // String tickLabel = + // getChartPainter().getAxisPair().getXAxis().getAxisType().equals(AxisType.Date) ? new SimpleDateFormat(getChartPainter().getStyleManager().getDatePattern()).format(new Date()) : "0"; + TextLayout textLayout = new TextLayout(sampleLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), new FontRenderContext(null, true, false)); + AffineTransform rot = + getChartPainter().getStyleManager().getXAxisLabelRotation() == 0 ? null : AffineTransform.getRotateInstance(-Math.toRadians(getChartPainter().getStyleManager().getXAxisLabelRotation())); + Shape shape = textLayout.getOutline(rot); + Rectangle2D rectangle = shape.getBounds(); + axisTickLabelsHeight = rectangle.getHeight() + getChartPainter().getStyleManager().getAxisTickPadding() + getChartPainter().getStyleManager().getAxisTickMarkLength(); } + return titleHeight + axisTickLabelsHeight; } @Override diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java index a0f739bb5d5b0abf5e52c6d900467f18bf27ee37..ec0f3236942d18b49d25f4bd764f69762915f770 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTick.java @@ -74,31 +74,7 @@ public class AxisTick implements ChartPart { // System.out.println("workingspace= " + workingSpace); } - if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) { - - axisTickCalculator = new AxisTickBarChartCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter()); - - } - else if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().isXAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { - - axisTickCalculator = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); - - } - else if (axis.getDirection() == Axis.Direction.Y && getChartPainter().getStyleManager().isYAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { - - axisTickCalculator = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); - - } - else if (axis.getAxisType() == AxisType.Number) { - - axisTickCalculator = new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); - - } - else if (axis.getAxisType() == AxisType.Date) { - - axisTickCalculator = new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); - - } + axisTickCalculator = getAxisTickCalculator(workingSpace); if (axis.getDirection() == Axis.Direction.Y && getChartPainter().getStyleManager().isYAxisTicksVisible()) { @@ -107,15 +83,15 @@ public class AxisTick implements ChartPart { bounds = new Rectangle2D.Double( - axisTickLabels.getBounds().getX(), + axisTickLabels.getBounds().getX(), - axisTickLabels.getBounds().getY(), + axisTickLabels.getBounds().getY(), - axisTickLabels.getBounds().getWidth() + getChartPainter().getStyleManager().getAxisTickPadding() + axisTickMarks.getBounds().getWidth(), + axisTickLabels.getBounds().getWidth() + getChartPainter().getStyleManager().getAxisTickPadding() + axisTickMarks.getBounds().getWidth(), - axisTickMarks.getBounds().getHeight() + axisTickMarks.getBounds().getHeight() - ); + ); // g.setColor(Color.red); // g.draw(bounds); @@ -136,6 +112,35 @@ public class AxisTick implements ChartPart { } + public AxisTickCalculator getAxisTickCalculator(double workingSpace) { + + if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) { + + return new AxisTickBarChartCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter()); + + } + else if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().isXAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { + + return new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); + + } + else if (axis.getDirection() == Axis.Direction.Y && getChartPainter().getStyleManager().isYAxisLogarithmic() && axis.getAxisType() != AxisType.Date) { + + return new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); + + } + else if (axis.getAxisType() == AxisType.Date) { + + return new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); + + } + else { // number + + return new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager()); + + } + } + @Override public ChartPainter getChartPainter() { diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java index cb01cae4b76b48f9db0f44f5cc206490671c5747..7782376c422c15e013152457ab9f0f8bf5fdef56 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java @@ -15,6 +15,7 @@ */ package com.xeiam.xchart.internal.chartpart; +import java.awt.Color; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.font.FontRenderContext; @@ -170,8 +171,9 @@ public class AxisTickLabels implements ChartPart { g.setTransform(orig); // // debug box - // g.setColor(Color.blue); - // g.draw(new Rectangle2D.Double(xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0, yOffset - tickLabelBounds.getHeight(), tickLabelBounds.getWidth(), tickLabelBounds.getHeight())); + g.setColor(Color.MAGENTA); + g.draw(new Rectangle2D.Double(xPos, yOffset - tickLabelBounds.getHeight(), tickLabelBounds.getWidth(), tickLabelBounds.getHeight())); + g.setColor(getChartPainter().getStyleManager().getAxisTickLabelsColor()); if (tickLabelBounds.getHeight() > maxTickLabelHeight) { maxTickLabelHeight = tickLabelBounds.getHeight();