Pie percentage doesn't hit 100%

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
Softwell Solutions
Newbie
Newbie
Posts: 5
Joined: Fri Aug 10, 2012 12:00 am

Pie percentage doesn't hit 100%

Post by Softwell Solutions » Wed Feb 06, 2013 12:40 pm

Hello there,

I'm getting a few problems to get the percentage of the pie graph to 100%.
I use it to show the percentage of the values from query of the database.

The query returns integer values like:
34
27
1
2

Doing the math, we can get the follow percentage of each:
34 - 53,125%
27 - 42,1875%
1 - 1,5625%
2 - 3.125%

The pie just show 2 decimals (which is ok, because the default mask uses ##0.## '%' and i think that 2 decimals is ok.
But the result, you can see in the image below attached, doesn't sum the 100%, it always get 99,99%.

PS: We use the 2012.08.08 version
Ps2: We already has a "trick" posted into the forum somewhere else:

Code: Select all

    if (series.getChart().getParent() instanceof TChart) {
      TChart.class.cast(series.getChart().getParent()).setLegendResolver(new LegendResolver() {
        public String getItemText(Legend legend, LegendStyle legendStyle, int index, String text) {
          if (legend.getTextStyle() != LegendTextStyle.PERCENT) {
            return text;
          } else {
            String tmps = text.substring(0, text.length() - 1).trim();
            java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
            tmps = tmps.replace(",", ".");
            double tmpd = (Double.parseDouble(tmps) * 100);
            return String.valueOf((df.format(tmpd)) + "%");
          }
        }

        public LegendItemCoordinates getItemCoordinates(Legend legend, LegendItemCoordinates coordinates) {
          return coordinates;
        }

        public Rectangle getBounds(Legend legend, Rectangle rectangle) {
          return rectangle;
        }
      });
PS3: If i use "series.setPercentFormat("0.0 '%'")" i get 100% of the sum of each value, but, it doesn't have 2 decimals, only one.

Am i doing something wrong ? Should teechart round the values to get 100% ?

Thanks
Attachments
pie_graph99.png
Pie Graph 99%
pie_graph99.png (80.63 KiB) Viewed 13522 times

Softwell Solutions
Newbie
Newbie
Posts: 5
Joined: Fri Aug 10, 2012 12:00 am

Re: Pie percentage doesn't hit 100%

Post by Softwell Solutions » Wed Feb 06, 2013 12:45 pm

Pie with 99%

Sorry about the wrong image before
Attachments
pie_graph99.png
pie_graph99.png (80.6 KiB) Viewed 13511 times

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

Re: Pie percentage doesn't hit 100%

Post by Yeray » Thu Feb 07, 2013 10:11 am

Hello,

Right, this is a rounding problem. Since the PercentFormat is specified to have a limited number of decimals, there will always be set of values that will end in a non 100 % sum.

In some cases, adding some more decimals will be enough:

Code: Select all

series.setPercentFormat("##0.### '%'");
However, in some other cases, the rounding conflict will be in a too far decimal number (to be shown in a easy to read text). Or even you can have periodic numbers. Ie, you can have the values 33, 33, 33. They sum 99, and their percentage is for both them 33.3333... Which one of the three texts should be rounded?
We'd better let our customers decide it. You can always use the LegendResolver and MarkTextResolver to manually calculate the percentages and decide where to apply any rounding.
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

Softwell Solutions
Newbie
Newbie
Posts: 5
Joined: Fri Aug 10, 2012 12:00 am

Re: Pie percentage doesn't hit 100%

Post by Softwell Solutions » Thu Feb 07, 2013 12:42 pm

Right,

Do you have a sample of how can i try to solve this using LegendResolver and MarkTextResolver ?

Softwell Solutions
Newbie
Newbie
Posts: 5
Joined: Fri Aug 10, 2012 12:00 am

Re: Pie percentage doesn't hit 100%

Post by Softwell Solutions » Thu Feb 07, 2013 12:44 pm

I mean, where is the place that i have all the values in % to try to get round one of them... ?
Where am i supposed to do it ?

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

Re: Pie percentage doesn't hit 100%

Post by Yeray » Fri Feb 08, 2013 10:22 am

Hi,
Softwell Solutions wrote:Do you have a sample of how can i try to solve this using LegendResolver and MarkTextResolver ?
Softwell Solutions wrote:I mean, where is the place that i have all the values in % to try to get round one of them... ?
Where am i supposed to do it ?
You can use the percentString function to get the % strings. You could, for example, calculate the sum of all them except the last, and show 100-sum for the last mark.
Ie:

Code: Select all

		tChart1.getAspect().setView3D(false);
		Pie pieSeries = new Pie(tChart1.getChart());
		pieSeries.add(33);
		pieSeries.add(33);
		pieSeries.add(33);
		
		pieSeries.getMarks().setStyle(MarksStyle.PERCENT);
		tChart1.getLegend().setTextStyle(LegendTextStyle.PERCENT);
		
		pieSeries.setMarkTextResolver(new MarkTextResolver() {
			@Override
			public String getMarkText(int valueIndex, String markText) {
				if ((valueIndex==tChart1.getSeries(0).getCount()-1) && 
						(tChart1.getSeries(0).getMarks().getStyle() == MarksStyle.PERCENT)) {
					
					double subTotal = 0;
					for (int i=0; i<tChart1.getSeries(0).getCount()-1; i++) {
						String text = tChart1.getSeries(0).getMarks().percentString(valueIndex, false);
						String tmps = text.substring(0, text.length() - 1).trim();
						tmps = tmps.replace(",", ".");
						subTotal += (Double.parseDouble(tmps));
					}
					
					markText = String.valueOf(100-subTotal) + " %";
				}
				
				return markText;
			}
		});
And the same for the LegendResolver.
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

Softwell Solutions
Newbie
Newbie
Posts: 5
Joined: Fri Aug 10, 2012 12:00 am

Re: Pie percentage doesn't hit 100%

Post by Softwell Solutions » Tue Jul 23, 2013 5:46 pm

Yeray,

Thanks again!

Post Reply