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

chart axis ticks style refactor

parent 5310ee1e
Branches
No related tags found
No related merge requests found
Showing
with 285 additions and 209 deletions
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<groupId>com.xeiam.xchart</groupId> <groupId>com.xeiam.xchart</groupId>
<artifactId>xchart-parent</artifactId> <artifactId>xchart-parent</artifactId>
<version>1.3.2-SNAPSHOT</version> <version>2.0.0-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>XChart Parent</name> <name>XChart Parent</name>
<description>Basic Charts for Java Applications</description> <description>Basic Charts for Java Applications</description>
......
...@@ -76,7 +76,7 @@ public class Example1 implements ExampleChart { ...@@ -76,7 +76,7 @@ public class Example1 implements ExampleChart {
// Customize Chart // Customize Chart
chart.setTitle("Example1"); chart.setTitle("Example1");
chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setChartLegendVisible(false); chart.getStyleManager().setLegendVisible(false);
// Series 1 // Series 1
Series series1 = chart.addSeries("data", xData, yData); Series series1 = chart.addSeries("data", xData, yData);
......
...@@ -46,7 +46,7 @@ public class Example10 implements ExampleChart { ...@@ -46,7 +46,7 @@ public class Example10 implements ExampleChart {
chart.setTitle("Example10"); chart.setTitle("Example10");
chart.setXAxisTitle("X"); chart.setXAxisTitle("X");
chart.setYAxisTitle("Y"); chart.setYAxisTitle("Y");
chart.getStyleManager().setChartLegendVisible(false); chart.getStyleManager().setLegendVisible(false);
for (int i = 0; i < 200; i++) { for (int i = 0; i < 200; i++) {
Series series = chart.addSeries("", new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() }); Series series = chart.addSeries("", new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() });
......
...@@ -57,7 +57,7 @@ public class Example2 implements ExampleChart { ...@@ -57,7 +57,7 @@ public class Example2 implements ExampleChart {
// Customize Chart // Customize Chart
chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setChartLegendVisible(false); chart.getStyleManager().setLegendVisible(false);
// Series 1 // Series 1
Series series1 = chart.addSeries("y=sin(x)", xData1, yData1); Series series1 = chart.addSeries("y=sin(x)", xData1, yData1);
......
...@@ -58,8 +58,8 @@ public class Example8 implements ExampleChart { ...@@ -58,8 +58,8 @@ public class Example8 implements ExampleChart {
// Customize Chart // Customize Chart
chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setChartLegendVisible(false); chart.getStyleManager().setLegendVisible(false);
chart.setAxisTitlesVisible(false); chart.getStyleManager().setAxisTitlesVisible(false);
// Series 1 // Series 1
Series series1 = chart.addSeries("10^(-x)", xData1, yData1, errorBars); Series series1 = chart.addSeries("10^(-x)", xData1, yData1, errorBars);
......
...@@ -77,14 +77,13 @@ public class Example9 implements ExampleChart { ...@@ -77,14 +77,13 @@ public class Example9 implements ExampleChart {
chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY)); chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY));
chart.setGridLinesColor(new Color(255, 255, 255)); chart.setGridLinesColor(new Color(255, 255, 255));
chart.getStyleManager().setChartBackgroundColor(Color.WHITE); chart.getStyleManager().setChartBackgroundColor(Color.WHITE);
chart.getStyleManager().setChartLegendBackgroundColor(Color.PINK); chart.getStyleManager().setLegendBackgroundColor(Color.PINK);
chart.getStyleManager().setChartBordersColor(Color.GREEN); chart.getStyleManager().setChartBordersColor(Color.GREEN);
chart.getStyleManager().setChartFontColor(Color.MAGENTA); chart.getStyleManager().setChartFontColor(Color.MAGENTA);
chart.getStyleManager().setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24)); chart.getStyleManager().setChartTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24));
chart.getStyleManager().setChartLegendFont(new Font(Font.SERIF, Font.PLAIN, 18)); chart.getStyleManager().setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18));
chart.setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); chart.getStyleManager().setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18));
chart.setTickLabelFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); chart.getStyleManager().setAxisTicksFont(new Font(Font.SERIF, Font.PLAIN, 11));
chart.setTickLabelFont(new Font(Font.SERIF, Font.PLAIN, 11));
chart.setDateFormatter("dd-MMM"); chart.setDateFormatter("dd-MMM");
chart.setDecmialFormatter("#.000"); chart.setDecmialFormatter("#.000");
chart.setLocale(Locale.GERMAN); chart.setLocale(Locale.GERMAN);
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package com.xeiam.xchart; package com.xeiam.xchart;
import java.awt.Color; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -26,8 +25,8 @@ import java.util.Locale; ...@@ -26,8 +25,8 @@ import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import com.xeiam.xchart.internal.chartpart.AxisPair; import com.xeiam.xchart.internal.chartpart.AxisPair;
import com.xeiam.xchart.internal.chartpart.ChartLegend;
import com.xeiam.xchart.internal.chartpart.ChartTitle; import com.xeiam.xchart.internal.chartpart.ChartTitle;
import com.xeiam.xchart.internal.chartpart.Legend;
import com.xeiam.xchart.internal.chartpart.Plot; import com.xeiam.xchart.internal.chartpart.Plot;
import com.xeiam.xchart.style.Series; import com.xeiam.xchart.style.Series;
import com.xeiam.xchart.style.StyleManager; import com.xeiam.xchart.style.StyleManager;
...@@ -47,7 +46,7 @@ public class Chart { ...@@ -47,7 +46,7 @@ public class Chart {
// Chart Parts // Chart Parts
public ChartTitle chartTitle = new ChartTitle(this); public ChartTitle chartTitle = new ChartTitle(this);
public ChartLegend chartLegend = new ChartLegend(this); public Legend chartLegend = new Legend(this);
public AxisPair axisPair = new AxisPair(this); public AxisPair axisPair = new AxisPair(this);
protected Plot plot = new Plot(this); protected Plot plot = new Plot(this);
...@@ -75,7 +74,7 @@ public class Chart { ...@@ -75,7 +74,7 @@ public class Chart {
setTitle(chartBuilder.title); setTitle(chartBuilder.title);
setXAxisTitle(chartBuilder.xAxisTitle); setXAxisTitle(chartBuilder.xAxisTitle);
setYAxisTitle(chartBuilder.yAxisTitle); setYAxisTitle(chartBuilder.yAxisTitle);
styleManager.setChartLegendVisible(chartBuilder.isLegendVisible); styleManager.setLegendVisible(chartBuilder.isLegendVisible);
} }
/** /**
...@@ -232,6 +231,11 @@ public class Chart { ...@@ -232,6 +231,11 @@ public class Chart {
*/ */
public void setXAxisTitle(String title) { public void setXAxisTitle(String title) {
if (title == null || title.trim().equalsIgnoreCase("")) {
styleManager.setxAxisTitleVisible(false);
} else {
styleManager.setxAxisTitleVisible(true);
}
this.axisPair.xAxis.axisTitle.setText(title); this.axisPair.xAxis.axisTitle.setText(title);
} }
...@@ -242,73 +246,16 @@ public class Chart { ...@@ -242,73 +246,16 @@ public class Chart {
*/ */
public void setYAxisTitle(String title) { public void setYAxisTitle(String title) {
if (title == null || title.trim().equalsIgnoreCase("")) {
styleManager.setyAxisTitleVisible(false);
} else {
styleManager.setyAxisTitleVisible(true);
}
this.axisPair.yAxis.axisTitle.setText(title); this.axisPair.yAxis.axisTitle.setText(title);
} }
// ChartPart visibility //////////////////////////////// // ChartPart visibility ////////////////////////////////
/**
* Set the x- and y-axis titles visibility
*
* @param isVisible
*/
public void setAxisTitlesVisible(boolean isVisible) {
this.axisPair.xAxis.getAxisTitle().setVisible(isVisible);
this.axisPair.yAxis.getAxisTitle().setVisible(isVisible);
}
/**
* Set the x-axis title visibility
*
* @param isVisible
*/
public void setXAxisTitleVisible(boolean isVisible) {
this.axisPair.xAxis.getAxisTitle().setVisible(isVisible);
}
/**
* Set the y-axis title visibility
*
* @param isVisible
*/
public void setYAxisTitleVisible(boolean isVisible) {
this.axisPair.yAxis.getAxisTitle().setVisible(isVisible);
}
/**
* Set the x- and y-axis tick marks and labels visibility
*
* @param isVisible
*/
public void setAxisTicksVisible(boolean isVisible) {
this.axisPair.xAxis.axisTick.setVisible(isVisible);
this.axisPair.yAxis.axisTick.setVisible(isVisible);
}
/**
* Set the x-axis tick marks and labels visibility
*
* @param isVisible
*/
public void setXAxisTicksVisible(boolean isVisible) {
this.axisPair.xAxis.axisTick.setVisible(isVisible);
}
/**
* Set the y-axis tick marks and labels visibility
*
* @param isVisible
*/
public void setYAxisTicksVisible(boolean isVisible) {
this.axisPair.yAxis.axisTick.setVisible(isVisible);
}
/** /**
* Set the chart grid lines visibility * Set the chart grid lines visibility
* *
...@@ -339,28 +286,6 @@ public class Chart { ...@@ -339,28 +286,6 @@ public class Chart {
this.plot.plotSurface.setGridLinesColor(color); this.plot.plotSurface.setGridLinesColor(color);
} }
/**
* Set the x- and y-axis title font
*
* @param font
*/
public void setAxisTitleFont(Font font) {
this.axisPair.xAxis.axisTitle.font = font;
this.axisPair.yAxis.axisTitle.font = font;
}
/**
* Set the x- and y-axis tick label font
*
* @param font
*/
public void setTickLabelFont(Font font) {
this.axisPair.xAxis.axisTick.axisTickLabels.font = font;
this.axisPair.yAxis.axisTick.axisTickLabels.font = font;
}
/** /**
* Set the String formatter for Data x-axis * Set the String formatter for Data x-axis
* *
......
...@@ -85,7 +85,7 @@ public final class QuickChart { ...@@ -85,7 +85,7 @@ public final class QuickChart {
if (seriesNames != null) { if (seriesNames != null) {
series = chart.addSeries(seriesNames[i], xData, yData[i]); series = chart.addSeries(seriesNames[i], xData, yData[i]);
} else { } else {
chart.getStyleManager().setChartLegendVisible(false); chart.getStyleManager().setLegendVisible(false);
series = chart.addSeries(" " + i, xData, yData[i]); series = chart.addSeries(" " + i, xData, yData[i]);
} }
series.setMarker(SeriesMarker.NONE); series.setMarker(SeriesMarker.NONE);
......
...@@ -80,9 +80,8 @@ public class Axis implements IChartPart { ...@@ -80,9 +80,8 @@ public class Axis implements IChartPart {
this.axisPair = axisPair; this.axisPair = axisPair;
this.direction = direction; this.direction = direction;
axisTitle = new AxisTitle(this, direction == Direction.X ? axisPair.chart.getStyleManager().isxAxisTitleVisible() : axisPair.chart.getStyleManager().isyAxisTitleVisible());
axisTitle = new AxisTitle(this); axisTick = new AxisTick(this, direction == Direction.X ? axisPair.chart.getStyleManager().isxAxisTicksVisible() : axisPair.chart.getStyleManager().isyAxisTicksVisible());
axisTick = new AxisTick(this);
} }
/** /**
...@@ -142,16 +141,16 @@ public class Axis implements IChartPart { ...@@ -142,16 +141,16 @@ public class Axis implements IChartPart {
// Axis title // Axis title
double titleHeight = 0.0; double titleHeight = 0.0;
if (axisTitle.isVisible) { if (axisPair.chart.getStyleManager().isxAxisTitleVisible()) {
TextLayout textLayout = new TextLayout(axisTitle.getText(), axisTitle.getFont(), new FontRenderContext(null, true, false)); TextLayout textLayout = new TextLayout(axisTitle.getText(), axisTick.axis.axisPair.chart.getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false));
Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0);
titleHeight = rectangle.getHeight() + AxisTitle.AXIS_TITLE_PADDING; titleHeight = rectangle.getHeight() + AxisTitle.AXIS_TITLE_PADDING;
} }
// Axis tick labels // Axis tick labels
double axisTickLabelsHeight = 0.0; double axisTickLabelsHeight = 0.0;
if (axisTick.isVisible) { if (axisPair.chart.getStyleManager().isxAxisTicksVisible()) {
TextLayout textLayout = new TextLayout("0", axisTick.axisTickLabels.font, new FontRenderContext(null, true, false)); TextLayout textLayout = new TextLayout("0", axisTick.axis.axisPair.chart.getStyleManager().getAxisTicksFont(), new FontRenderContext(null, true, false));
Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0);
axisTickLabelsHeight = rectangle.getHeight() + AxisTick.AXIS_TICK_PADDING + AxisTickMarks.TICK_LENGTH + Plot.PLOT_PADDING; axisTickLabelsHeight = rectangle.getHeight() + AxisTick.AXIS_TICK_PADDING + AxisTickMarks.TICK_LENGTH + Plot.PLOT_PADDING;
} }
...@@ -192,7 +191,7 @@ public class Axis implements IChartPart { ...@@ -192,7 +191,7 @@ public class Axis implements IChartPart {
xOffset = (int) paintZone.getX(); xOffset = (int) paintZone.getX();
yOffset = (int) paintZone.getY(); yOffset = (int) paintZone.getY();
width = (int) (axisTitle.isVisible ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth(); width = (int) (axisPair.chart.getStyleManager().isyAxisTitleVisible() ? axisTitle.getBounds().getWidth() : 0) + (int) axisTick.getBounds().getWidth();
height = (int) paintZone.getHeight(); height = (int) paintZone.getHeight();
bounds = new Rectangle(xOffset, yOffset, width, height); bounds = new Rectangle(xOffset, yOffset, width, height);
// g.setColor(Color.yellow); // g.setColor(Color.yellow);
...@@ -203,9 +202,10 @@ public class Axis implements IChartPart { ...@@ -203,9 +202,10 @@ public class Axis implements IChartPart {
// calculate paint zone // calculate paint zone
// |____________________| // |____________________|
int xOffset = (int) (axisPair.yAxis.getBounds().getWidth() + (axisPair.yAxis.axisTick.isVisible ? Plot.PLOT_PADDING : 0) + axisPair.chart.getStyleManager().getChartPadding()); int xOffset = (int) (axisPair.yAxis.getBounds().getWidth() + (axisPair.chart.getStyleManager().isyAxisTicksVisible() ? Plot.PLOT_PADDING : 0) + axisPair.chart.getStyleManager()
.getChartPadding());
int yOffset = (int) (axisPair.yAxis.getBounds().getY() + axisPair.yAxis.getBounds().getHeight()); int yOffset = (int) (axisPair.yAxis.getBounds().getY() + axisPair.yAxis.getBounds().getHeight());
int width = (int) (axisPair.chart.width - axisPair.yAxis.getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (axisPair.chart.getStyleManager().isChartLegendVisisble() ? 3 : 2) int width = (int) (axisPair.chart.width - axisPair.yAxis.getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (axisPair.chart.getStyleManager().isLegendVisible() ? 3 : 2)
* axisPair.chart.getStyleManager().getChartPadding()); * axisPair.chart.getStyleManager().getChartPadding());
int height = this.getSizeHint(); int height = this.getSizeHint();
Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height);
...@@ -219,7 +219,7 @@ public class Axis implements IChartPart { ...@@ -219,7 +219,7 @@ public class Axis implements IChartPart {
xOffset = (int) paintZone.getX(); xOffset = (int) paintZone.getX();
yOffset = (int) paintZone.getY(); yOffset = (int) paintZone.getY();
width = (int) paintZone.getWidth(); width = (int) paintZone.getWidth();
height = (int) ((axisTitle.isVisible ? axisTitle.getBounds().getHeight() : 0) + (int) axisTick.getBounds().getHeight()); height = (int) ((axisPair.chart.getStyleManager().isxAxisTitleVisible() ? axisTitle.getBounds().getHeight() : 0) + (int) axisTick.getBounds().getHeight());
bounds = new Rectangle(xOffset, yOffset, width, height); bounds = new Rectangle(xOffset, yOffset, width, height);
bounds = new Rectangle(xOffset, yOffset, width, height); bounds = new Rectangle(xOffset, yOffset, width, height);
// g.setColor(Color.yellow); // g.setColor(Color.yellow);
......
...@@ -52,10 +52,10 @@ public class AxisTick implements IChartPart, IHideable { ...@@ -52,10 +52,10 @@ public class AxisTick implements IChartPart, IHideable {
/** the axistickmarks */ /** the axistickmarks */
protected AxisTickMarks axisTickMarks; protected AxisTickMarks axisTickMarks;
/** the arraylist of tick label position in pixels */ /** the List of tick label position in pixels */
protected List<Integer> tickLocations; protected List<Integer> tickLocations;
/** the arraylist of tick label values */ /** the List of tick label values */
protected List<String> tickLabels; protected List<String> tickLabels;
private int workingSpace; private int workingSpace;
...@@ -74,16 +74,17 @@ public class AxisTick implements IChartPart, IHideable { ...@@ -74,16 +74,17 @@ public class AxisTick implements IChartPart, IHideable {
private Rectangle bounds; private Rectangle bounds;
/** the visibility state of axistick */ /** the visibility state of axistick */
protected boolean isVisible = true; // default to true private boolean isVisible = true; // default to true
/** /**
* Constructor * Constructor
* *
* @param axis the axis * @param axis the axis
*/ */
protected AxisTick(Axis axis) { protected AxisTick(Axis axis, boolean isVisible) {
this.axis = axis; this.axis = axis;
this.isVisible = isVisible;
axisTickLabels = new AxisTickLabels(this); axisTickLabels = new AxisTickLabels(this);
axisTickMarks = new AxisTickMarks(this); axisTickMarks = new AxisTickMarks(this);
...@@ -270,6 +271,6 @@ public class AxisTick implements IChartPart, IHideable { ...@@ -270,6 +271,6 @@ public class AxisTick implements IChartPart, IHideable {
@Override @Override
public void setVisible(boolean isVisible) { public void setVisible(boolean isVisible) {
this.isVisible = isVisible; // this.isVisible = isVisible;
} }
} }
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package com.xeiam.xchart.internal.chartpart; package com.xeiam.xchart.internal.chartpart;
import java.awt.Font;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.font.FontRenderContext; import java.awt.font.FontRenderContext;
...@@ -31,9 +30,6 @@ public class AxisTickLabels implements IChartPart { ...@@ -31,9 +30,6 @@ public class AxisTickLabels implements IChartPart {
/** parent */ /** parent */
private final AxisTick axisTick; private final AxisTick axisTick;
/** the font */
public Font font;
/** the bounds */ /** the bounds */
private Rectangle bounds; private Rectangle bounds;
...@@ -45,7 +41,6 @@ public class AxisTickLabels implements IChartPart { ...@@ -45,7 +41,6 @@ public class AxisTickLabels implements IChartPart {
protected AxisTickLabels(AxisTick axisTick) { protected AxisTickLabels(AxisTick axisTick) {
this.axisTick = axisTick; this.axisTick = axisTick;
font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font
} }
@Override @Override
...@@ -58,7 +53,7 @@ public class AxisTickLabels implements IChartPart { ...@@ -58,7 +53,7 @@ public class AxisTickLabels implements IChartPart {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
bounds = new Rectangle(); bounds = new Rectangle();
g.setFont(font); g.setFont(axisTick.axis.axisPair.chart.getStyleManager().getAxisTicksFont());
g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getChartFontColor()); g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getChartFontColor());
...@@ -75,7 +70,7 @@ public class AxisTickLabels implements IChartPart { ...@@ -75,7 +70,7 @@ public class AxisTickLabels implements IChartPart {
FontRenderContext frc = g.getFontRenderContext(); FontRenderContext frc = g.getFontRenderContext();
// TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false)); // TextLayout layout = new TextLayout(tickLabel, font, new FontRenderContext(null, true, false));
TextLayout layout = new TextLayout(tickLabel, font, frc); TextLayout layout = new TextLayout(tickLabel, axisTick.axis.axisPair.chart.getStyleManager().getAxisTicksFont(), frc);
Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0);
layout.draw(g, xOffset, (int) (yOffset + axisTick.axis.getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0)); layout.draw(g, xOffset, (int) (yOffset + axisTick.axis.getPaintZone().getHeight() - tickLocation + tickLabelBounds.getHeight() / 2.0));
...@@ -100,7 +95,7 @@ public class AxisTickLabels implements IChartPart { ...@@ -100,7 +95,7 @@ public class AxisTickLabels implements IChartPart {
int tickLocation = axisTick.tickLocations.get(i); int tickLocation = axisTick.tickLocations.get(i);
FontRenderContext frc = g.getFontRenderContext(); FontRenderContext frc = g.getFontRenderContext();
TextLayout layout = new TextLayout(tickLabel, font, frc); TextLayout layout = new TextLayout(tickLabel, axisTick.axis.axisPair.chart.getStyleManager().getAxisTicksFont(), frc);
Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0); Rectangle tickLabelBounds = layout.getPixelBounds(null, 0, 0);
layout.draw(g, (int) (xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0), yOffset); layout.draw(g, (int) (xOffset + tickLocation - tickLabelBounds.getWidth() / 2.0), yOffset);
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package com.xeiam.xchart.internal.chartpart; package com.xeiam.xchart.internal.chartpart;
import java.awt.Font;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.font.FontRenderContext; import java.awt.font.FontRenderContext;
...@@ -38,12 +37,6 @@ public class AxisTitle implements IChartPart, IHideable { ...@@ -38,12 +37,6 @@ public class AxisTitle implements IChartPart, IHideable {
/** the title text */ /** the title text */
protected String text = ""; // default to "" protected String text = ""; // default to ""
/** the visibility state of title */
protected boolean isVisible = false; // default to false, set true if text is set
/** the font */
public Font font;
/** the bounds */ /** the bounds */
private Rectangle bounds; private Rectangle bounds;
...@@ -52,10 +45,9 @@ public class AxisTitle implements IChartPart, IHideable { ...@@ -52,10 +45,9 @@ public class AxisTitle implements IChartPart, IHideable {
* *
* @param axis the axis * @param axis the axis
*/ */
protected AxisTitle(Axis axis) { protected AxisTitle(Axis axis, boolean isVisible) {
this.axis = axis; this.axis = axis;
font = new Font(Font.SANS_SERIF, Font.BOLD, 12); // default font
} }
protected String getText() { protected String getText() {
...@@ -65,28 +57,12 @@ public class AxisTitle implements IChartPart, IHideable { ...@@ -65,28 +57,12 @@ public class AxisTitle implements IChartPart, IHideable {
public void setText(String text) { public void setText(String text) {
if (text.trim().equalsIgnoreCase("")) {
this.isVisible = false;
} else {
this.isVisible = true;
}
this.text = text; this.text = text;
} }
protected Font getFont() {
return font;
}
@Override @Override
public void setVisible(boolean isVisible) { public void setVisible(boolean isVisible) {
if (!text.trim().equalsIgnoreCase("")) {
this.isVisible = isVisible;
} else {
// don't allow a set to true if text is empty!
this.isVisible = false; // set to false for good measure
}
} }
@Override @Override
...@@ -101,13 +77,13 @@ public class AxisTitle implements IChartPart, IHideable { ...@@ -101,13 +77,13 @@ public class AxisTitle implements IChartPart, IHideable {
bounds = new Rectangle(); bounds = new Rectangle();
g.setColor(axis.axisPair.chart.getStyleManager().getChartFontColor()); g.setColor(axis.axisPair.chart.getStyleManager().getChartFontColor());
g.setFont(font); g.setFont(axis.axisPair.chart.getStyleManager().getAxisTitleFont());
if (axis.direction == Axis.Direction.Y) { if (axis.direction == Axis.Direction.Y) {
if (isVisible) { if (axis.axisPair.chart.getStyleManager().isyAxisTitleVisible()) {
FontRenderContext frc = g.getFontRenderContext(); FontRenderContext frc = g.getFontRenderContext();
TextLayout nonRotatedTextLayout = new TextLayout(text, font, frc); TextLayout nonRotatedTextLayout = new TextLayout(text, axis.axisPair.chart.getStyleManager().getAxisTitleFont(), frc);
Rectangle nonRotatedRectangle = nonRotatedTextLayout.getPixelBounds(null, 0, 0); Rectangle nonRotatedRectangle = nonRotatedTextLayout.getPixelBounds(null, 0, 0);
// System.out.println(nonRotatedRectangle); // System.out.println(nonRotatedRectangle);
...@@ -145,10 +121,10 @@ public class AxisTitle implements IChartPart, IHideable { ...@@ -145,10 +121,10 @@ public class AxisTitle implements IChartPart, IHideable {
} else { } else {
if (isVisible) { if (axis.axisPair.chart.getStyleManager().isxAxisTitleVisible()) {
FontRenderContext frc = g.getFontRenderContext(); FontRenderContext frc = g.getFontRenderContext();
TextLayout textLayout = new TextLayout(text, font, frc); TextLayout textLayout = new TextLayout(text, axis.axisPair.chart.getStyleManager().getAxisTitleFont(), frc);
Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0);
// System.out.println(rectangle); // System.out.println(rectangle);
......
...@@ -30,7 +30,7 @@ import com.xeiam.xchart.style.Series; ...@@ -30,7 +30,7 @@ import com.xeiam.xchart.style.Series;
/** /**
* @author timmolter * @author timmolter
*/ */
public class ChartLegend implements IChartPart, IHideable { public class Legend implements IChartPart, IHideable {
/** parent */ /** parent */
private final Chart chart; private final Chart chart;
...@@ -43,7 +43,7 @@ public class ChartLegend implements IChartPart, IHideable { ...@@ -43,7 +43,7 @@ public class ChartLegend implements IChartPart, IHideable {
* *
* @param chart * @param chart
*/ */
public ChartLegend(Chart chart) { public Legend(Chart chart) {
this.chart = chart; this.chart = chart;
} }
...@@ -57,9 +57,9 @@ public class ChartLegend implements IChartPart, IHideable { ...@@ -57,9 +57,9 @@ public class ChartLegend implements IChartPart, IHideable {
public void paint(Graphics2D g) { public void paint(Graphics2D g) {
bounds = new Rectangle(); bounds = new Rectangle();
g.setFont(chart.getStyleManager().getChartLegendFont()); g.setFont(chart.getStyleManager().getLegendFont());
if (chart.getStyleManager().isChartLegendVisisble()) { if (chart.getStyleManager().isLegendVisible()) {
Map<Integer, Series> seriesMap = chart.axisPair.seriesMap; Map<Integer, Series> seriesMap = chart.axisPair.seriesMap;
...@@ -69,7 +69,7 @@ public class ChartLegend implements IChartPart, IHideable { ...@@ -69,7 +69,7 @@ public class ChartLegend implements IChartPart, IHideable {
for (Integer seriesId : seriesMap.keySet()) { for (Integer seriesId : seriesMap.keySet()) {
Series series = seriesMap.get(seriesId); Series series = seriesMap.get(seriesId);
TextLayout textLayout = new TextLayout(series.name, chart.getStyleManager().getChartLegendFont(), new FontRenderContext(null, true, false)); TextLayout textLayout = new TextLayout(series.name, chart.getStyleManager().getLegendFont(), new FontRenderContext(null, true, false));
Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0);
// System.out.println(rectangle); // System.out.println(rectangle);
if (rectangle.getWidth() > legendTextContentMaxWidth) { if (rectangle.getWidth() > legendTextContentMaxWidth) {
...@@ -83,25 +83,25 @@ public class ChartLegend implements IChartPart, IHideable { ...@@ -83,25 +83,25 @@ public class ChartLegend implements IChartPart, IHideable {
// determine legend content height // determine legend content height
int legendContentHeight = 0; int legendContentHeight = 0;
int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE); int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE);
legendContentHeight = maxContentHeight * seriesMap.size() + chart.getStyleManager().getChartLegendPadding() * (seriesMap.size() - 1); legendContentHeight = maxContentHeight * seriesMap.size() + chart.getStyleManager().getLegendPadding() * (seriesMap.size() - 1);
// determine legend content width // determine legend content width
int legendContentWidth = (int) (3.0 * Marker.SIZE + chart.getStyleManager().getChartLegendPadding() + legendTextContentMaxWidth); int legendContentWidth = (int) (3.0 * Marker.SIZE + chart.getStyleManager().getLegendPadding() + legendTextContentMaxWidth);
// Draw Legend Box // Draw Legend Box
int legendBoxWidth = legendContentWidth + 2 * chart.getStyleManager().getChartLegendPadding(); int legendBoxWidth = legendContentWidth + 2 * chart.getStyleManager().getLegendPadding();
int legendBoxHeight = legendContentHeight + 2 * chart.getStyleManager().getChartLegendPadding(); int legendBoxHeight = legendContentHeight + 2 * chart.getStyleManager().getLegendPadding();
int xOffset = chart.width - legendBoxWidth - chart.getStyleManager().getChartPadding(); int xOffset = chart.width - legendBoxWidth - chart.getStyleManager().getChartPadding();
int yOffset = (int) ((chart.height - legendBoxHeight) / 2.0 + chart.chartTitle.getBounds().getY() + chart.chartTitle.getBounds().getHeight()); int yOffset = (int) ((chart.height - legendBoxHeight) / 2.0 + chart.chartTitle.getBounds().getY() + chart.chartTitle.getBounds().getHeight());
g.setColor(chart.getStyleManager().getChartBordersColor()); g.setColor(chart.getStyleManager().getChartBordersColor());
g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight);
g.setColor(chart.getStyleManager().getChartLegendBackgroundColor()); g.setColor(chart.getStyleManager().getLegendBackgroundColor());
g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1);
// Draw legend content inside legend box // Draw legend content inside legend box
int startx = xOffset + chart.getStyleManager().getChartLegendPadding(); int startx = xOffset + chart.getStyleManager().getLegendPadding();
int starty = yOffset + chart.getStyleManager().getChartLegendPadding(); int starty = yOffset + chart.getStyleManager().getLegendPadding();
for (Integer seriesId : seriesMap.keySet()) { for (Integer seriesId : seriesMap.keySet()) {
Series series = seriesMap.get(seriesId); Series series = seriesMap.get(seriesId);
// paint line // paint line
...@@ -118,9 +118,9 @@ public class ChartLegend implements IChartPart, IHideable { ...@@ -118,9 +118,9 @@ public class ChartLegend implements IChartPart, IHideable {
// paint series name // paint series name
g.setColor(chart.getStyleManager().getChartFontColor()); g.setColor(chart.getStyleManager().getChartFontColor());
TextLayout layout = new TextLayout(series.name, chart.getStyleManager().getChartLegendFont(), new FontRenderContext(null, true, false)); TextLayout layout = new TextLayout(series.name, chart.getStyleManager().getLegendFont(), new FontRenderContext(null, true, false));
layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + chart.getStyleManager().getChartLegendPadding()), (starty + Marker.SIZE)); layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + chart.getStyleManager().getLegendPadding()), (starty + Marker.SIZE));
starty = starty + legendTextContentMaxHeight + chart.getStyleManager().getChartLegendPadding(); starty = starty + legendTextContentMaxHeight + chart.getStyleManager().getLegendPadding();
} }
// bounds // bounds
......
...@@ -57,7 +57,7 @@ public class Plot implements IChartPart { ...@@ -57,7 +57,7 @@ public class Plot implements IChartPart {
bounds = new Rectangle(); bounds = new Rectangle();
// calculate bounds // calculate bounds
int xOffset = (int) (chart.axisPair.yAxis.getBounds().getX() + chart.axisPair.yAxis.getBounds().getWidth() + (chart.axisPair.yAxis.axisTick.isVisible ? (Plot.PLOT_PADDING + 1) : 0)); int xOffset = (int) (chart.axisPair.yAxis.getBounds().getX() + chart.axisPair.yAxis.getBounds().getWidth() + (chart.getStyleManager().isyAxisTicksVisible() ? (Plot.PLOT_PADDING + 1) : 0));
int yOffset = (int) (chart.axisPair.yAxis.getBounds().getY()); int yOffset = (int) (chart.axisPair.yAxis.getBounds().getY());
int width = (int) chart.axisPair.xAxis.getBounds().getWidth(); int width = (int) chart.axisPair.xAxis.getBounds().getWidth();
int height = (int) chart.axisPair.yAxis.getBounds().getHeight(); int height = (int) chart.axisPair.yAxis.getBounds().getHeight();
......
...@@ -43,10 +43,17 @@ public class StyleManager { ...@@ -43,10 +43,17 @@ public class StyleManager {
private Font chartTitleFont; private Font chartTitleFont;
private boolean isChartTitleVisible; private boolean isChartTitleVisible;
private boolean isChartLegendVisisble; private boolean isLegendVisible;
private Color chartLegendBackgroundColor; private Color legendBackgroundColor;
private Font chartLegendFont; private Font legendFont;
private int chartLegendPadding; private int legendPadding;
private boolean xAxisTitleVisible;
private boolean yAxisTitleVisible;
private Font axisTitleFont;
private boolean xAxisTicksVisible;
private boolean yAxisTicksVisible;
private Font axisTicksFont;
/** /**
* Constructor * Constructor
...@@ -66,11 +73,18 @@ public class StyleManager { ...@@ -66,11 +73,18 @@ public class StyleManager {
// chart title // chart title
chartTitleFont = theme.getChartTitleFont(); chartTitleFont = theme.getChartTitleFont();
isChartTitleVisible = theme.isChartTitleVisible(); isChartTitleVisible = theme.isChartTitleVisible();
// chart legend // legend
isChartLegendVisisble = theme.isChartLegendVisible(); isLegendVisible = theme.isLegendVisible();
chartLegendBackgroundColor = theme.getChartLegendBackgroundColor(); legendBackgroundColor = theme.getLegendBackgroundColor();
chartLegendFont = theme.getChartLegendFont(); legendFont = theme.getLegendFont();
chartLegendPadding = theme.getChartLegendPadding(); legendPadding = theme.getLegendPadding();
// axes
xAxisTitleVisible = theme.isXAxisTitleVisible();
yAxisTitleVisible = theme.isYAxisTitleVisible();
axisTitleFont = theme.getAxisTitleFont();
xAxisTicksVisible = theme.isXAxisTicksVisible();
yAxisTicksVisible = theme.isYAxisTicksVisible();
axisTicksFont = theme.getAxisTicksFont();
} }
/** /**
...@@ -153,9 +167,9 @@ public class StyleManager { ...@@ -153,9 +167,9 @@ public class StyleManager {
* *
* @param font * @param font
*/ */
public void setTitleFont(Font font) { public void setChartTitleFont(Font chartTitleFont) {
this.chartTitleFont = font; this.chartTitleFont = chartTitleFont;
} }
public Font getChartTitleFont() { public Font getChartTitleFont() {
...@@ -185,14 +199,14 @@ public class StyleManager { ...@@ -185,14 +199,14 @@ public class StyleManager {
* *
* @param color * @param color
*/ */
public void setChartLegendBackgroundColor(Color color) { public void setLegendBackgroundColor(Color color) {
this.chartLegendBackgroundColor = color; this.legendBackgroundColor = color;
} }
public Color getChartLegendBackgroundColor() { public Color getLegendBackgroundColor() {
return chartLegendBackgroundColor; return legendBackgroundColor;
} }
/** /**
...@@ -200,46 +214,162 @@ public class StyleManager { ...@@ -200,46 +214,162 @@ public class StyleManager {
* *
* @param font * @param font
*/ */
public void setChartLegendFont(Font font) { public void setLegendFont(Font font) {
this.chartLegendFont = font; this.legendFont = font;
} }
public Font getChartLegendFont() { public Font getLegendFont() {
return chartLegendFont; return legendFont;
} }
/** /**
* Set the chart legend visibility * Set the chart legend visibility
* *
* @param isChartLegendVisisble * @param isLegendVisible
*/ */
public void setChartLegendVisible(boolean isChartLegendVisisble) { public void setLegendVisible(boolean isLegendVisible) {
this.isChartLegendVisisble = isChartLegendVisisble; this.isLegendVisible = isLegendVisible;
} }
public boolean isChartLegendVisisble() { public boolean isLegendVisible() {
return isChartLegendVisisble; return isLegendVisible;
} }
/** /**
* Set the chart legend padding * Set the chart legend padding
* *
* @param chartLegendPadding * @param legendPadding
*/ */
public void setChartLegendPadding(int chartLegendPadding) { public void setLegendPadding(int legendPadding) {
this.chartLegendPadding = chartLegendPadding; this.legendPadding = legendPadding;
} }
public int getChartLegendPadding() { public int getLegendPadding() {
return chartLegendPadding; return legendPadding;
} }
// Chart Title /////////////////////////////// // Chart Axes ///////////////////////////////
/**
* Set the x-axis title visibility
*
* @param isVisible
*/
public void setxAxisTitleVisible(boolean xAxisTitleVisible) {
this.xAxisTitleVisible = xAxisTitleVisible;
}
public boolean isxAxisTitleVisible() {
return xAxisTitleVisible;
}
/**
* Set the y-axis title visibility
*
* @param isVisible
*/
public void setyAxisTitleVisible(boolean yAxisTitleVisible) {
this.yAxisTitleVisible = yAxisTitleVisible;
}
public boolean isyAxisTitleVisible() {
return yAxisTitleVisible;
}
/**
* Set the x- and y-axis titles visibility
*
* @param isVisible
*/
public void setAxisTitlesVisible(boolean isVisible) {
this.xAxisTitleVisible = isVisible;
this.yAxisTitleVisible = isVisible;
}
/**
* Set the x- and y-axis title font
*
* @param axisTitleFont
*/
public void setAxisTitleFont(Font axisTitleFont) {
this.axisTitleFont = axisTitleFont;
}
public Font getAxisTitleFont() {
return axisTitleFont;
}
/**
* Set the x-axis tick marks and labels visibility
*
* @param isVisible
*/
public void setxAxisTicksVisible(boolean xAxisTicksVisible) {
this.xAxisTicksVisible = xAxisTicksVisible;
}
public boolean isxAxisTicksVisible() {
return xAxisTicksVisible;
}
/**
* Set the y-axis tick marks and labels visibility
*
* @param isVisible
*/
public void setyAxisTicksVisible(boolean yAxisTicksVisible) {
this.yAxisTicksVisible = yAxisTicksVisible;
}
public boolean isyAxisTicksVisible() {
return yAxisTicksVisible;
}
/**
* Set the x- and y-axis tick marks and labels visibility
*
* @param isVisible
*/
public void setAxisTicksVisible(boolean isVisible) {
this.xAxisTicksVisible = isVisible;
this.yAxisTicksVisible = isVisible;
}
/**
* Set the x- and y-axis tick label font
*
* @param foxAxisTicksFontnt
*/
public void setAxisTicksFont(Font axisTicksFont) {
this.axisTicksFont = axisTicksFont;
}
public Font getAxisTicksFont() {
return axisTicksFont;
}
} }
...@@ -47,12 +47,26 @@ public interface Theme { ...@@ -47,12 +47,26 @@ public interface Theme {
// Chart Legend /////////////////////////////// // Chart Legend ///////////////////////////////
public Font getChartLegendFont(); public Font getLegendFont();
public boolean isChartLegendVisible(); public boolean isLegendVisible();
public Color getChartLegendBackgroundColor(); public Color getLegendBackgroundColor();
public int getChartLegendPadding(); public int getLegendPadding();
// Chart Axes ///////////////////////////////
public boolean isXAxisTitleVisible();
public boolean isYAxisTitleVisible();
public Font getAxisTitleFont();
public boolean isXAxisTicksVisible();
public boolean isYAxisTicksVisible();
public Font getAxisTicksFont();
} }
...@@ -74,27 +74,63 @@ public class XChartTheme implements Theme { ...@@ -74,27 +74,63 @@ public class XChartTheme implements Theme {
// Chart Legend /////////////////////////////// // Chart Legend ///////////////////////////////
@Override @Override
public Font getChartLegendFont() { public Font getLegendFont() {
return new Font(Font.SANS_SERIF, Font.PLAIN, 11); return new Font(Font.SANS_SERIF, Font.PLAIN, 11);
} }
@Override @Override
public boolean isChartLegendVisible() { public boolean isLegendVisible() {
return true; return true;
} }
@Override @Override
public Color getChartLegendBackgroundColor() { public Color getLegendBackgroundColor() {
return ChartColor.getAWTColor(ChartColor.LIGHT_GREY); return ChartColor.getAWTColor(ChartColor.LIGHT_GREY);
} }
@Override @Override
public int getChartLegendPadding() { public int getLegendPadding() {
return 10; return 10;
} }
@Override
public boolean isXAxisTitleVisible() {
return false;
}
@Override
public boolean isYAxisTitleVisible() {
return false;
}
@Override
public Font getAxisTitleFont() {
return new Font(Font.SANS_SERIF, Font.BOLD, 12);
}
@Override
public boolean isXAxisTicksVisible() {
return true;
}
@Override
public boolean isYAxisTicksVisible() {
return true;
}
@Override
public Font getAxisTicksFont() {
return new Font(Font.SANS_SERIF, Font.BOLD, 12);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment