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

chart legend style refactor

parent b51a75fc
Branches
No related tags found
No related merge requests found
Showing
with 140 additions and 74 deletions
......@@ -76,7 +76,7 @@ public class Example1 implements ExampleChart {
// Customize Chart
chart.setTitle("Example1");
chart.getStyleManager().setChartTitleVisible(false);
chart.setLegendVisible(false);
chart.getStyleManager().setChartLegendVisible(false);
// Series 1
Series series1 = chart.addSeries("data", xData, yData);
......
......@@ -46,7 +46,7 @@ public class Example10 implements ExampleChart {
chart.setTitle("Example10");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.setLegendVisible(false);
chart.getStyleManager().setChartLegendVisible(false);
for (int i = 0; i < 200; i++) {
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 {
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
chart.setLegendVisible(false);
chart.getStyleManager().setChartLegendVisible(false);
// Series 1
Series series1 = chart.addSeries("y=sin(x)", xData1, yData1);
......
......@@ -58,7 +58,7 @@ public class Example8 implements ExampleChart {
// Customize Chart
chart.getStyleManager().setChartTitleVisible(false);
chart.setLegendVisible(false);
chart.getStyleManager().setChartLegendVisible(false);
chart.setAxisTitlesVisible(false);
// Series 1
......
......@@ -77,11 +77,11 @@ public class Example9 implements ExampleChart {
chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY));
chart.setGridLinesColor(new Color(255, 255, 255));
chart.getStyleManager().setChartBackgroundColor(Color.WHITE);
chart.setLegendBackgroundColor(Color.PINK);
chart.getStyleManager().setChartLegendBackgroundColor(Color.PINK);
chart.getStyleManager().setChartBordersColor(Color.GREEN);
chart.getStyleManager().setChartFontColor(Color.MAGENTA);
chart.getStyleManager().setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24));
chart.setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18));
chart.getStyleManager().setChartLegendFont(new Font(Font.SERIF, Font.PLAIN, 18));
chart.setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18));
chart.setTickLabelFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18));
chart.setTickLabelFont(new Font(Font.SERIF, Font.PLAIN, 11));
......
......@@ -26,8 +26,8 @@ import java.util.Locale;
import java.util.TimeZone;
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.Legend;
import com.xeiam.xchart.internal.chartpart.Plot;
import com.xeiam.xchart.style.Series;
import com.xeiam.xchart.style.StyleManager;
......@@ -49,7 +49,7 @@ public class Chart {
// Chart Parts
public ChartTitle chartTitle = new ChartTitle(this);
public Legend chartLegend = new Legend(this);
public ChartLegend chartLegend = new ChartLegend(this);
public AxisPair axisPair = new AxisPair(this);
protected Plot plot = new Plot(this);
......@@ -77,7 +77,7 @@ public class Chart {
setTitle(chartBuilder.title);
setXAxisTitle(chartBuilder.xAxisTitle);
setYAxisTitle(chartBuilder.yAxisTitle);
setLegendVisible(chartBuilder.isLegendVisible);
styleManager.setChartLegendVisible(chartBuilder.isLegendVisible);
}
/**
......@@ -280,16 +280,6 @@ public class Chart {
this.axisPair.yAxis.getAxisTitle().setVisible(isVisible);
}
/**
* Set the chart legend visibility
*
* @param isVisible
*/
public void setLegendVisible(boolean isVisible) {
this.chartLegend.setVisible(isVisible);
}
/**
* Set the x- and y-axis tick marks and labels visibility
*
......@@ -351,26 +341,6 @@ public class Chart {
this.plot.plotSurface.setGridLinesColor(color);
}
/**
* Set the chart legend color
*
* @param color
*/
public void setLegendBackgroundColor(Color color) {
this.chartLegend.backgroundColor = color;
}
/**
* Set the chart legend font
*
* @param font
*/
public void setLegendFont(Font font) {
this.chartLegend.font = font;
}
/**
* Set the x- and y-axis title font
*
......
......@@ -85,7 +85,7 @@ public final class QuickChart {
if (seriesNames != null) {
series = chart.addSeries(seriesNames[i], xData, yData[i]);
} else {
chart.setLegendVisible(false);
chart.getStyleManager().setChartLegendVisible(false);
series = chart.addSeries(" " + i, xData, yData[i]);
}
series.setMarker(SeriesMarker.NONE);
......
......@@ -206,7 +206,8 @@ public class Axis implements IChartPart {
int xOffset = (int) (axisPair.yAxis.getBounds().getWidth() + (axisPair.yAxis.axisTick.isVisible ? Plot.PLOT_PADDING : 0) + Chart.CHART_PADDING);
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.chartLegend.isVisible ? 3 : 2) * Chart.CHART_PADDING);
int width = (int) (axisPair.chart.width - axisPair.yAxis.getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (axisPair.chart.getStyleManager().isChartLegendVisisble() ? 3 : 2)
* Chart.CHART_PADDING);
int height = this.getSizeHint();
Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height);
this.paintZone = xAxisRectangle;
......
......@@ -15,8 +15,6 @@
*/
package com.xeiam.xchart.internal.chartpart;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
......@@ -27,28 +25,16 @@ import com.xeiam.xchart.Chart;
import com.xeiam.xchart.internal.interfaces.IChartPart;
import com.xeiam.xchart.internal.interfaces.IHideable;
import com.xeiam.xchart.internal.markers.Marker;
import com.xeiam.xchart.style.ChartColor;
import com.xeiam.xchart.style.Series;
/**
* @author timmolter
*/
public class Legend implements IChartPart, IHideable {
private static final int LEGEND_PADDING = 10;
public class ChartLegend implements IChartPart, IHideable {
/** parent */
private final Chart chart;
/** the visibility state of legend */
protected boolean isVisible = true; // default to true
/** the font */
public Font font;
/** the background color */
public Color backgroundColor;
/** the bounds */
private Rectangle bounds;
......@@ -57,26 +43,23 @@ public class Legend implements IChartPart, IHideable {
*
* @param chart
*/
public Legend(Chart chart) {
public ChartLegend(Chart chart) {
this.chart = chart;
backgroundColor = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color
font = new Font(Font.SANS_SERIF, Font.PLAIN, 11); // default font
}
@Override
public void setVisible(boolean isVisible) {
this.isVisible = isVisible;
}
@Override
public void paint(Graphics2D g) {
bounds = new Rectangle();
g.setFont(font);
g.setFont(chart.getStyleManager().getChartLegendFont());
if (isVisible) {
if (chart.getStyleManager().isChartLegendVisisble()) {
Map<Integer, Series> seriesMap = chart.axisPair.seriesMap;
......@@ -86,7 +69,7 @@ public class Legend implements IChartPart, IHideable {
for (Integer seriesId : seriesMap.keySet()) {
Series series = seriesMap.get(seriesId);
TextLayout textLayout = new TextLayout(series.name, font, new FontRenderContext(null, true, false));
TextLayout textLayout = new TextLayout(series.name, chart.getStyleManager().getChartLegendFont(), new FontRenderContext(null, true, false));
Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0);
// System.out.println(rectangle);
if (rectangle.getWidth() > legendTextContentMaxWidth) {
......@@ -100,25 +83,25 @@ public class Legend implements IChartPart, IHideable {
// determine legend content height
int legendContentHeight = 0;
int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE);
legendContentHeight = maxContentHeight * seriesMap.size() + LEGEND_PADDING * (seriesMap.size() - 1);
legendContentHeight = maxContentHeight * seriesMap.size() + chart.getStyleManager().getChartLegendPadding() * (seriesMap.size() - 1);
// determine legend content width
int legendContentWidth = (int) (3.0 * Marker.SIZE + LEGEND_PADDING + legendTextContentMaxWidth);
int legendContentWidth = (int) (3.0 * Marker.SIZE + chart.getStyleManager().getChartLegendPadding() + legendTextContentMaxWidth);
// Draw Legend Box
int legendBoxWidth = legendContentWidth + 2 * LEGEND_PADDING;
int legendBoxHeight = legendContentHeight + 2 * LEGEND_PADDING;
int legendBoxWidth = legendContentWidth + 2 * chart.getStyleManager().getChartLegendPadding();
int legendBoxHeight = legendContentHeight + 2 * chart.getStyleManager().getChartLegendPadding();
int xOffset = chart.width - legendBoxWidth - Chart.CHART_PADDING;
int yOffset = (int) ((chart.height - legendBoxHeight) / 2.0 + chart.chartTitle.getBounds().getY() + chart.chartTitle.getBounds().getHeight());
g.setColor(chart.getStyleManager().getChartBordersColor());
g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight);
g.setColor(backgroundColor);
g.setColor(chart.getStyleManager().getChartLegendBackgroundColor());
g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1);
// Draw legend content inside legend box
int startx = xOffset + LEGEND_PADDING;
int starty = yOffset + LEGEND_PADDING;
int startx = xOffset + chart.getStyleManager().getChartLegendPadding();
int starty = yOffset + chart.getStyleManager().getChartLegendPadding();
for (Integer seriesId : seriesMap.keySet()) {
Series series = seriesMap.get(seriesId);
// paint line
......@@ -135,9 +118,9 @@ public class Legend implements IChartPart, IHideable {
// paint series name
g.setColor(chart.getStyleManager().getChartFontColor());
TextLayout layout = new TextLayout(series.name, font, new FontRenderContext(null, true, false));
layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + LEGEND_PADDING), (starty + Marker.SIZE));
starty = starty + legendTextContentMaxHeight + LEGEND_PADDING;
TextLayout layout = new TextLayout(series.name, chart.getStyleManager().getChartLegendFont(), new FontRenderContext(null, true, false));
layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + chart.getStyleManager().getChartLegendPadding()), (starty + Marker.SIZE));
starty = starty + legendTextContentMaxHeight + chart.getStyleManager().getChartLegendPadding();
}
// bounds
......
......@@ -40,9 +40,13 @@ public class StyleManager {
public Color chartFontColor;
private Font chartTitleFont;
private boolean isChartTitleVisible;
private boolean isChartLegendVisisble;
private Color chartLegendBackgroundColor;
private Font chartLegendFont;
private int chartLegendPadding;
/**
* Constructor
*/
......@@ -53,11 +57,18 @@ public class StyleManager {
private void setAllStyles() {
// chart
chartBackgroundColor = theme.getChartBackgroundColor();
chartBordersColor = theme.getChartBordersColor();
chartFontColor = theme.getChartFontColor();
// chart title
chartTitleFont = theme.getChartTitleFont();
isChartTitleVisible = theme.isChartTitleVisible();
// chart legend
isChartLegendVisisble = theme.isChartLegendVisible();
chartLegendBackgroundColor = theme.getChartLegendBackgroundColor();
chartLegendFont = theme.getChartLegendFont();
chartLegendPadding = theme.getChartLegendPadding();
}
/**
......@@ -151,6 +162,67 @@ public class StyleManager {
}
// Chart Legend ///////////////////////////////
/**
* Set the chart legend color
*
* @param color
*/
public void setChartLegendBackgroundColor(Color color) {
this.chartLegendBackgroundColor = color;
}
public Color getChartLegendBackgroundColor() {
return chartLegendBackgroundColor;
}
/**
* Set the chart legend font
*
* @param font
*/
public void setChartLegendFont(Font font) {
this.chartLegendFont = font;
}
public Font getChartLegendFont() {
return chartLegendFont;
}
/**
* Set the chart legend visibility
*
* @param isChartLegendVisisble
*/
public void setChartLegendVisible(boolean isChartLegendVisisble) {
this.isChartLegendVisisble = isChartLegendVisisble;
}
public boolean isChartLegendVisisble() {
return isChartLegendVisisble;
}
/**
* Set the chart legend padding
*
* @param chartLegendPadding
*/
public void setChartLegendPadding(int chartLegendPadding) {
this.chartLegendPadding = chartLegendPadding;
}
public int getChartLegendPadding() {
return chartLegendPadding;
}
// Chart Title ///////////////////////////////
}
......@@ -43,4 +43,14 @@ public interface Theme {
public boolean isChartTitleVisible();
// Chart Legend ///////////////////////////////
public Font getChartLegendFont();
public boolean isChartLegendVisible();
public Color getChartLegendBackgroundColor();
public int getChartLegendPadding();
}
......@@ -31,6 +31,8 @@ import com.xeiam.xchart.style.ChartColor;
*/
public class XChartTheme implements Theme {
// Chart Style ///////////////////////////////
@Override
public Color getChartBackgroundColor() {
......@@ -49,6 +51,8 @@ public class XChartTheme implements Theme {
return ChartColor.getAWTColor(ChartColor.BLACK);
}
// Chart Title ///////////////////////////////
@Override
public Font getChartTitleFont() {
......@@ -61,4 +65,30 @@ public class XChartTheme implements Theme {
return false;
}
// Chart Legend ///////////////////////////////
@Override
public Font getChartLegendFont() {
return new Font(Font.SANS_SERIF, Font.PLAIN, 11);
}
@Override
public boolean isChartLegendVisible() {
return true;
}
@Override
public Color getChartLegendBackgroundColor() {
return ChartColor.getAWTColor(ChartColor.LIGHT_GREY);
}
@Override
public int getChartLegendPadding() {
return 10;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment