tags:

views:

84

answers:

1

i am trying to graph the results of a survey, where the question is multiple choice.

eg. How would you describe this website? format:

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

the graph is a bar graph, where each bar represents one of those options, and the height of the bar depends on the number of times selected.

however, the labels are slanted at a 45 degree angle and are barely readable! here is my code:

<?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();
    }

and here is where i use it on the page:

<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] ?>" />

is there a setting i can apply to the graph which will make the text look nicer, or at least let me set the angle to 90 degrees so people can read it if they cock their head to the left?

thanks!

btw, mtchart is located here: http://code.google.com/p/mtchart/ and pchart (the original, which has mainly the same code) is here: http://pchart.sourceforge.net/documentation.php

+1  A: 

Edit the following line, which draws the horizontal labels:

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

The sixth argument (55) is the angle at which to write the text; 0 is horizontal, 90 is vertical, 120 is leaning back on itself, etc.. So, if you want some head cocking, set the value to 90.

The whole prototype for that method is:

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)
salathe
thanks a lot!!!
gsquare567