Legent text formatting

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
patronas
Newbie
Newbie
Posts: 26
Joined: Fri Mar 01, 2019 12:00 am

Legent text formatting

Post by patronas » Wed Mar 20, 2019 2:02 pm

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?

patronas
Newbie
Newbie
Posts: 26
Joined: Fri Mar 01, 2019 12:00 am

Re: Legent text formatting

Post by patronas » Thu Mar 21, 2019 7:55 am

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.

patronas
Newbie
Newbie
Posts: 26
Joined: Fri Mar 01, 2019 12:00 am

Re: Legent text formatting

Post by patronas » Mon Mar 25, 2019 12:44 pm

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.

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Legent text formatting

Post by Yeray » Mon Mar 25, 2019 3:54 pm

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;
			}
			
		});
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

patronas
Newbie
Newbie
Posts: 26
Joined: Fri Mar 01, 2019 12:00 am

Re: Legent text formatting

Post by patronas » Tue Mar 26, 2019 7:52 am

Does this maintain the column widths set in the legend configuration?

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Legent text formatting

Post by Yeray » Tue Mar 26, 2019 8:32 am

Sorry, you can add Language.columnSeparator in the function above. I'll edit the code above adding it.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

patronas
Newbie
Newbie
Posts: 26
Joined: Fri Mar 01, 2019 12:00 am

Re: Legent text formatting

Post by patronas » Tue Mar 26, 2019 8:50 am

Thanks.

Post Reply