Page 1 of 1

Pie percentage doesn't hit 100%

Posted: Wed Feb 06, 2013 12:40 pm
by 17463196
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

Re: Pie percentage doesn't hit 100%

Posted: Wed Feb 06, 2013 12:45 pm
by 17463196
Pie with 99%

Sorry about the wrong image before

Re: Pie percentage doesn't hit 100%

Posted: Thu Feb 07, 2013 10:11 am
by yeray
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.

Re: Pie percentage doesn't hit 100%

Posted: Thu Feb 07, 2013 12:42 pm
by 17463196
Right,

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

Re: Pie percentage doesn't hit 100%

Posted: Thu Feb 07, 2013 12:44 pm
by 17463196
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 ?

Re: Pie percentage doesn't hit 100%

Posted: Fri Feb 08, 2013 10:22 am
by yeray
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.

Re: Pie percentage doesn't hit 100%

Posted: Tue Jul 23, 2013 5:46 pm
by 17463196
Yeray,

Thanks again!