PHP mtChart(新pChart):我如何控制x轴标签的角度?

时间:2010-06-14 19:09:55

标签: php pchart

我正在尝试绘制调查结果,其中问题是多项选择。

例如。你会如何描述这个网站? 格式:

option | number of times selected | percentage of users who selected that option

Informative     1   50%
All of the above    1   50%
Interesting     0   0%
Intelligent     0   0%
Cool    0   0%
Incredible  0   0%
Sleek   0   0%
Amazing

图表是一个条形图,其中每个条形代表其中一个选项,条形的高度取决于所选的次数。

然而,标签倾斜45度角,几乎无法读取!这是我的代码:

<?php
require_once ("includes/common.php");
require_once ("graph/mtChart.min.php");

// type must be specified
$type = $_GET['type'];

if($type == "surveys_report_MC_or_CB") {
    // PARAMS
    $surveyID   = $_GET['surveyID'];
    $questionID = $_GET['questionID'];
    // END PARAMS

    $question   = SurveyQuestions::getSingle($questionID);
    $answers    = SurveyAnswers::getAll($questionID);

    $options        = SurveyQuestionOptions::getAll($question[SurveyQuestions::surveyQuestionID]);
    $others         = SurveyAnswers::setOptionCounts($options, $answers);
    $printedOthers  = false;

    // set graph
    $values = array();
    $axisLabels = array();
    foreach($options as $option) {
        $values[$option[SurveyQuestionOptions::optionText]] = $option['count'];
        $axisLabels[] = $option[SurveyQuestionOptions::optionText];
    }

    $graphs = array();
    $graphs[0] = $values;

    $xName = "Option";
    $yName = "Number of Times Selected";
    $graphTitle = $question[SurveyQuestions::question];
    $series = array("Total");
    $showLegend = false;
    $tall = false;
}                   

drawGraph($graphs, $axisLabels, $xName, $yName, $graphTitle, $series, $showLegend, $tall);




function drawGraph($graphs, $axisLabels, $xName, $yName, $graphTitle, $series, $showLegend, $tall) {
        $Graph = ($tall) ? new mtChart(575,375) : new mtChart(575,275);

        // Dataset definition
        $avg = 0;
        $i = 0;
        foreach ($graphs as $key => $value) {
            $Graph->AddPoint($value,"series" . $key);
            $Graph->SetSerieName($series[$key],"series" . $key);

            // Get average
            $avg += array_sum($value);
            $size = sizeof($value);
            $i += $size;

            // Calculate x-axis tick interval
            $step = ceil($size / 25);
        }

        $Graph->AddPoint($axisLabels,"XLabel");
        $Graph->AddAllSeries();
        $Graph->RemoveSerie("XLabel");
        $Graph->SetAbsciseLabelSerie("XLabel");
        $Graph->SetXAxisName($xName);
        $Graph->SetYAxisName($yName);

        // Get from cache if it exists
        $Graph->enableCaching(NULL, 'graph/cache/');
        $Graph->GetFromCache();

        // Initialize the graph
        $Graph->setInterval($step);
        $Graph->setFontProperties("graph/tahoma.ttf",8);
        ($showLegend) ? $Graph->setGraphArea(45,30,475,200) : $Graph->setGraphArea(75,30,505,200);
        $Graph->drawGraphArea(255,255,255,TRUE);
        $Graph->drawScale(SCALE_START0,100,100,100,TRUE,55,1,TRUE);
        $Graph->drawGrid(4,TRUE,230,230,230,50);

        // Draw the 0 line
        $Graph->setFontProperties("graph/tahoma.ttf",6);
        $Graph->drawTreshold(0,143,55,72,TRUE,TRUE);

        // Draw the bar graph
        $Graph->drawBarGraph();

        // Draw average line
        $Graph->drawTreshold($avg/$i, 0, 0, 0, FALSE, FALSE, 5);     

        // Finish the graph
        $Graph->setFontProperties("graph/tahoma.ttf",8);
        if ($showLegend) {
            $Graph->drawLegend(482,30,255,255,255,255,255,255,100,100,100);
        }
        $Graph->setFontProperties("graph/tahoma.ttf",10);
        $Graph->drawTitle(0,22,$graphTitle,100,100,100,555);

        // Draw Graph
        $Graph->Stroke();
    }

这是我在页面上使用它的地方:

<div class="graph_container">
                            <img src="drawGraph.php?type=surveys_report_MC_or_CB&surveyID=<?php
                                echo $survey[Surveys::surveyID] ?>&questionID=<?php
                                echo $question[SurveyQuestions::surveyQuestionID] ?>" />

是否有一个设置可以应用于图表,这将使文本看起来更好,或者至少让我将角度设置为90度,这样人们可以读取它,如果他们向左侧抬头?

btw,mtchart位于此处:http://code.google.com/p/mtchart/ 和pchart(原文,主要是相同的代码)在这里:http://pchart.sourceforge.net/documentation.php                             

1 个答案:

答案 0 :(得分:1)

编辑以下行,绘制水平标签:

 $Graph->drawScale(SCALE_START0,100,100,100,TRUE,55,1,TRUE);
 //                                              ^^--- Edit this value

第六个参数(55)是写文本的角度; 0是水平的,90是垂直的,120靠近自己等等。所以,如果你想要一些头部翘起,请将值设置为90。< / p>

该方法的整个原型是:

void drawScale(int $ScaleMode = SCALE_NORMAL, 
               int $R = 150, int $G = 150, int $B = 150, 
               bool $DrawTicks = TRUE, int $Angle = 0, int $Decimals = 1, 
               bool $WithMargin = FALSE, bool $RightScale = FALSE)