我正在用PHP和pchart绘制一些图表,但在X轴上我看到了一些长标签,有什么方法可以避免重叠吗?
或其他解决方案?
发布于 2012-07-25 01:11:57
我有时使用pchart2,在您描述的情况下,我个人倾向于修改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;
}发布于 2015-07-03 23:10:47
您可以尝试对标签的文本使用word-wrap函数,这样它们就会变得更短,并占据几行。
https://stackoverflow.com/questions/10716079
复制相似问题