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?
Legent text formatting
Re: Legent text formatting
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
We have written a small patch that adds the possibility of providing a custom formatting function for the legends value string:
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.
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) {
Re: Legent text formatting
Hello,
You can use the LegendAdapter to manually set the legend items. Ie:
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;
}
});
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |
Re: Legent text formatting
Does this maintain the column widths set in the legend configuration?
Re: Legent text formatting
Sorry, you can add Language.columnSeparator in the function above. I'll edit the code above adding it.
Best Regards,
Yeray Alonso Development & Support Steema Software Av. Montilivi 33, 17003 Girona, Catalonia (SP) | |
Please read our Bug Fixing Policy |