|
Labels on barchart |
Posted by Line Wolff on Jan-05-2015 22:40 |
|
I'm rendering a small bar chart (see attached file #1).
I'd like the labels to go inside the bar, if the bar can contain the label, and outside, if it
can't (see attached file #2). Right now it doesn't render the label if it doesn't fit inside the
bar (as documented).
This is the code:
$layer = $c->addBarLayer3($data, $colors);
$layer->setBorderColor(Transparent);
$layer->setDataLabelStyle("",$font_size,0x000000, 90);
$layer->setBarGap(0.1);
$c->yAxis->setLinearScale(0, max($data), NoValue, NoValue);
I've thought about rendering the label manually, but I can't figure out how to position it
according to the bar. I figured it was probably something to do with TextBox.setAlignment
but need to know what to position against.
Could you give me a pointer? Thanks!
|
Re: Labels on barchart |
Posted by Peter Kwan on Jan-06-2015 02:03 |
|
Hi Line,
You can force the labels to always render by using:
$layer->setMinLabelSize(0);
With your current code, the labels are positioned so that the the top edge of the label
box aligns with the top edge of the bar. This probably is not what you want for short
bars.
In ChartDirector, "aggregate labels" (using setAggregateLabelStyle) are for labelling the
bars, and are put outside the bars. "Data labels" (using setDataLabelStyle) are for
labelling the "bar segments" (a bar can have multiple segments as in a stacked bar or
overlay bar), and so have to be put internal to the bar segment. For your case, as the
bar only have one segment, the setDataLabelStyle can be thought of putting the labels
inside the bar.
If you want some labels to be inside the bars, and some labels outside it, you can use
addCustomDataLabel and addCustomAggregateLabel. It is like:
$maxValue = max($data);
for ($i = 0; $i < count($data); ++$i)
{
if ($data[$i] > $maxValue / 2)
$layer->addCustomDataLabel(0, $i, "{value}", "arial.ttf", $font_size, 0x000000, 90);
else
$layer->addCustomAggregateLabel($i, "{value}", "arial.ttf", $font_size, 0x000000, 90);
}
Hope this can help.
Regards
Peter Kwan |
Re: Labels on barchart |
Posted by Line Wolff on Feb-24-2015 20:50 |
|
Sorry for the late reply.
It's a smooth solution and I can get it to work - almost.
I say "almost", because apparently I can only target dataSet 0, dataItem 0, when using
addCustomDataLabel() with addBarLayer3() (for multi-colored bars) and not the other
dataItems (1 and 2) in dataSet 0. I'm using the code as per your example. It works if I
switch to using addBarLayer() instead of addBarLayer3().
I then tried to target dataSet 1 and dataSet 2, both with dataItem 0, thinking
addBarLayer3() maybe behind the scenes splits the data into several sets due to the
multi-coloring, but (of course) this doesn't work either.
Below is example code to reproduce it (just uncomment lines with addBarLayer() and
addBarLayer3() respectively).
Is there something I'm perhaps overlooking? Thanks.
<?php
require_once("phpchartdir.php");
$data = array(30,50,20);
$colors = array(0x6ba8ed, 0x97e165, 0xf0c943);
$c = new XYChart(150, 150, Transparent);
$c->setPlotArea(0, 0, 150, 150, Transparent, -1, Transparent, Transparent,
Transparent);
$layer = $c->addBarLayer($data, $colors); // addBarLayer works -> custom data labels
show
#$layer = $c->addBarLayer3($data, $colors); // addBarLayer3 fails -> custom data
labels don't show
$layer->addCustomDataLabel(0, 0, "item0", "arial.ttf", 8, 0xff0000, 90);
$layer->addCustomDataLabel(0, 1, "item1", "arial.ttf", 8, 0xff0000, 90);
$layer->addCustomDataLabel(0, 2, "item2", "arial.ttf", 8, 0xff0000, 90);
header("Content-type: image/png");
print($c->makeChart2(PNG));
?> |
Re: Labels on barchart |
Posted by Line Wolff on Feb-24-2015 20:52 |
|
Sorry, there's an error in the comment in the line:
#$layer = $c->addBarLayer3($data, $colors); // addBarLayer3 fails -> custom data
labels don't show
The comment should of course read:
"// addBarLayer3 fails -> only the first custom data label (0,0) show - not the rest (0,1)
and (0,2)" |
Re: Labels on barchart |
Posted by Peter Kwan on Feb-25-2015 00:06 |
|
Hi Line,
Instead of (0, 0), (0, 1), (0, 2) .. may be you can try (0, 0), (1, 1), (2, 2), ....
Historically, each data set can only have one fill color. A long time ago, to create a
multicolor bar chart, one has to use multiple data sets, with each data set containing one
bar in the correct position. As a lot of people wants multi-color bar charts, so later we
created addBarLayer3, which essentially is a short-cut to create multiple data sets, each
with one bar, so that the bars can have different colors. Because of this, with
addBarLayer3, each bar has a different data set number as well as data item number.
Regards
Peter Kwan |
Re: Labels on barchart |
Posted by Line Wolff on Feb-25-2015 01:46 |
|
Splendid, that worked! Then I wasn't far off with my assumption, I just needed to
increment both set and item. Thanks as always for your quick replies and explanation. |
|