diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/chart/TimelineChart.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/chart/TimelineChart.java index 273280fbcf..335cb9be62 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/chart/TimelineChart.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/chart/TimelineChart.java @@ -1,6 +1,5 @@ package name.abuchen.portfolio.ui.util.chart; -import java.text.DecimalFormat; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; @@ -31,6 +30,7 @@ import name.abuchen.portfolio.ui.UIConstants; import name.abuchen.portfolio.ui.util.Colors; +import name.abuchen.portfolio.ui.util.format.PercentNumberFormat; public class TimelineChart extends Chart // NOSONAR { @@ -120,7 +120,7 @@ public TimelineChart(Composite parent) IAxis y3Axis = getAxisSet().getYAxis(axisId3rd); y3Axis.getTitle().setVisible(false); y3Axis.getTick().setVisible(false); - y3Axis.getTick().setFormat(new DecimalFormat("+#.##%;-#.##%")); //$NON-NLS-1$ + y3Axis.getTick().setFormat(new PercentNumberFormat("+#.##%;-#.##%")); //$NON-NLS-1$ y3Axis.getGrid().setStyle(LineStyle.NONE); y3Axis.setPosition(Position.Primary); diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/format/PercentNumberFormat.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/format/PercentNumberFormat.java new file mode 100644 index 0000000000..6004fbec15 --- /dev/null +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/util/format/PercentNumberFormat.java @@ -0,0 +1,35 @@ +package name.abuchen.portfolio.ui.util.format; + +import java.text.DecimalFormat; +import java.text.FieldPosition; +import java.text.Format; +import java.text.ParsePosition; + +public class PercentNumberFormat extends Format +{ + private String decimalFormat; + private static final long serialVersionUID = 1L; + + public PercentNumberFormat(String decimalFormat) + { + this.decimalFormat = decimalFormat; + } + + @Override + public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) + { + if (!(obj instanceof Number)) + throw new IllegalArgumentException("object must be a subclass of Number"); //$NON-NLS-1$ + + toAppendTo.append(new DecimalFormat(decimalFormat).format(((Number) obj).doubleValue())); + + return toAppendTo; + } + + @Override + public Object parseObject(String source, ParsePosition pos) + { + pos.setErrorIndex(0); + return null; + } +} diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/PerformanceChartView.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/PerformanceChartView.java index 7f068ee738..abb6ce6691 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/PerformanceChartView.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/PerformanceChartView.java @@ -39,6 +39,7 @@ import name.abuchen.portfolio.ui.util.SimpleAction; import name.abuchen.portfolio.ui.util.chart.TimelineChart; import name.abuchen.portfolio.ui.util.chart.TimelineChartCSVExporter; +import name.abuchen.portfolio.ui.util.format.PercentNumberFormat; import name.abuchen.portfolio.ui.views.dataseries.DataSeries; import name.abuchen.portfolio.ui.views.dataseries.DataSeriesCache; import name.abuchen.portfolio.ui.views.dataseries.DataSeriesChartLegend; @@ -114,7 +115,7 @@ protected Composite createBody(Composite parent) chart = new TimelineChart(composite); chart.getTitle().setText(getTitle()); chart.getTitle().setVisible(false); - chart.getAxisSet().getYAxis(0).getTick().setFormat(new DecimalFormat("0.#%")); //$NON-NLS-1$ + chart.getAxisSet().getYAxis(0).getTick().setFormat(new PercentNumberFormat("0.#%")); //$NON-NLS-1$ chart.getToolTip().setDefaultValueFormat(new DecimalFormat(Values.Percent2.pattern())); chart.getToolTip().reverseLabels(true); diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/ReturnsVolatilityChartView.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/ReturnsVolatilityChartView.java index 80b305fa0b..0be5ac3c87 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/ReturnsVolatilityChartView.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/ReturnsVolatilityChartView.java @@ -39,6 +39,7 @@ import name.abuchen.portfolio.ui.util.SimpleAction; import name.abuchen.portfolio.ui.util.chart.ScatterChart; import name.abuchen.portfolio.ui.util.chart.ScatterChartCSVExporter; +import name.abuchen.portfolio.ui.util.format.PercentNumberFormat; import name.abuchen.portfolio.ui.views.dataseries.DataSeries; import name.abuchen.portfolio.ui.views.dataseries.DataSeriesCache; import name.abuchen.portfolio.ui.views.dataseries.DataSeriesChartLegend; @@ -192,11 +193,11 @@ protected Composite createBody(Composite parent) IAxis xAxis = chart.getAxisSet().getXAxis(0); xAxis.getTitle().setText(this.riskMetric.toString()); - xAxis.getTick().setFormat(new DecimalFormat("0.##%")); //$NON-NLS-1$ + xAxis.getTick().setFormat(new PercentNumberFormat("0.##%")); //$NON-NLS-1$ IAxis yAxis = chart.getAxisSet().getYAxis(0); yAxis.getTitle().setText(useIRR ? Messages.LabelPerformanceIRR : Messages.LabelPerformanceTTWROR); - yAxis.getTick().setFormat(new DecimalFormat("0.##%")); //$NON-NLS-1$ + yAxis.getTick().setFormat(new PercentNumberFormat("0.##%")); //$NON-NLS-1$ configurator = new DataSeriesConfigurator(this, DataSeries.UseCase.RETURN_VOLATILITY); configurator.addListener(this::updateChart); diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/ChartWidget.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/ChartWidget.java index c3cdcd3200..1c8ebf7d38 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/ChartWidget.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/ChartWidget.java @@ -34,6 +34,7 @@ import name.abuchen.portfolio.ui.util.SimpleAction; import name.abuchen.portfolio.ui.util.chart.TimelineChart; import name.abuchen.portfolio.ui.util.format.AmountNumberFormat; +import name.abuchen.portfolio.ui.util.format.PercentNumberFormat; import name.abuchen.portfolio.ui.util.format.ThousandsNumberFormat; import name.abuchen.portfolio.ui.views.ChartViewConfig; import name.abuchen.portfolio.ui.views.PerformanceChartView; @@ -295,7 +296,7 @@ public void update(Object object) if (useCase == DataSeries.UseCase.STATEMENT_OF_ASSETS) chart.getAxisSet().getYAxis(0).getTick().setFormat(new ThousandsNumberFormat()); else - chart.getAxisSet().getYAxis(0).getTick().setFormat(new DecimalFormat("0.#%")); //$NON-NLS-1$ + chart.getAxisSet().getYAxis(0).getTick().setFormat(new PercentNumberFormat("0.#%")); //$NON-NLS-1$ chart.getAxisSet().getYAxis(0).getTick().setVisible(get(ChartShowYAxisConfig.class).getIsShowYAxis()); diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/charts/DrawdownChartWidget.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/charts/DrawdownChartWidget.java index 4b41dbab91..b586d1b334 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/charts/DrawdownChartWidget.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/dashboard/charts/DrawdownChartWidget.java @@ -17,6 +17,7 @@ import name.abuchen.portfolio.snapshot.PerformanceIndex; import name.abuchen.portfolio.ui.Messages; import name.abuchen.portfolio.ui.util.chart.TimelineChart; +import name.abuchen.portfolio.ui.util.format.PercentNumberFormat; import name.abuchen.portfolio.ui.views.dashboard.ChartHeightConfig; import name.abuchen.portfolio.ui.views.dashboard.ChartShowYAxisConfig; import name.abuchen.portfolio.ui.views.dashboard.DashboardData; @@ -111,7 +112,7 @@ public void update(Object object) for (var s : chart.getSeriesSet().getSeries()) chart.getSeriesSet().deleteSeries(s.getId()); - chart.getAxisSet().getYAxis(0).getTick().setFormat(new DecimalFormat("0.#%")); //$NON-NLS-1$ + chart.getAxisSet().getYAxis(0).getTick().setFormat(new PercentNumberFormat("0.#%")); //$NON-NLS-1$ chart.getAxisSet().getYAxis(0).getTick().setVisible(get(ChartShowYAxisConfig.class).getIsShowYAxis()); Interval reportingPeriod = get(ReportingPeriodConfig.class).getReportingPeriod() diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/taxonomy/StackedChartViewer.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/taxonomy/StackedChartViewer.java index a70d8a902a..6f6ad8dc97 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/taxonomy/StackedChartViewer.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/views/taxonomy/StackedChartViewer.java @@ -8,6 +8,7 @@ import name.abuchen.portfolio.ui.editor.PortfolioPart; import name.abuchen.portfolio.ui.util.chart.StackedTimelineChart; +import name.abuchen.portfolio.ui.util.format.PercentNumberFormat; public class StackedChartViewer extends AbstractStackedChartViewer { @@ -20,7 +21,7 @@ public StackedChartViewer(PortfolioPart part, TaxonomyModel model, TaxonomyNodeR @Override protected void configureChart(StackedTimelineChart chart) { - chart.getAxisSet().getYAxis(0).getTick().setFormat(new DecimalFormat("#0.0%")); //$NON-NLS-1$ + chart.getAxisSet().getYAxis(0).getTick().setFormat(new PercentNumberFormat("#0.0%")); //$NON-NLS-1$ chart.getToolTip().setDefaultValueFormat(new DecimalFormat("#0.0%")); //$NON-NLS-1$ }