diff --git a/src/main/java/com/xeiam/xchart/AxisTick.java b/src/main/java/com/xeiam/xchart/AxisTick.java index d9b45f476fe91102496e69ade703266ef0056142..76a09f5b531ad1708f7eea3758e34654ceef140f 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 096c8c8984ea5b10792d5d2e7242f39450c335ba..580105ef656747d92a7fa9437827df5564ca89f7 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 fd58e8572913ec906519a1e5ad4b603bfda9268d..937cc6161c77ec3223a6997f0a8fe4dc8d8feaad 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 e9adcc8b8c171aab1913fabc160bc248888d2519..97522a41c305d8b87ac914d49328c8c41e38a19a 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 7615a75069faddd112ff4584804ae5a96d49e170..e71b16b7ba80f1d093332badf9ab7c866957780f 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 1341b1656d839c5b4872d6445c41889ea45fca6c..94075eb392ec1d3cf493f3c927d7dc3ea63042d0 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 a63b97ed0f67e0a7402e9c02e68b6a1a8cc7b8ce..33af391f77c25c9167637e4f9a4841368e5be609 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 bf0c5ce82bef18b3d708a84815e12d5637f392f6..8a359e7d7ba182b3cafb50dd4c6559807dc262fd 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 05a2eb52107bc3f205417544b6a5ad18325a43bd..831289dbafc8a438a068cc4f5f6aecaf4163616c 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 1377b4018de22abb39e3dd37ff40748cf30f1d19..d8c72171fb8bc6029f4f904e8d3646256647a932 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 72f8b15b49668d54610e9ca43e38223c06aa86e3..8f5330b17b6c97a488e9094bf37c4962bbb8eed7 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 d119bf81b9e7662bcba8a87aa3ee467eee682e17..8ffe4c116393899ac2c8dff0f29b77a62f236ae4 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);