PHP - pchart长标签

时间:2012-05-23 08:19:58

标签: php pchart

我正在使用PHP和pchart绘制一些图形但是在X轴上我看到了一些长标签,以避免重叠?

或其他解决方案?

2 个答案:

答案 0 :(得分:2)

我有时使用pchart2,在你描述的情况下,我个人倾向于修改pchart代码本身。

您必须搜索标签的打印位置并执行调整。不幸的是,pchart的代码是......嗯......好吧,在我看来不是很好,所以很容易迷失它。

也许,您可以欣赏我的自制功能,将长文本分成多行以强制执行文本的最大可接受宽度:

    /**
 * 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;
}

答案 1 :(得分:0)

您可以尝试将word-wrap功能用于标签的文字,以便它们变短并占用几行。