Page 1 of 1

Annotation CustomSize

Posted: Thu Sep 12, 2013 9:12 pm
by 15965760
What does it take to make CustomSize work for annotations?
The CustomSize elements below have no impact even though I tested for the returns in Annotation.php and I get 1, 50 and 50 respectively but no sizing changes in the annotation. CustomPosition works, but not CustomSize. What's missing?

Code: Select all

$label=new Annotation($chart->getChart());
$label->setCustomSize(true);
$label->setWidth(50);
$label->setHeight(50);
$label->getShape()->setCustomPosition(true);
$label->setTop(150);
$label->setLeft(50);
$label->setText(" text   ");
$label->Shape->Font->setSize(10);
$label->Shape->Font->setColor(Color::BLACK());
$label->Shape->setColor(new Color(235,238,238));
$label->Shape->getShadow()->setVisible(false);
$label->Shape->getPen()->setVisible(false);

Re: Annotation CustomSize

Posted: Thu Sep 12, 2013 11:09 pm
by 15965760
Investigating, I find that although the GET and SET for CustomSize, Width and Height are doing their thing, there appears no further implementation to override the default rectangle built by text length and font size calcs. Ergo, I implemented this override in Annotation.php at line 390...

Code: Select all

    if($this->getCustomSize()){//new
         $cw = $this->shape->getWidth();//new
         $ch = $this->shape->getHeight();//new
         $this->shape->setShapeBounds(new Rectangle($x - 4, $y - 4, $cw,$ch));//new		
    }else{//new
        $this->shape->setShapeBounds(new Rectangle($x - 4, $y - 4, $tmpW + 4,
        4 + ($tmpHeight * 1.20) + 4));
      }// end if//new
Now I am working to clear the error for multiline so I can fully test the implementation. If I use "\n" in annotation text, I get...
Notice: Undefined offset: 1 in G:\xampp\htdocs\charts\sources\tools\Annotation.php on line 430

I notice that the multiline is implemented differently in Chart.php, SeriesMarks.php, and Axis.php

Re: Annotation CustomSize

Posted: Fri Sep 13, 2013 3:11 am
by 15965760
OK, now to the undefined index error problem.

Currently, if you set text for an annotation such..
$label->setText("something\nsomethingelse");

The text will be processed through

Code: Select all

    public function multiLineTextWidth($s) {
        $result = new MultiLine();

        // Note our use of ===.  Simply == would not work as expected
        // because the position of 'a' was the 0th (first) character.
        $i = strpos($s, "\n"); //Language::getString("LineSeparator"));

        if ($i === FALSE) {
            $result->count = 1;
            $result->width = $this->graphics3D->textWidth($s);
        } else {
            $tmpResult = 0;
            $r = new CalcStringResults();

            while ($i !== false) {
                $r = $this->calcString($r, substr($s,0, $i + 1));
                $tmpResult = $r->tmpResult;

                $s = substr($s,$i + 1);
                $i = strpos($s, "\n"); //Language::getString("LineSeparator"));
            }

            $result->count = $r->numLines;
            $result->width = $r->tmpResult;

            if (strlen($s) != 0) {
                $result->count++;
                $result->width = max($tmpResult, $this->graphics3D->textWidth($s));
            }
        }
        return $result;
    }
in Chart.php which divides it up by the "\n" for a count and calculates the width by string length for the default rectangle width based on text length, etc.

That exchange takes place on Annotation.php around line 348 in the drawText function.
Short applicable excerpt:

Code: Select all

      $m = $this->chart->multiLineTextWidth($tmp);
      $tmpW = $m->width-$tmpHeight;
      $tmpN = $m->count;
      $tmpH = $tmpN * $tmpHeight;
Note the $tmpN (count)

A little farther down in Annotation.php, you find this near line 417 (NOTE the markup and commenting out)..

Code: Select all

      $s = Array();// Array of String
      //$s = StringFormat::split($tmp, Language::getString("LineSeparator")); <<-----ORIGINAL CODE
			
      $s = preg_split('/\\n/', $tmp ); <<-----MY CHANGE

      for($t = 1; $t <= $tmpN; $t++)
      {
         $g->textOut($x, $y + ($t * $tmpHeight),0, $s[$t - 1]);
      }
What was occurring is that there was an accurate count of string elements split by "\n" from the first return from Chart.php and then the string was subjected to this code at StringFormat.php around line 52...

Code: Select all

    public static function split($in, $ch ){
        $tmp= Array();

        $pos=false;

        do {
            $pos = strpos($in,$ch);

            if ($pos != false) {
                $s = substr($in,0,$pos);
                $tmp[]=$s;
                $in = substr($in,0,$pos+1);
            }
        } while ($pos != false);

            if (strlen($in) > 0) {
                $tmp[]=$in;
            }

        //$result = new String[$this->tmp->size()];
        return /*str_split(*/$tmp/*)*/;
    }
That code has a problem returning the array it is supposed to return with a "\n" delimiter. Ergo, this string "some text\nmore text" would be returned in a single element array rather than two. Then part of the code from Annotation.php..

Code: Select all

      for($t = 1; $t <= $tmpN; $t++)
      {
         $g->textOut($x, $y + ($t * $tmpHeight),0, $s[$t - 1]);
      }
Has this dilemma.. the count from the - public function multiLineTextWidth($s) { for that string is 2 but the array returned by StringFormat.php contains only one element with all the text minus the "\n". So at the end of the for on the count of 2 where the code seeks $s[$t - 1] $t =2 at that point, and key [1] does not exist.... hence the undefined index error.

Since I cannot see that StringFormat.php does anything but return the string as an array divided by "\n", I used preg_split above to solve.

NOTE: DOUBLE QUOTES FOR THE TEXT i.e., "text\nmore text" ARE A MUST, SINGLE QUOTES (apostrophes) WILL NOT WORK FOR THAT.

Re: Annotation CustomSize

Posted: Fri Sep 13, 2013 11:06 am
by yeray
Hi,

Thanks a lot for the investigation and suggestions! We'll try to implement them asap.

Re: Annotation CustomSize

Posted: Fri Sep 13, 2013 1:02 pm
by 15965760
While you are there, see what is up with the text-align for annotations. The following and variations of it have no effect..

$label->setTextAlign("CENTER");