PHP mtChart(新pChart):如何控制x轴标签的角度?
我正在尝试绘制一项调查的结果,其中的问题是多项选择。
例如。您会如何描述这个网站? 格式:
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 度,以便人们可以阅读它如果他们把头转向左边呢?
顺便说一句,mtchart
位于此处:http://code.google.com /p/mtchart/ pchart(原始版本,主要具有相同的代码)位于:http://pchart.sourceforge。 .net/documentation.php
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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑以下行,绘制水平标签:
第六个参数 (
55
) 是写入文本的角度;0
是水平的,90
是垂直的,120
是靠在自己身上,等等。所以,如果你想要一些头部翘起,设置值为90
。该方法的整个原型是:
Edit the following line, which draws the horizontal labels:
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 to90
.The whole prototype for that method is: