|
stacked bar chart How to display the percentages selectively? |
Posted by jayadev on Jul-07-2017 10:41 |
|
I'm rendering a percentage stacked bar chart (see attached file).
I would like to show the percentages in the Green and Red area to be always displayed.
The Yellow can display if there is space to display. But if there is no space the Yellow should not be displayed. Is there a way to achieve this.
This is the code:
$layer = $c->addBarLayer2(Percentage);
$layer->setBorderColor(0xffffff);
$layer->setDataLabelFormat("{value|1}%");
# Add the three data sets to the bar layer, using icons images with labels as data set names
$layer->addDataSet($data0, 0x009900, "<*block,valign=absmiddle*>XXX<*/*>");
$layer->addDataSet($data1, 0xFFFF00, "<*block,valign=absmiddle*>YYY<*/*>");
$layer->addDataSet($data2, 0xCC0000, "<*block,valign=absmiddle*>ZZZ<*/*>");
I am aware of the $layer->setMinLabelSize(0); which will display the labels even if there is no space. But I want the yellow area percentage to be displayed if there is space.
Please help.
|
Re: stacked bar chart How to display the percentages selectively? |
Posted by Peter Kwan on Jul-08-2017 19:07 |
|
Hi Jayadev,
ChartDirector can automatically hide labels if there is insufficient space, or to always display the labels, but for data labels, they can only be controlled on a per layer basis, not per data set.
For your case, I can think of two methods:
Method (1)
=======
(a) Create two layers using the same data. The top layer will have completely transparent bars so it would not affect the bottom visible stacked bar layer.
(b) For the bottom layer, configures it to always display labels using setMinLabelSize(0). Configure the yellow bar labels to be transparent (using DataSet.setDataLabelStyle). So the red and green labels are always visible.
(c) For the top layer, configures the red and green labels to be transparent. This provides the yellow labels which are only visible if there is enough space.
Method (2)
=======
Always display all the labels. Then use addCustomDataLabel in a loop to hide the yellow label (eg. by setting the label to an empty space) if its percentage too small. Whereas this method is simpler, you would need to determine which value is considered as too small.
Regards
Peter Kwan |
Re: stacked bar chart How to display the percentages selectively? |
Posted by jayadev on Jul-10-2017 17:37 |
|
This solved the issue. I did it with the second method.
Peter Kwan wrote:
Hi Jayadev,
ChartDirector can automatically hide labels if there is insufficient space, or to always display the labels, but for data labels, they can only be controlled on a per layer basis, not per data set.
For your case, I can think of two methods:
Method (1)
=======
(a) Create two layers using the same data. The top layer will have completely transparent bars so it would not affect the bottom visible stacked bar layer.
(b) For the bottom layer, configures it to always display labels using setMinLabelSize(0). Configure the yellow bar labels to be transparent (using DataSet.setDataLabelStyle). So the red and green labels are always visible.
(c) For the top layer, configures the red and green labels to be transparent. This provides the yellow labels which are only visible if there is enough space.
Method (2)
=======
Always display all the labels. Then use addCustomDataLabel in a loop to hide the yellow label (eg. by setting the label to an empty space) if its percentage too small. Whereas this method is simpler, you would need to determine which value is considered as too small.
Regards
Peter Kwan
|
|