0

I am drawing some graph with PHP and pchart but on the X axis I saw some long labels, any way to avoid overlapping?

Or other solutions?

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • can you provide code snippet in which you are drawing your x-axis ? – jugnu May 23 '12 at 08:24
  • i don't have it yet but would be like this http://pchart.sourceforge.net/screenshots.php?ID=10 but with longer names –  May 23 '12 at 18:36

2 Answers2

2

I sometimes use pchart2 and in situations such as you described I personally tend to modify the pchart code itself.

You will have to search for the place where the labels are printed and perform the tweak. Unfortunately, the code of the pchart is... ehm... well, not very nice in my opinion, so one can easily get lost in it.

Perhaps, you could appreciate my home-made function for dividing a long text to more than one lines to enforce the maximum acceptable width of the text:

    /**
 * Insert ends of line into the given string to prevent exceeding the given width of the
 * string when it is printed.
 * @param unknown_type $text String for separation by EOLs.
 * @param unknown_type $displayFont Font used for text printing.
 * @param unknown_type $displaySize Size of the printed text.
 * @param unknown_type $angle Angle of text printing.
 * @param unknown_type $maximumWidth Maximum allowed width (pixels).
 * @return string The edited input text.
 */
function changeTextForMaximumWidth($text, $displayFont, $displaySize, $angle, $maximumWidth) {

    $result = "";

    $processedText = $text;
    $remainingText = "";

    while ($processedText != "") {
        // replace this by any routine that computes the width and height of the text
        $TxtPos = $this->getTextBox(0, 0, $displayFont, $displaySize, $angle, $processedText);

        // what about TXT margin??
        $TxtWidth = abs($TxtPos[0]["X"] - $TxtPos[1]["X"]); 

        // if text length is sufficient
        if ($TxtWidth <= $maximumWidth) {

            $result .= $processedText;
            if ($remainingText != "") {
                $result .= PHP_EOL;
            }

            $processedText = $remainingText;
            $remainingText = "";
            continue; 
        }

        // the text is too wide
        // try to make it shorter
        $pos = strrpos($processedText, " ");
        if ($pos == FALSE) {
            // cannot be made shorter
            $result .= $processedText;
            if ($remainingText != "") {
                $result .= PHP_EOL;
            }

            $processedText = $remainingText;
            $remainingText = "";
            continue;
        }

        // can be shorten

        $shorten = substr($processedText, 0, $pos);

        $restLength = strlen($processedText) - ($pos + 1); 
        $rest = substr($processedText, $pos + 1, $restLength);

        $processedText = $shorten;
        $remainingText = $rest . " " . $remainingText;
    }

    return $result;
}
Matfyz
  • 61
  • 4
0

You can try to use the word-wrap function to your label's text, so that they become shorter and take several lines.

Julian E.
  • 4,687
  • 6
  • 32
  • 49
slavugan
  • 1,463
  • 17
  • 17