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

moved all formatting code to a ValueFormatter class.

parent 5bc523fa
No related branches found
No related tags found
No related merge requests found
Showing
with 117 additions and 152 deletions
......@@ -74,7 +74,7 @@ public class Example1 implements ExampleChart {
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitle("Example1");
chart.setChartTitle("Example1");
chart.getStyleManager().setChartTitleVisible(false);
chart.getStyleManager().setLegendVisible(false);
......
......@@ -43,7 +43,7 @@ public class Example10 implements ExampleChart {
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitle("Example10");
chart.setChartTitle("Example10");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.getStyleManager().setLegendVisible(false);
......
......@@ -16,7 +16,7 @@
package com.xeiam.xchart.demo.charts;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.SwingWrapper;
......@@ -44,8 +44,8 @@ public class Example2 implements ExampleChart {
// generates sine data
int size = 30;
Collection<Number> xData1 = new ArrayList<Number>();
Collection<Number> yData1 = new ArrayList<Number>();
List<Number> xData1 = new ArrayList<Number>();
List<Number> yData1 = new ArrayList<Number>();
for (int i = 0; i <= size; i++) {
double radians = (Math.PI / (size / 2) * i);
xData1.add(i - size / 2);
......
......@@ -53,7 +53,7 @@ public class Example3 implements ExampleChart {
}
// Customize Chart
chart.setTitle("Example3");
chart.setChartTitle("Example3");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
......
......@@ -65,10 +65,10 @@ public class Example4 implements ExampleChart {
}
// Customize Chart
chart.setTitle("Example4");
chart.setChartTitle("Example4");
chart.setXAxisTitle("time of day");
chart.setYAxisTitle("gigawatts");
chart.setTimezone(TimeZone.getTimeZone("UTC"));
chart.getValueFormatter().setTimezone(TimeZone.getTimeZone("UTC"));
Series series = chart.addDateSeries("value", xData, yData);
......
......@@ -39,7 +39,7 @@ public class Example5 implements ExampleChart {
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitle("Example5");
chart.setChartTitle("Example5");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
......
......@@ -39,7 +39,7 @@ public class Example6 implements ExampleChart {
Chart chart = new Chart(800, 600);
// Customize Chart
chart.setTitle("Example6");
chart.setChartTitle("Example6");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
......
......@@ -43,7 +43,7 @@ public class Example7 implements ExampleChart {
// Create Chart
Chart chart = new Chart(800, 600);
chart.setTitle("Example7");
chart.setChartTitle("Example7");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.addSeries("y(x)", xData, yData);
......
......@@ -71,11 +71,11 @@ public class Example9 implements ExampleChart {
}
// Customize Chart
chart.setTitle("Sample Chart Extensive Cusomization");
chart.setChartTitle("Sample Chart Extensive Cusomization");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY));
chart.setGridLinesColor(new Color(255, 255, 255));
chart.getStyleManager().setPlotBackgroundColor(ChartColor.getAWTColor(ChartColor.GREY));
chart.getStyleManager().setPlotGridLinesColor(new Color(255, 255, 255));
chart.getStyleManager().setChartBackgroundColor(Color.WHITE);
chart.getStyleManager().setLegendBackgroundColor(Color.PINK);
chart.getStyleManager().setChartBordersColor(Color.GREEN);
......@@ -84,9 +84,9 @@ public class Example9 implements ExampleChart {
chart.getStyleManager().setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18));
chart.getStyleManager().setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18));
chart.getStyleManager().setAxisTicksFont(new Font(Font.SERIF, Font.PLAIN, 11));
chart.setDateFormatter("dd-MMM");
chart.setDecmialFormatter("#.000");
chart.setLocale(Locale.GERMAN);
chart.getValueFormatter().setDatePattern("dd-MMM");
chart.getValueFormatter().setNormalDecimalPattern("#.000");
chart.getValueFormatter().setLocale(Locale.GERMAN);
Series series = chart.addDateSeries("Fake Data", xData, yData);
series.setLineColor(SeriesColor.BLUE);
......
......@@ -31,7 +31,7 @@ public class Example1 {
// Create Chart
Chart chart = new Chart(500, 400);
chart.setTitle("Sample Chart");
chart.setChartTitle("Sample Chart");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
chart.addSeries("y(x)", null, yData);
......
......@@ -15,14 +15,11 @@
*/
package com.xeiam.xchart;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.xeiam.xchart.internal.chartpart.AxisPair;
import com.xeiam.xchart.internal.chartpart.ChartTitle;
......@@ -30,6 +27,7 @@ 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;
import com.xeiam.xchart.style.ValueFormatter;
import com.xeiam.xchart.style.theme.Theme;
/**
......@@ -43,6 +41,7 @@ public class Chart {
public int height;
private StyleManager styleManager = new StyleManager();
private ValueFormatter valueFormatter = new ValueFormatter();
// Chart Parts
public ChartTitle chartTitle = new ChartTitle(this);
......@@ -71,7 +70,7 @@ public class Chart {
public Chart(ChartBuilder chartBuilder) {
this(chartBuilder.width, chartBuilder.height);
setTitle(chartBuilder.title);
setChartTitle(chartBuilder.title);
setXAxisTitle(chartBuilder.xAxisTitle);
setYAxisTitle(chartBuilder.yAxisTitle);
styleManager.setLegendVisible(chartBuilder.isLegendVisible);
......@@ -219,7 +218,7 @@ public class Chart {
*
* @param title
*/
public void setTitle(String title) {
public void setChartTitle(String title) {
this.chartTitle.setText(title);
}
......@@ -236,7 +235,7 @@ public class Chart {
} else {
styleManager.setxAxisTitleVisible(true);
}
this.axisPair.xAxis.axisTitle.setText(title);
this.axisPair.xAxis.axisTitle.text = title;
}
/**
......@@ -251,81 +250,7 @@ public class Chart {
} else {
styleManager.setyAxisTitleVisible(true);
}
this.axisPair.yAxis.axisTitle.setText(title);
}
/**
* Set the chart foreground color - the part the series are drawn on
*
* @param color
*/
public void setForegroundColor(Color color) {
this.plot.plotSurface.setForegroundColor(color);
}
/**
* Set the chart grid lines color
*
* @param color
*/
public void setGridLinesColor(Color color) {
this.plot.plotSurface.setGridLinesColor(color);
}
/**
* Set the String formatter for Data x-axis
*
* @param pattern - the pattern describing the date and time format
*/
public void setDateFormatter(String pattern) {
this.axisPair.xAxis.axisTick.datePattern = pattern;
}
/**
* Set the decimal formatter for all tick labels
*
* @param pattern - the pattern describing the decimal format
*/
public void setDecmialFormatter(String pattern) {
this.axisPair.xAxis.axisTick.normalDecimalPattern = pattern;
this.axisPair.yAxis.axisTick.normalDecimalPattern = pattern;
}
/**
* Set the scientific notation formatter for all tick labels
*
* @param pattern - the pattern describing the scientific notation format
*/
public void setDecmialScientificFormatter(String pattern) {
this.axisPair.xAxis.axisTick.scientificDecimalPattern = pattern;
this.axisPair.yAxis.axisTick.scientificDecimalPattern = pattern;
}
/**
* Set the locale to use for rendering the chart
*
* @param locale - the locale to use when formatting Strings and dates for the axis tick labels
*/
public void setLocale(Locale locale) {
this.axisPair.xAxis.axisTick.locale = locale;
this.axisPair.yAxis.axisTick.locale = locale;
}
/**
* Set the timezone to use for formatting Date axis tick labels
*
* @param timezone the timezone to use when formatting date data
*/
public void setTimezone(TimeZone timezone) {
this.axisPair.xAxis.axisTick.timezone = timezone;
this.axisPair.yAxis.axisTick.timezone = timezone;
this.axisPair.yAxis.axisTitle.text = title;
}
/**
......@@ -349,4 +274,14 @@ public class Chart {
}
/**
* Gets the Chart's value formatter, which can be used to customize the formatting of numbers and dates
*
* @return
*/
public ValueFormatter getValueFormatter() {
return valueFormatter;
}
}
......@@ -75,7 +75,7 @@ public final class QuickChart {
Chart chart = new Chart(WIDTH, HEIGHT);
// Customize Chart
chart.setTitle(chartTitle);
chart.setChartTitle(chartTitle);
chart.setXAxisTitle(xTitle);
chart.setYAxisTitle(yTitle);
......@@ -111,7 +111,7 @@ public final class QuickChart {
Chart chart = new Chart(WIDTH, HEIGHT);
// Customize Chart
chart.setTitle(chartTitle);
chart.setChartTitle(chartTitle);
chart.setXAxisTitle(xTitle);
chart.setYAxisTitle(yTitle);
......
......@@ -142,7 +142,7 @@ public class Axis implements IChartPart {
// Axis title
double titleHeight = 0.0;
if (axisPair.chart.getStyleManager().isxAxisTitleVisible()) {
TextLayout textLayout = new TextLayout(axisTitle.getText(), axisTick.axis.axisPair.chart.getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false));
TextLayout textLayout = new TextLayout(axisTitle.text, axisTick.axis.axisPair.chart.getStyleManager().getAxisTitleFont(), new FontRenderContext(null, true, false));
Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0);
titleHeight = rectangle.getHeight() + axisTick.axis.axisPair.chart.getStyleManager().getAxisTitlePadding();
}
......
......@@ -26,7 +26,6 @@ import java.util.TimeZone;
import com.xeiam.xchart.internal.chartpart.Axis.AxisType;
import com.xeiam.xchart.internal.chartpart.Axis.Direction;
import com.xeiam.xchart.internal.interfaces.IChartPart;
import com.xeiam.xchart.internal.misc.AxisValueFormatterUtil;
/**
* An axis tick
......@@ -75,7 +74,8 @@ public class AxisTick implements IChartPart {
/**
* Constructor
*
* @param axis the axis
* @param axis
* @param isVisible
*/
protected AxisTick(Axis axis, boolean isVisible) {
......@@ -83,7 +83,6 @@ public class AxisTick implements IChartPart {
this.isVisible = isVisible;
axisTickLabels = new AxisTickLabels(this);
axisTickMarks = new AxisTickMarks(this);
}
@Override
......@@ -255,11 +254,11 @@ public class AxisTick implements IChartPart {
if (axis.axisType == AxisType.NUMBER) {
return AxisValueFormatterUtil.formatNumber(value, normalDecimalPattern, scientificDecimalPattern, locale);
return axis.axisPair.chart.getValueFormatter().formatNumber(value);
} else {
return AxisValueFormatterUtil.formatDateValue(value, axis.min, axis.max, datePattern, locale, timezone);
return axis.axisPair.chart.getValueFormatter().formatDateValue(value, axis.min, axis.max);
}
}
......
......@@ -76,11 +76,13 @@ public class AxisTickMarks implements IChartPart {
+ (int) (axisTick.axis.getPaintZone().getHeight() - tickLocation));
}
// Line
if (axisTick.axis.axisPair.chart.getStyleManager().isAxisTicksLineVisible()) {
g.drawLine(xOffset + axisTick.axis.axisPair.chart.getStyleManager().getAxisTickMarkLength(), yOffset, xOffset + axisTick.axis.axisPair.chart.getStyleManager().getAxisTickMarkLength(), yOffset
+ (int) axisTick.axis.getPaintZone().getHeight());
}
// bounds
bounds = new Rectangle(xOffset, yOffset, axisTick.axis.axisPair.chart.getStyleManager().getAxisTickMarkLength(), (int) axisTick.axis.getPaintZone().getHeight());
// g.setColor(Color.yellow);
......
......@@ -32,7 +32,7 @@ public class AxisTitle implements IChartPart {
private final Axis axis;
/** the title text */
protected String text = ""; // default to ""
public String text = ""; // default to ""
/** the bounds */
private Rectangle bounds;
......@@ -47,16 +47,6 @@ public class AxisTitle implements IChartPart {
this.axis = axis;
}
protected String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public Rectangle getBounds() {
......
......@@ -47,6 +47,11 @@ public class ChartTitle implements IChartPart {
this.chart = chart;
}
/**
* set the chart title's text
*
* @param text
*/
public void setText(String text) {
if (text.trim().equalsIgnoreCase("")) {
......
......@@ -18,6 +18,7 @@ package com.xeiam.xchart.internal.chartpart;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
......@@ -36,6 +37,8 @@ public class PlotContent implements IChartPart {
/** parent */
private Plot plot;
Stroke errorBarStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
/**
* Constructor
*
......@@ -145,8 +148,8 @@ public class PlotContent implements IChartPart {
// paint errorbar
if (errorBars != null) {
g.setColor(plot.chart.getStyleManager().getChartBordersColor());
g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
g.setColor(plot.chart.getStyleManager().getErrorBarsColor());
g.setStroke(errorBarStroke);
int bottom = (int) (-1 * bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue()));
int top = (int) (bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue()));
g.drawLine(xOffset, yOffset + bottom, xOffset, yOffset + top);
......
......@@ -16,13 +16,11 @@
package com.xeiam.xchart.internal.chartpart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;
import com.xeiam.xchart.internal.interfaces.IChartPart;
import com.xeiam.xchart.style.ChartColor;
/**
* @author timmolter
......@@ -32,14 +30,8 @@ public class PlotSurface implements IChartPart {
/** parent */
private Plot plot;
/** the gridLines Color */
private Color gridLinesColor;
/** the background color */
private Color foregroundColor;
/** the line style */
private BasicStroke stroke;
private BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f);
/**
* Constructor
......@@ -49,9 +41,6 @@ public class PlotSurface implements IChartPart {
protected PlotSurface(Plot plot) {
this.plot = plot;
gridLinesColor = ChartColor.getAWTColor(ChartColor.GREY); // default gridLines color
foregroundColor = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default foreground Color color
stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] { 3.0f, 3.0f }, 0.0f);
}
@Override
......@@ -65,9 +54,9 @@ public class PlotSurface implements IChartPart {
Rectangle bounds = plot.getBounds();
// paint foreground
// paint plot background
Rectangle backgroundRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight());
g.setColor(foregroundColor);
g.setColor(plot.chart.getStyleManager().getPlotBackgroundColor());
g.fill(backgroundRectangle);
Rectangle borderRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight());
g.setColor(plot.chart.getStyleManager().getChartBordersColor());
......@@ -75,13 +64,14 @@ public class PlotSurface implements IChartPart {
// paint grid lines
if (plot.chart.getStyleManager().isPlotGridLinesVisible()) {
// horizontal
List<Integer> yAxisTickLocations = plot.chart.axisPair.yAxis.axisTick.tickLocations;
for (int i = 0; i < yAxisTickLocations.size(); i++) {
int tickLocation = yAxisTickLocations.get(i);
g.setColor(gridLinesColor);
g.setColor(plot.chart.getStyleManager().getPlotGridLinesColor());
g.setStroke(stroke);
// System.out.println("bounds.getY()= " + bounds.getY());
g.drawLine((int) bounds.getX(), (int) (bounds.getY() + bounds.getHeight() - tickLocation), (int) (bounds.getX() + bounds.getWidth() - 2),
......@@ -94,7 +84,7 @@ public class PlotSurface implements IChartPart {
int tickLocation = xAxisTickLocations.get(i);
g.setColor(gridLinesColor);
g.setColor(plot.chart.getStyleManager().getPlotGridLinesColor());
g.setStroke(stroke);
g.drawLine((int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + 1), (int) (bounds.getX() + tickLocation - 1), (int) (bounds.getY() + bounds.getHeight() - 1));
......@@ -102,20 +92,4 @@ public class PlotSurface implements IChartPart {
}
}
/**
* @param gridLinesColor the gridLinesColor to set
*/
public void setGridLinesColor(Color gridLinesColor) {
this.gridLinesColor = gridLinesColor;
}
/**
* @param foregroundColor the foregroundColor to set
*/
public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
}
}
......@@ -66,6 +66,11 @@ public class StyleManager {
// Chart Plot Area ///////////////////////////////
private boolean isPlotGridLinesVisible;
private Color plotBackgroundColor;
private Color plotGridLinesColor;
// Error Bars ///////////////////////////////
private Color errorBarsColor;
/**
* Constructor
......@@ -108,6 +113,11 @@ public class StyleManager {
// Chart Plot Area ///////////////////////////////
isPlotGridLinesVisible = theme.isPlotGridLinesVisible();
plotBackgroundColor = theme.getPlotBackgroundColor();
plotGridLinesColor = theme.getPlotGridLinesColor();
// Error Bars ///////////////////////////////
errorBarsColor = theme.getErrorBarsColor();
}
/**
......@@ -486,4 +496,51 @@ public class StyleManager {
return isPlotGridLinesVisible;
}
/**
* set the plot area's background color
*
* @param plotBackgroundColor
*/
public void setPlotBackgroundColor(Color plotBackgroundColor) {
this.plotBackgroundColor = plotBackgroundColor;
}
public Color getPlotBackgroundColor() {
return plotBackgroundColor;
}
/**
* set the plot area's grid lines color
*
* @param plotGridLinesColor
*/
public void setPlotGridLinesColor(Color plotGridLinesColor) {
this.plotGridLinesColor = plotGridLinesColor;
}
public Color getPlotGridLinesColor() {
return plotGridLinesColor;
}
// Error Bars ///////////////////////////////
/**
* Sets the color of the error bars
*
* @param errorBarsColor
*/
public void setErrorBarsColor(Color errorBarsColor) {
this.errorBarsColor = errorBarsColor;
}
public Color getErrorBarsColor() {
return errorBarsColor;
}
}
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