Skip to content

Commit

Permalink
fix non visible 0% tick axis since the switch to swtchart 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
mierin12 committed Nov 29, 2024
1 parent 9069fe2 commit 2b07192
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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$
}

Expand Down

0 comments on commit 2b07192

Please sign in to comment.