我正在用 PHP 和 pchart 绘制一些图表,但在 X 轴上我看到了一些长标签,有什么方法可以避免重叠?
还是其他解决方案?
我有时会使用 pchart2,在您描述的情况下,我个人倾向于修改 pchart 代码本身。
您必须搜索打印标签的位置并执行调整。不幸的是,pcchart 的代码是……嗯……嗯,在我看来不是很好,所以很容易迷失在其中。
也许,您可以欣赏我自制的功能,将长文本分成多行以强制文本的最大可接受宽度:
/**
* 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;
}
您可以尝试对 word-wrap
标签的文本使用该功能,使它们变得更短并占用多行。