Annotation CustomSize

TeeChart for PHP
Post Reply
rustydiver
Newbie
Newbie
Posts: 22
Joined: Tue Apr 30, 2013 12:00 am

Annotation CustomSize

Post by rustydiver » Thu Sep 12, 2013 9:12 pm

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);

rustydiver
Newbie
Newbie
Posts: 22
Joined: Tue Apr 30, 2013 12:00 am

Re: Annotation CustomSize

Post by rustydiver » Thu Sep 12, 2013 11:09 pm

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

rustydiver
Newbie
Newbie
Posts: 22
Joined: Tue Apr 30, 2013 12:00 am

Re: Annotation CustomSize

Post by rustydiver » Fri Sep 13, 2013 3:11 am

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.

Yeray
Site Admin
Site Admin
Posts: 9509
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Annotation CustomSize

Post by Yeray » Fri Sep 13, 2013 11:06 am

Hi,

Thanks a lot for the investigation and suggestions! We'll try to implement them asap.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

rustydiver
Newbie
Newbie
Posts: 22
Joined: Tue Apr 30, 2013 12:00 am

Re: Annotation CustomSize

Post by rustydiver » Fri Sep 13, 2013 1:02 pm

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");

Post Reply