|
Legend box + setTruncate = issues? |
Posted by Serge on May-02-2016 18:06 |
|
Hi!
I've noticed several issues with setTruncate method and legend boxes. Let's start with the following code (I just modified legendpie.php sample for this demo).
<?php
require_once("../lib/phpchartdir.php");
$data = array(25, 18, 15, 12, 8, 30, 35);
$labels = array("Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities",
"Production Long Text");
$c = new PieChart(450, 270);
$c->setPieSize(150, 160, 80);
$l = $c->addLegend(0, 0);
$l->setWidth(450);
$l->setCols(4);
$c->setLabelFormat("{value}");
$c->setLabelLayout(1, -20);
$c->setData($data, $labels);
header("Content-type: image/png");
print($c->makeChart2(PNG));
?>
This produces a chart shown by 1.png. So far so good.
Now let's try truncating the longest label (the last one). Legend's width is set to 450 px and I asked for 4 columns. So I assumed I had to make the following call.
$l->setTruncate(110, 1);
Unfortunately it didn't work as expected, see 2.png
My experiments show that one has to pass the legend width (!) as the first parameter to setTruncate to make it work properly with legends.
$l->setTruncate(450, 1); # see 3.png
Is this a bug?
Another issue arises if I use the AutoGrid option.
$l->setCols(AutoGrid); # see 4.png
Adding a setTruncate call "breaks" the legend.
$l->setTruncate(110, 1); # see 5.png
|
Re: Legend box + setTruncate = issues? |
Posted by Peter Kwan on May-03-2016 00:37 |
|
Hi Serge,
The width and maxWidth of the LegendBox object refers to the width and maxWidth of the LegendBox, not the width and maxWidth of the text in a legend entry.
By default, the LegendBox will grow as wide as necessary to contain the legend entries. If you set the maxWidth, the LegendBox will not grow beyond the maxWidth, but it can be shorter than the maxWidth if the legend texts are short. If you set the width, the LegendBox will always be of the specified width.
If you set both maxWidth and width, they should be the same value to be consistent.
So the charts #1, #3 and #4 appears normal. The charts #2 and #5 may be due to the inconsistency between width and maxWidth. When drawing legend text, the text width is computed assuming the whole legend box cannot exceed 110 pixels. In #2, it means each legend item only has a few pixels for the text, so only 1 character is visible. In #5, a few more characters are possible, so it displays the "...".
If you would like to set the maxWidth of the legend text, you may use the followings:
$l->setText("<*block,maxwidth=110,truncate=1*>{label}");
Hope this can help.
Regards
Peter Kwan |
|