From b48f474a272be140fefdb93674944edd85526d33 Mon Sep 17 00:00:00 2001 From: timmolter <tim.molter@gmail.com> Date: Thu, 20 Sep 2012 10:38:22 +0200 Subject: [PATCH] added Locale setting capability, renamed several Chart public methods, renamed Legend class --- src/main/java/com/xeiam/xchart/AxisTick.java | 39 +++++++++---- src/main/java/com/xeiam/xchart/Chart.java | 55 ++++++++++++------- .../xchart/{ChartLegend.java => Legend.java} | 4 +- .../java/com/xeiam/xchart/QuickChart.java | 4 +- .../com/xeiam/xchart/example/Example1.java | 2 +- .../com/xeiam/xchart/example/Example2.java | 4 +- .../com/xeiam/xchart/example/Example3.java | 2 +- .../com/xeiam/xchart/example/Example5.java | 2 +- .../com/xeiam/xchart/example/Example6.java | 2 +- .../com/xeiam/xchart/example/Example7.java | 2 +- .../com/xeiam/xchart/example/Example8.java | 4 +- .../com/xeiam/xchart/example/Example9.java | 26 +++++---- 12 files changed, 89 insertions(+), 57 deletions(-) rename src/main/java/com/xeiam/xchart/{ChartLegend.java => Legend.java} (98%) diff --git a/src/main/java/com/xeiam/xchart/AxisTick.java b/src/main/java/com/xeiam/xchart/AxisTick.java index d9b45f47..76a09f5b 100644 --- a/src/main/java/com/xeiam/xchart/AxisTick.java +++ b/src/main/java/com/xeiam/xchart/AxisTick.java @@ -19,10 +19,11 @@ import java.awt.Graphics2D; import java.awt.Rectangle; import java.math.BigDecimal; import java.text.DecimalFormat; -import java.text.Format; +import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import com.xeiam.xchart.Axis.AxisType; import com.xeiam.xchart.interfaces.IChartPart; @@ -56,14 +57,12 @@ public class AxisTick implements IChartPart, IHideable { private int workingSpace; - /** the normal format for tick labels */ - protected Format normalFormat; + /** the Locale for Date tick labels */ + protected Locale locale; - /** the scientific format for tick labels */ - protected Format scientificFormat; - - /** the format for Date tick labels */ - protected SimpleDateFormat simpleDateformat; + protected String normalDecimalPattern; + protected String scientificDecimalPattern; + protected String datePattern; /** the bounds */ private Rectangle bounds; @@ -82,9 +81,11 @@ public class AxisTick implements IChartPart, IHideable { axisTickLabels = new AxisTickLabels(this); axisTickMarks = new AxisTickMarks(this); - normalFormat = new DecimalFormat("#.###"); - scientificFormat = new DecimalFormat("0.###E0"); - simpleDateformat = new SimpleDateFormat("MM-dd"); + // formatting + locale = Locale.getDefault(); + normalDecimalPattern = "#.###"; + scientificDecimalPattern = "0.###E0"; + datePattern = "HHmmss"; } @@ -244,13 +245,29 @@ public class AxisTick implements IChartPart, IHideable { private String format(BigDecimal value) { if (axis.axisType == AxisType.NUMBER) { + + NumberFormat nf = NumberFormat.getNumberInstance(locale); + if (Math.abs(value.doubleValue()) <= 9999 && Math.abs(value.doubleValue()) > .0001 || value.doubleValue() == 0) { + + DecimalFormat normalFormat = (DecimalFormat) nf; + normalFormat.applyPattern(normalDecimalPattern); return normalFormat.format(value.doubleValue()); + } else { + + DecimalFormat scientificFormat = (DecimalFormat) nf; + scientificFormat.applyPattern(scientificDecimalPattern); return scientificFormat.format(value.doubleValue()); + } } else { + + // TODO set this more intelligently + SimpleDateFormat simpleDateformat = new SimpleDateFormat(datePattern, locale); + simpleDateformat.applyPattern(datePattern); return simpleDateformat.format(value.longValueExact()); + } } diff --git a/src/main/java/com/xeiam/xchart/Chart.java b/src/main/java/com/xeiam/xchart/Chart.java index 096c8c89..580105ef 100644 --- a/src/main/java/com/xeiam/xchart/Chart.java +++ b/src/main/java/com/xeiam/xchart/Chart.java @@ -19,11 +19,10 @@ import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.RenderingHints; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.Locale; import com.xeiam.xchart.series.Series; import com.xeiam.xchart.series.SeriesColor; @@ -44,7 +43,7 @@ public class Chart { protected final static int CHART_PADDING = 10; protected ChartTitle chartTitle = new ChartTitle(this); - protected ChartLegend chartLegend = new ChartLegend(this); + protected Legend chartLegend = new Legend(this); protected AxisPair axisPair = new AxisPair(this); protected Plot plot = new Plot(this); @@ -183,7 +182,7 @@ public class Chart { return axisPair.addSeries(seriesName, xDataNumber, yDataNumber, errorBarDataNumber); } - public void setChartTitle(String title) { + public void setTitle(String title) { this.chartTitle.setText(title); } @@ -200,7 +199,7 @@ public class Chart { // ChartPart visibility //////////////////////////////// - public void setChartTitleVisible(boolean isVisible) { + public void setTitleVisible(boolean isVisible) { this.chartTitle.setVisible(isVisible); } @@ -221,7 +220,7 @@ public class Chart { this.axisPair.yAxis.getAxisTitle().setVisible(isVisible); } - public void setChartLegendVisible(boolean isVisible) { + public void setLegendVisible(boolean isVisible) { this.chartLegend.setVisible(isVisible); } @@ -242,47 +241,52 @@ public class Chart { this.axisPair.yAxis.axisTick.setVisible(isVisible); } - public void setChartGridlinesVisible(boolean isVisible) { + public void setGridlinesVisible(boolean isVisible) { this.plot.plotSurface.setVisible(isVisible); } - public void setChartBackgroundColor(Color color) { + public void setBackgroundColor(Color color) { this.backgroundColor = color; } - public void setChartForegroundColor(Color color) { + public void setForegroundColor(Color color) { this.plot.plotSurface.setForegroundColor(color); } - public void setChartGridLinesColor(Color color) { + public void setGridLinesColor(Color color) { this.plot.plotSurface.setGridLinesColor(color); } - public void setChartLegendBackgroundColor(Color color) { + public void setLegendBackgroundColor(Color color) { this.chartLegend.backgroundColor = color; } - public void setChartBordersColor(Color color) { + /** + * Sets the color of the plot border, legend border, tick marks, and error bars + * + * @param color + */ + public void setLinesColor(Color color) { this.bordersColor = color; } - public void setChartFontColor(Color color) { + public void setFontColor(Color color) { this.fontColor = color; } - public void setChartTitleFont(Font font) { + public void setTitleFont(Font font) { this.chartTitle.font = font; } - public void setChartLegendFont(Font font) { + public void setLegendFont(Font font) { this.chartLegend.font = font; } @@ -293,7 +297,7 @@ public class Chart { this.axisPair.yAxis.axisTitle.font = font; } - public void setChartTickLabelsFont(Font font) { + public void setTickLabelsFont(Font font) { this.axisPair.xAxis.axisTick.axisTickLabels.font = font; this.axisPair.yAxis.axisTick.axisTickLabels.font = font; @@ -304,7 +308,7 @@ public class Chart { */ public void setDateFormatter(String pattern) { - this.axisPair.xAxis.axisTick.simpleDateformat = new SimpleDateFormat(pattern); + this.axisPair.xAxis.axisTick.datePattern = pattern; } /** @@ -312,8 +316,8 @@ public class Chart { */ public void setDecmialFormatter(String pattern) { - this.axisPair.xAxis.axisTick.normalFormat = new DecimalFormat(pattern); - this.axisPair.yAxis.axisTick.normalFormat = new DecimalFormat(pattern); + this.axisPair.xAxis.axisTick.normalDecimalPattern = pattern; + this.axisPair.yAxis.axisTick.normalDecimalPattern = pattern; } /** @@ -321,8 +325,17 @@ public class Chart { */ public void setDecmialScientificFormatter(String pattern) { - this.axisPair.xAxis.axisTick.scientificFormat = new DecimalFormat(pattern); - this.axisPair.yAxis.axisTick.scientificFormat = new DecimalFormat(pattern); + this.axisPair.xAxis.axisTick.scientificDecimalPattern = pattern; + this.axisPair.yAxis.axisTick.scientificDecimalPattern = pattern; + } + + /** + * @param locale - the locale to use when drawing the chart + */ + public void setLocale(Locale locale) { + + this.axisPair.xAxis.axisTick.locale = locale; + this.axisPair.yAxis.axisTick.locale = locale; } } diff --git a/src/main/java/com/xeiam/xchart/ChartLegend.java b/src/main/java/com/xeiam/xchart/Legend.java similarity index 98% rename from src/main/java/com/xeiam/xchart/ChartLegend.java rename to src/main/java/com/xeiam/xchart/Legend.java index fd58e857..937cc616 100644 --- a/src/main/java/com/xeiam/xchart/ChartLegend.java +++ b/src/main/java/com/xeiam/xchart/Legend.java @@ -30,7 +30,7 @@ import com.xeiam.xchart.series.markers.Marker; /** * @author timmolter */ -public class ChartLegend implements IHideable { +public class Legend implements IHideable { private static final int LEGEND_PADDING = 10; @@ -54,7 +54,7 @@ public class ChartLegend implements IHideable { * * @param chart */ - public ChartLegend(Chart chart) { + public Legend(Chart chart) { this.chart = chart; backgroundColor = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color diff --git a/src/main/java/com/xeiam/xchart/QuickChart.java b/src/main/java/com/xeiam/xchart/QuickChart.java index e9adcc8b..97522a41 100644 --- a/src/main/java/com/xeiam/xchart/QuickChart.java +++ b/src/main/java/com/xeiam/xchart/QuickChart.java @@ -59,7 +59,7 @@ public class QuickChart { Chart chart = new Chart(400, 280); // Customize Chart - chart.setChartTitle(chartTitle); + chart.setTitle(chartTitle); chart.setXAxisTitle(xTitle); chart.setYAxisTitle(yTitle); @@ -69,7 +69,7 @@ public class QuickChart { if (seriesNames != null) { series = chart.addSeries(seriesNames[i], xData, yData[i]); } else { - chart.setChartLegendVisible(false); + chart.setLegendVisible(false); series = chart.addSeries(" " + i, xData, yData[i]); } series.setMarker(SeriesMarker.NONE); diff --git a/src/test/java/com/xeiam/xchart/example/Example1.java b/src/test/java/com/xeiam/xchart/example/Example1.java index 7615a750..e71b16b7 100644 --- a/src/test/java/com/xeiam/xchart/example/Example1.java +++ b/src/test/java/com/xeiam/xchart/example/Example1.java @@ -35,7 +35,7 @@ public class Example1 { // Create Chart Chart chart = new Chart(500, 400); - chart.setChartTitle("Sample Chart"); + chart.setTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); chart.addSeries("y(x)", xData, yData); diff --git a/src/test/java/com/xeiam/xchart/example/Example2.java b/src/test/java/com/xeiam/xchart/example/Example2.java index 1341b165..94075eb3 100644 --- a/src/test/java/com/xeiam/xchart/example/Example2.java +++ b/src/test/java/com/xeiam/xchart/example/Example2.java @@ -48,8 +48,8 @@ public class Example2 { Chart chart = new Chart(440, 300); // Customize Chart - chart.setChartTitleVisible(false); - chart.setChartLegendVisible(false); + chart.setTitleVisible(false); + chart.setLegendVisible(false); chart.setAxisTitlesVisible(false); // Series 1 diff --git a/src/test/java/com/xeiam/xchart/example/Example3.java b/src/test/java/com/xeiam/xchart/example/Example3.java index a63b97ed..33af391f 100644 --- a/src/test/java/com/xeiam/xchart/example/Example3.java +++ b/src/test/java/com/xeiam/xchart/example/Example3.java @@ -45,7 +45,7 @@ public class Example3 { } // Customize Chart - chart.setChartTitle("Sample Chart"); + chart.setTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); diff --git a/src/test/java/com/xeiam/xchart/example/Example5.java b/src/test/java/com/xeiam/xchart/example/Example5.java index bf0c5ce8..8a359e7d 100644 --- a/src/test/java/com/xeiam/xchart/example/Example5.java +++ b/src/test/java/com/xeiam/xchart/example/Example5.java @@ -31,7 +31,7 @@ public class Example5 { Chart chart = new Chart(700, 500); // Customize Chart - chart.setChartTitle("Sample Chart"); + chart.setTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); diff --git a/src/test/java/com/xeiam/xchart/example/Example6.java b/src/test/java/com/xeiam/xchart/example/Example6.java index 05a2eb52..831289db 100644 --- a/src/test/java/com/xeiam/xchart/example/Example6.java +++ b/src/test/java/com/xeiam/xchart/example/Example6.java @@ -31,7 +31,7 @@ public class Example6 { Chart chart = new Chart(700, 500); // Customize Chart - chart.setChartTitle("Sample Chart"); + chart.setTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); diff --git a/src/test/java/com/xeiam/xchart/example/Example7.java b/src/test/java/com/xeiam/xchart/example/Example7.java index 1377b401..d8c72171 100644 --- a/src/test/java/com/xeiam/xchart/example/Example7.java +++ b/src/test/java/com/xeiam/xchart/example/Example7.java @@ -35,7 +35,7 @@ public class Example7 { // Create Chart Chart chart = new Chart(700, 500); - chart.setChartTitle("Sample Chart"); + chart.setTitle("Sample Chart"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); chart.addSeries("y(x)", xData, yData); diff --git a/src/test/java/com/xeiam/xchart/example/Example8.java b/src/test/java/com/xeiam/xchart/example/Example8.java index 72f8b15b..8f5330b1 100644 --- a/src/test/java/com/xeiam/xchart/example/Example8.java +++ b/src/test/java/com/xeiam/xchart/example/Example8.java @@ -49,8 +49,8 @@ public class Example8 { Chart chart = new Chart(600, 400); // Customize Chart - chart.setChartTitleVisible(false); - chart.setChartLegendVisible(false); + chart.setTitleVisible(false); + chart.setLegendVisible(false); chart.setAxisTitlesVisible(false); // Series 1 diff --git a/src/test/java/com/xeiam/xchart/example/Example9.java b/src/test/java/com/xeiam/xchart/example/Example9.java index d119bf81..8ffe4c11 100644 --- a/src/test/java/com/xeiam/xchart/example/Example9.java +++ b/src/test/java/com/xeiam/xchart/example/Example9.java @@ -23,6 +23,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; +import java.util.Locale; import com.xeiam.xchart.Chart; import com.xeiam.xchart.ChartColor; @@ -56,22 +57,23 @@ public class Example9 { } // Customize Chart - chart.setChartTitle("Sample Chart with Date X-Axis"); + chart.setTitle("Sample Chart with Date X-Axis"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); - chart.setChartForegroundColor(ChartColor.getAWTColor(ChartColor.GREY)); - chart.setChartGridLinesColor(new Color(255, 255, 255)); - chart.setChartBackgroundColor(Color.WHITE); - chart.setChartLegendBackgroundColor(Color.PINK); - chart.setChartBordersColor(Color.GREEN); - chart.setChartFontColor(Color.MAGENTA); - chart.setChartTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24)); - chart.setChartLegendFont(new Font(Font.SERIF, Font.PLAIN, 18)); + chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY)); + chart.setGridLinesColor(new Color(255, 255, 255)); + chart.setBackgroundColor(Color.WHITE); + chart.setLegendBackgroundColor(Color.PINK); + chart.setLinesColor(Color.GREEN); + chart.setFontColor(Color.MAGENTA); + chart.setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24)); + chart.setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18)); chart.setAxisLabelsFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); - chart.setChartTickLabelsFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); - chart.setChartTickLabelsFont(new Font(Font.SERIF, Font.PLAIN, 11)); - chart.setDateFormatter("yyyy-MM-dd"); + chart.setTickLabelsFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); + chart.setTickLabelsFont(new Font(Font.SERIF, Font.PLAIN, 11)); + chart.setDateFormatter("dd-MMM"); chart.setDecmialFormatter("#.000"); + chart.setLocale(Locale.GERMAN); Series series = chart.addDateSeries("Fake Data", xData, yData); series.setLineColor(SeriesColor.BLUE); -- GitLab