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

added "missing" plot grid lines, tick marks, tick labels toward the edge of the plot area

parent bfeb9018
Branches
No related tags found
No related merge requests found
Showing
with 105 additions and 92 deletions
......@@ -39,7 +39,7 @@ public class AxisTick implements ChartPart {
/** the bounds */
private Rectangle2D bounds = new Rectangle2D.Double();
AxisTickCalculator gridStep = null;
AxisTickCalculator axisTickCalculator = null;
/**
* Constructor
......@@ -74,27 +74,27 @@ public class AxisTick implements ChartPart {
if (axis.getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) {
gridStep = new AxisTickBarChartCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter());
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) {
gridStep = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
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) {
gridStep = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
axisTickCalculator = new AxisTickLogarithmicCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
}
else if (axis.getAxisType() == AxisType.Number) {
gridStep = new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
axisTickCalculator = new AxisTickNumericalCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
}
else if (axis.getAxisType() == AxisType.Date) {
gridStep = new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
axisTickCalculator = new AxisTickDateCalculator(axis.getDirection(), workingSpace, axis.getMin(), axis.getMax(), getChartPainter().getStyleManager());
}
......@@ -154,11 +154,11 @@ public class AxisTick implements ChartPart {
public List<Double> getTickLocations() {
return gridStep.getTickLocations();
return axisTickCalculator.getTickLocations();
}
public List<String> getTickLabels() {
return gridStep.getTickLabels();
return axisTickCalculator.getTickLabels();
}
}
......@@ -147,7 +147,7 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator {
dateFormatter = new DateFormatter(chartPainter.getStyleManager());
}
for (double tickPosition = firstPosition; tickPosition <= maxValue; tickPosition = tickPosition + gridStep) {
for (double tickPosition = firstPosition; tickPosition <= maxValue + 2 * gridStep; tickPosition = tickPosition + gridStep) {
if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) {
tickLabels.add(numberFormatter.formatNumber(BigDecimal.valueOf(tickPosition), minValue, maxValue));
......
......@@ -102,11 +102,12 @@ public abstract class AxisTickCalculator {
// System.out.println("******");
double firstPosition = minValue - (minValue % gridStep) + gridStep;
if ((firstPosition - minValue) > gridStep) {
firstPosition = minValue - (minValue % gridStep);
}
// double firstPosition = minValue - (minValue % gridStep) + gridStep;
double firstPosition = minValue - (minValue % gridStep) - gridStep;
//
// if ((firstPosition - minValue) > gridStep) {
// firstPosition = minValue - (minValue % gridStep);
// }
return firstPosition;
}
......
......@@ -21,7 +21,7 @@ import com.xeiam.xchart.internal.chartpart.Axis.Direction;
/**
* This class encapsulates the logic to generate the axis tick mark and axis tick label data for rendering the axis ticks for date axes
*
*
* @author timmolter
*/
public class AxisTickDateCalculator extends AxisTickCalculator {
......@@ -30,7 +30,7 @@ public class AxisTickDateCalculator extends AxisTickCalculator {
/**
* Constructor
*
*
* @param axisDirection
* @param workingSpace
* @param minValue
......@@ -70,7 +70,7 @@ public class AxisTickDateCalculator extends AxisTickCalculator {
double firstPosition = getFirstPosition(gridStep);
// generate all tickLabels and tickLocations from the first to last position
for (double tickPosition = firstPosition; tickPosition <= maxValue; tickPosition = tickPosition + gridStep) {
for (double tickPosition = firstPosition; tickPosition <= maxValue + 2 * gridStep; tickPosition = tickPosition + gridStep) {
tickLabels.add(dateFormatter.formatDate(tickPosition, timeUnit));
// here we convert tickPosition finally to plot space, i.e. pixels
......
......@@ -35,7 +35,7 @@ public class AxisTickLabels implements ChartPart {
/**
* Constructor
*
*
* @param axisTick
*/
protected AxisTickLabels(AxisTick axisTick) {
......@@ -60,31 +60,27 @@ public class AxisTickLabels implements ChartPart {
double xOffset = axisTick.getAxis().getAxisTitle().getBounds().getX() + axisTick.getAxis().getAxisTitle().getBounds().getWidth();
double yOffset = axisTick.getAxis().getPaintZone().getY();
double height = axisTick.getAxis().getPaintZone().getHeight();
double maxTickLabelWidth = 0;
for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
String tickLabel = axisTick.getTickLabels().get(i);
// System.out.println(tickLabel);
double tickLocation = axisTick.getTickLocations().get(i);
double flippedTickLocation = yOffset + height - tickLocation;
if (tickLabel != null) { // some are null for logarithmic axes
// AffineTransform orig = g.getTransform();
// AffineTransform at = new AffineTransform();
// at.rotate(Math.PI / -2.0, xOffset, (float) (yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation / 2.0));
// g.transform(at);
if (tickLabel != null && flippedTickLocation > yOffset && flippedTickLocation < yOffset + height) { // some are null for logarithmic axes
FontRenderContext frc = g.getFontRenderContext();
// TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false));
TextLayout layout = new TextLayout(tickLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), frc);
Rectangle2D tickLabelBounds = layout.getBounds();
// layout.draw(g, (float) xOffset, (float) (yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0));
Shape shape = layout.getOutline(null);
AffineTransform orig = g.getTransform();
AffineTransform at = new AffineTransform();
at.translate(xOffset, yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0);
at.translate(xOffset, flippedTickLocation + tickLabelBounds.getHeight() / 2.0);
g.transform(at);
g.fill(shape);
g.setTransform(orig);
......@@ -97,7 +93,7 @@ public class AxisTickLabels implements ChartPart {
}
// bounds
bounds = new Rectangle2D.Double(xOffset, yOffset, maxTickLabelWidth, axisTick.getAxis().getPaintZone().getHeight());
bounds = new Rectangle2D.Double(xOffset, yOffset, maxTickLabelWidth, height);
// g.setColor(Color.blue);
// g.draw(bounds);
......@@ -106,6 +102,7 @@ public class AxisTickLabels implements ChartPart {
double xOffset = axisTick.getAxis().getPaintZone().getX();
double yOffset = axisTick.getAxis().getAxisTitle().getBounds().getY();
double width = axisTick.getAxis().getPaintZone().getWidth();
double maxTickLabelHeight = 0;
// System.out.println("axisTick.getTickLabels().size(): " + axisTick.getTickLabels().size());
......@@ -114,11 +111,12 @@ public class AxisTickLabels implements ChartPart {
String tickLabel = axisTick.getTickLabels().get(i);
// System.out.println("tickLabel: " + tickLabel);
double tickLocation = axisTick.getTickLocations().get(i);
double shiftedTickLocation = xOffset + tickLocation;
if (tickLabel != null && shiftedTickLocation > xOffset && shiftedTickLocation < xOffset + width) { // some are null for logarithmic axes
if (tickLabel != null) { // some are null for logarithmic axes
FontRenderContext frc = g.getFontRenderContext();
TextLayout textLayout = new TextLayout(tickLabel, getChartPainter().getStyleManager().getAxisTickLabelsFont(), frc);
// GlyphVector v = getChartPainter().getStyleManager().getAxisTickLabelsFont().createGlyphVector(frc, tickLabel);
// Shape shape = v.getOutline();
Shape shape = textLayout.getOutline(null);
......@@ -126,7 +124,7 @@ public class AxisTickLabels implements ChartPart {
AffineTransform orig = g.getTransform();
AffineTransform at = new AffineTransform();
at.translate(xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0, yOffset);
at.translate(shiftedTickLocation - tickLabelBounds.getWidth() / 2.0, yOffset);
g.transform(at);
g.fill(shape);
g.setTransform(orig);
......@@ -142,7 +140,7 @@ public class AxisTickLabels implements ChartPart {
}
// bounds
bounds = new Rectangle2D.Double(xOffset, yOffset - maxTickLabelHeight, axisTick.getAxis().getPaintZone().getWidth(), maxTickLabelHeight);
bounds = new Rectangle2D.Double(xOffset, yOffset - maxTickLabelHeight, width, maxTickLabelHeight);
// g.setColor(Color.blue);
// g.draw(bounds);
......
......@@ -33,7 +33,7 @@ public class AxisTickMarks implements ChartPart {
/**
* Constructor
*
*
* @param axisTick
*/
protected AxisTickMarks(AxisTick axisTick) {
......@@ -58,20 +58,26 @@ public class AxisTickMarks implements ChartPart {
double xOffset = axisTick.getAxisTickLabels().getBounds().getX() + axisTick.getAxisTickLabels().getBounds().getWidth() + getChartPainter().getStyleManager().getAxisTickPadding();
double yOffset = axisTick.getAxis().getPaintZone().getY();
// bounds
bounds = new Rectangle2D.Double(xOffset, yOffset, getChartPainter().getStyleManager().getAxisTickMarkLength(), axisTick.getAxis().getPaintZone().getHeight());
// g.setColor(Color.yellow);
// g.draw(bounds);
// tick marks
if (getChartPainter().getStyleManager().isAxisTicksMarksVisible()) {
for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
double tickLocation = axisTick.getTickLocations().get(i);
double flippedTickLocation = yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation;
if (flippedTickLocation > bounds.getY() && flippedTickLocation < bounds.getY() + bounds.getHeight()) {
Shape line =
new Line2D.Double(xOffset, yOffset + axisTick.getAxis().getPaintZone().getHeight() - tickLocation, xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset
+ axisTick.getAxis().getPaintZone().getHeight() - tickLocation);
g.draw(line);
Shape line = new Line2D.Double(xOffset, flippedTickLocation, xOffset + getChartPainter().getStyleManager().getAxisTickMarkLength(), flippedTickLocation);
g.draw(line);
}
}
}
// Line
if (getChartPainter().getStyleManager().isAxisTicksLineVisible()) {
......@@ -82,28 +88,35 @@ public class AxisTickMarks implements ChartPart {
}
// bounds
bounds = new Rectangle2D.Double(xOffset, yOffset, getChartPainter().getStyleManager().getAxisTickMarkLength(), axisTick.getAxis().getPaintZone().getHeight());
// g.setColor(Color.yellow);
// g.draw(bounds);
}
else if (axisTick.getAxis().getDirection() == Axis.Direction.X && getChartPainter().getStyleManager().isXAxisTicksVisible()) { // X-Axis
double xOffset = axisTick.getAxis().getPaintZone().getX();
double yOffset = axisTick.getAxisTickLabels().getBounds().getY() - getChartPainter().getStyleManager().getAxisTickPadding();
// bounds
bounds =
new Rectangle2D.Double(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength(), axisTick.getAxis().getPaintZone().getWidth(), getChartPainter().getStyleManager()
.getAxisTickMarkLength());
// g.setColor(Color.yellow);
// g.draw(bounds);
// tick marks
if (getChartPainter().getStyleManager().isAxisTicksMarksVisible()) {
for (int i = 0; i < axisTick.getTickLabels().size(); i++) {
double tickLocation = axisTick.getTickLocations().get(i);
double shiftedTickLocation = xOffset + tickLocation;
if (shiftedTickLocation > bounds.getX() && shiftedTickLocation < bounds.getX() + bounds.getWidth()) {
Shape line = new Line2D.Double(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength());
g.draw(line);
Shape line = new Line2D.Double(shiftedTickLocation, yOffset, xOffset + tickLocation, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength());
g.draw(line);
}
}
}
// Line
if (getChartPainter().getStyleManager().isAxisTicksLineVisible()) {
......@@ -113,12 +126,6 @@ public class AxisTickMarks implements ChartPart {
g.draw(line);
}
// bounds
bounds =
new Rectangle2D.Double(xOffset, yOffset - getChartPainter().getStyleManager().getAxisTickMarkLength(), axisTick.getAxis().getPaintZone().getWidth(), getChartPainter().getStyleManager()
.getAxisTickMarkLength());
// g.setColor(Color.yellow);
// g.draw(bounds);
}
}
......
......@@ -72,7 +72,7 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator {
// System.out.println("scaledfirstPosition: " + cleanedFirstPosition);
// generate all tickLabels and tickLocations from the first to last position
for (BigDecimal tickPosition = cleanedFirstPosition; tickPosition.compareTo(BigDecimal.valueOf(maxValue)) < 0; tickPosition = tickPosition.add(cleanedGridStep)) {
for (BigDecimal tickPosition = cleanedFirstPosition; tickPosition.compareTo(BigDecimal.valueOf(maxValue + 2 * cleanedGridStep.doubleValue())) < 0; tickPosition = tickPosition.add(cleanedGridStep)) {
tickLabels.add(numberFormatter.formatNumber(tickPosition, minValue, maxValue));
// here we convert tickPosition finally to plot space, i.e. pixels
......
......@@ -41,7 +41,7 @@ public class ChartPainter {
/**
* Constructor
*
*
* @param width
* @param height
*/
......@@ -117,7 +117,7 @@ public class ChartPainter {
/**
* for internal usage
*
*
* @return
*/
public ChartTitle getChartTitle() {
......@@ -127,7 +127,7 @@ public class ChartPainter {
/**
* for internal usage
*
*
* @return
*/
public Legend getChartLegend() {
......@@ -137,7 +137,7 @@ public class ChartPainter {
/**
* for internal usage
*
*
* @return
*/
public AxisPair getAxisPair() {
......@@ -147,7 +147,7 @@ public class ChartPainter {
/**
* for internal usage
*
*
* @return
*/
public Plot getPlot() {
......@@ -167,7 +167,7 @@ public class ChartPainter {
/**
* Gets the Chart's style manager, which can be used to customize the Chart's appearance
*
*
* @return the style manager
*/
public StyleManager getStyleManager() {
......
......@@ -24,6 +24,8 @@ import java.util.List;
import com.xeiam.xchart.StyleManager.ChartType;
/**
* Draws the plot background, the plot border and the horizontal and vertical grid lines
*
* @author timmolter
*/
public class PlotSurface implements ChartPart {
......@@ -33,7 +35,7 @@ public class PlotSurface implements ChartPart {
/**
* Constructor
*
*
* @param plot
*/
protected PlotSurface(Plot plot) {
......@@ -70,26 +72,28 @@ public class PlotSurface implements ChartPart {
List<Double> yAxisTickLocations = getChartPainter().getAxisPair().getYAxis().getAxisTick().getTickLocations();
for (int i = 0; i < yAxisTickLocations.size(); i++) {
double tickLocation = yAxisTickLocations.get(i);
double yOffset = bounds.getY() + bounds.getHeight() - tickLocation;
double yOffset = bounds.getY() + bounds.getHeight() - yAxisTickLocations.get(i);
// draw lines
if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) {
if (yOffset > bounds.getY() && yOffset < bounds.getY() + bounds.getHeight()) {
g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor());
g.setStroke(getChartPainter().getStyleManager().getPlotGridLinesStroke());
Shape line = new Line2D.Double(bounds.getX(), yOffset, bounds.getX() + bounds.getWidth(), yOffset);
g.draw(line);
}
// tick marks
if (getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
g.setColor(getChartPainter().getStyleManager().getAxisTickMarksColor());
g.setStroke(getChartPainter().getStyleManager().getAxisTickMarksStroke());
Shape line = new Line2D.Double(bounds.getX(), yOffset, bounds.getX() + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset);
g.draw(line);
line = new Line2D.Double(bounds.getX() + bounds.getWidth(), yOffset, bounds.getX() + bounds.getWidth() - getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset);
g.draw(line);
// draw lines
if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) {
g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor());
g.setStroke(getChartPainter().getStyleManager().getPlotGridLinesStroke());
Shape line = new Line2D.Double(bounds.getX(), yOffset, bounds.getX() + bounds.getWidth(), yOffset);
g.draw(line);
}
// tick marks
if (getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
g.setColor(getChartPainter().getStyleManager().getAxisTickMarksColor());
g.setStroke(getChartPainter().getStyleManager().getAxisTickMarksStroke());
Shape line = new Line2D.Double(bounds.getX(), yOffset, bounds.getX() + getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset);
g.draw(line);
line = new Line2D.Double(bounds.getX() + bounds.getWidth(), yOffset, bounds.getX() + bounds.getWidth() - getChartPainter().getStyleManager().getAxisTickMarkLength(), yOffset);
g.draw(line);
}
}
}
......@@ -108,24 +112,27 @@ public class PlotSurface implements ChartPart {
double tickLocation = xAxisTickLocations.get(i);
double xOffset = bounds.getX() + tickLocation;
// draw lines
if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) {
g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor());
g.setStroke(getChartPainter().getStyleManager().getPlotGridLinesStroke());
if (xOffset > bounds.getX() && xOffset < bounds.getX() + bounds.getWidth()) {
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + bounds.getHeight());
g.draw(line);
}
// tick marks
if (getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
// draw lines
if (getChartPainter().getStyleManager().isPlotGridLinesVisible()) {
g.setColor(getChartPainter().getStyleManager().getPlotGridLinesColor());
g.setStroke(getChartPainter().getStyleManager().getPlotGridLinesStroke());
g.setColor(getChartPainter().getStyleManager().getAxisTickMarksColor());
g.setStroke(getChartPainter().getStyleManager().getAxisTickMarksStroke());
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + bounds.getHeight());
g.draw(line);
}
// tick marks
if (getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + getChartPainter().getStyleManager().getAxisTickMarkLength());
g.draw(line);
line = new Line2D.Double(xOffset, bounds.getY() + bounds.getHeight(), xOffset, bounds.getY() + bounds.getHeight() - getChartPainter().getStyleManager().getAxisTickMarkLength());
g.draw(line);
g.setColor(getChartPainter().getStyleManager().getAxisTickMarksColor());
g.setStroke(getChartPainter().getStyleManager().getAxisTickMarksStroke());
Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + getChartPainter().getStyleManager().getAxisTickMarkLength());
g.draw(line);
line = new Line2D.Double(xOffset, bounds.getY() + bounds.getHeight(), xOffset, bounds.getY() + bounds.getHeight() - getChartPainter().getStyleManager().getAxisTickMarkLength());
g.draw(line);
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment