Page 1 of 1
Legent text formatting
Posted: Wed Mar 20, 2019 2:02 pm
by 17185819
I have cretaed my legend as I described in post :
viewtopic.php?f=10&t=17135
I now have the issue that I need to format the numeric value from the legend.
e.G I have the data:
Bob 100
Bob 50
then i want to format the legend to display
150 € Bob
any idea how i can do this?
Re: Legent text formatting
Posted: Thu Mar 21, 2019 7:55 am
by 17185819
Hi. As far as i can see, in the Series class there is the valueToString method responsible for formatting the value. Would it be possible to change this such that it is possible to specify a lambda method to the series which takes care of the formatting? This would improve flexiblitiy when it comes to value formatting.
Re: Legent text formatting
Posted: Mon Mar 25, 2019 12:44 pm
by 17185819
We have written a small patch that adds the possibility of providing a custom formatting function for the legends value string:
Code: Select all
--- SWT/com/steema/teechart/styles/Series.java
+++ SWT/com/steema/teechart/styles/Series.java
@@ -1535,13 +1535,25 @@ public class Series extends TeeBase implements ISeries, Cloneable {
}
transient private DecimalFormat seriesFormat = null;
- private String formatValue(final double value) {
- try {
- return seriesFormat.format(value);
- } catch (Exception e) {
- return new DecimalFormat(Language.getString("DefValueFormat"))
+ private java.util.function.Function<Object, String> formatFunction = new java.util.function.Function<Object, String>()
+ {
+ @Override
+ public String apply(final Object value) {
+ try {
+ return seriesFormat.format(value);
+ } catch (final Exception e) {
+ return new DecimalFormat(Language.getString("DefValueFormat"))
.format(value);
+ }
}
+ };
+
+ private String formatValue(final double value) {
+ return formatFunction.apply(value);
+ }
+
+ public void setFormatFunction(final java.util.function.Function<Object, String> formatFunction) {
+ this.formatFunction = formatFunction;
}
private String labelOrValue(int valueIndex) {
This way it is easy to add formatting to the legend value string if more complex transformations are required that can be expressed by a formatting string.
Re: Legent text formatting
Posted: Mon Mar 25, 2019 3:54 pm
by yeray
Hello,
You can use the LegendAdapter to manually set the legend items. Ie:
Code: Select all
Pie pie1 = new Pie(tChart1.getChart());
pie1.fillSampleValues();
tChart1.setLegendResolver(new LegendAdapter() {
@Override
public String getItemText(Legend legend, LegendStyle legendStyle, int index, String text) {
if ((index >-1) && (index < pie1.getCount())) {
double value = pie1.getYValues().getValue(index);
text = new DecimalFormat(Language.getString("DefValueFormat")).format(value);
text += "€ " + Language.columnSeparator + pie1.getLabels().getString(index);
}
return text;
}
});
Re: Legent text formatting
Posted: Tue Mar 26, 2019 7:52 am
by 17185819
Does this maintain the column widths set in the legend configuration?
Re: Legent text formatting
Posted: Tue Mar 26, 2019 8:32 am
by yeray
Sorry, you can add Language.columnSeparator in the function above. I'll edit the code above adding it.
Re: Legent text formatting
Posted: Tue Mar 26, 2019 8:50 am
by 17185819
Thanks.