首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP - pchart长标签

PHP - pchart长标签
EN

Stack Overflow用户
提问于 2012-05-23 16:19:58
回答 2查看 916关注 0票数 0

我正在用PHP和pchart绘制一些图表,但在X轴上我看到了一些长标签,有什么方法可以避免重叠吗?

或其他解决方案?

EN

回答 2

Stack Overflow用户

发布于 2012-07-25 01:11:57

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

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

也许,您可能会欣赏我的自制函数,该函数将一个长文本划分为多行,以实现文本的最大可接受宽度:

代码语言:javascript
复制
    /**
 * 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;
}
票数 2
EN

Stack Overflow用户

发布于 2015-07-03 23:10:47

您可以尝试对标签的文本使用word-wrap函数,这样它们就会变得更短,并占据几行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10716079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档