I´m trying to graph a gaussian chart in php. I found the jpgraph library, but I don´t know how to use the $x, $y data. Please somebody who knows how to declare the axis. Thanks. This is my code:
while ($row = pg_fetch_assoc($sql))
{
$ydata = array();
$xdata = array();
$ydata[] = abs(($row['grade']-$miu)/$sigma);
$xdata[] = 0;
for ($i = 0; $i <= 60; $i+=10) // put z value
{
$xdata[] = 0 + $i;
$ydata[] = 0 + $i;
}
$graph = new Graph(600,400,"auto");
$graph->SetScale("linlin");
$lplot = new LinePlot($ydata,$xdata);
$lplot->SetColor("blue");
$lplot->SetWeight(2);
$graph->Add($lplot);
// Add data to X coordinate
$graph->xaxis->SetTickLabels($xdata);
// Display the graph
$graph->Stroke();
}
I already resolved it.
while ($row = pg_fetch_assoc($sql))
{
$z[$i] = ($row['grade'] - $miu)/$sigma; // $z => arreglo con notas normalizadas
$ordenada[$i] = (1/($sigma*sqrt(pi())))*(exp(-0.5*(($row['grade']-$miu)*($row['grade'] - 57))/(12*12)));
$i++;
}
//rest of code
$p1 = new LinePlot($ordenada);
$graph->Add($p1);
Related
I've got an array with specifications. I want each specification to become a column. I am having trouble with working this out though.
$specifications = new Specifications();
$columnCounter = 1;
foreach ($specifications as $specificationId => $specification) {
$column = PHPExcel_Cell::getColumnByNumber($columnCounter);
$objPHPExcel
->getActiveSheet()
->getColumnDimension($column)
->setAutoSize(true)
;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($column.'1', $specification['value'])
;
$columnCounter++;
}
The PHPExcel::getColumnByNumber() is of course an imaginary function. Though I am wondering how others do this and how best to address this.
$book = new PHPExcel();
$book->setActiveSheetIndex(0);
$sheet = $book->getActiveSheet();
$sheet->setTitle('Sets');
$xls_row = 5;
$xls_col = 3;
foreach($specifications as $specificationId => &$specification)
{
$adr = coord($xls_row, $xls_col);
$sheet->setCellValueExplicit($adr, $specification->title, PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->getColumnDimension(coord_x($xls_col))->setAutoSize(true);
$xls_col++;
}
// convert a 0-based coordinate value into EXCEL B1-format
function coord_x($x)
{
if($x<26) $x = chr(ord('A')+$x);
else
{
$x -= 26;
$c1 = $x % 26;
$c2 = intval(($x - $c1)/26);
$x = chr(ord('A')+$c2).chr(ord('A')+$c1);
}
return $x;
}
// convert X,Y 0-based cell address into EXCEL B1-format pair
function coord($y,$x)
{
return coord_x($x).($y+1);
}
I'm new at using pChart. I take data from database to construct the graph.
I will have a random number of rows and I would like to do a graph for each row.
(one row -> one graph). Is it possible?
So far I can do the graph but all the rows are in the same graph.
Here is my code:
<?php
include("pChart/class/pData.class.php");
include("pChart/class/pDraw.class.php");
include("pChart/class/pImage.class.php");
include("pChart/class/pPie.class.php");
$myData = new pData();
$Requete = "SELECT * FROM `day`";
$Result = mysql_query($Requete, $db);
while($row = mysql_fetch_array($Result)){
$hour = explode(" ", $row["g1"]);
$nb = $row["numb"] * 2;
for($i = 0; $i < $nb; $i++){
if ($i%2 == 1){
$time[$i] = ($hour[$i])/ 60;
$myData->addPoints($time[$i],"year");
}else{
if ($hour[$i] == "00"){
$name[$i] = "On";
}elseif ($hour[$i] == "02"){
$name[$i] = "Off";
}
$myData->addPoints($name[$i],"name");
}
}
}
$myData->setAbscissa("name");
$myData->setSerieDescription("year","Application A");
$myPicture = new pImage(600,300,$myData);
$myPicture->setFontProperties(array("FontName"=>"pChart/fonts/tahoma.ttf","FontSize"=>16));
$PieChart = new pPie($myPicture,$myData);
$PieChart->draw3DPie(340,125,array("DrawLabels"=>TRUE,"Border"=>TRUE));
$myPicture->autoOutput("images/example.png");
?>
If I understand the question, you want a picture for every row? In that case I think that you will have to include the lower part of your code (starting from $myData to $myPicture) in the while loop.
A little background
I am attempting to migrate a project built with Symfony 1.2 from one server to another. One of the functions of the project is to build a graph (originally done with JpGraph 2.3.5).
The graph does not display as intended without modifications to the code and am looking for some insight on what I might be overlooking. Images are linked due to not having enough points to post. Graph Image Gallery
The following graph is what is generated by the code block below
<?php
public function Graph($section) {
$report = $section->getReport();
$this->crews = array();
foreach ($section->getCrews() as $crew) {
$this->crews[$crew->getId()] = $crew;
};
# get the data
$nextDayValues = $section->getNextDayValues();
$nextDayValueLabels = $section->getNextDayValueLabels();
$max_y = max($nextDayValues) < 7 ? 7 : max($nextDayValues);
$this->crew_order = array_keys($nextDayValues);
$this->summary = $this->getSummary();
$this->bar_count = count($this->crews) + count($this->summary);
$left = 200;
$right = 30;
$top = 60;
$bottom = 80;
$width = 640;
$height = $top + $bottom + ($this->bar_count * 30 );
$x_unit = $this->bar_count / ($height - $top - $bottom);
$y_unit = $max_y / ($width - $left - $right);
$csim_targets = array();
$csim_alts = array();
$bar_data = array();
$max_days = 0;
foreach ($this->crew_order as $i => $crew_id) {
$csim_targets[$i] = url_for('units/index?crew_id='.$crew_id);
$csim_alts[$i] = sprintf("Units for %s",
$this->crews[$crew_id]->getCrew());
# figure out the maximum y value
$nextDayUnitsList = $this->crews[$crew_id]->getNextDayUnitsList();
$units_array[$crew_id] = $nextDayUnitsList;
if (count($nextDayUnitsList) > $this->max_days) {
$this->max_days = count($nextDayUnitsList);
};
};
$bg_values = array_values($nextDayValues);
foreach ($this->summary as $summary) {
array_push ($bg_values, $summary['value']);
};
$bg_bar = new BarPlot($bg_values);
$bg_bar->SetCSIMTargets($csim_targets, $csim_alts);
$bg_bar->SetNoFill(true);
$fg_bars = $this->getFgBars($units_array);
$fg_bar = new AccBarPlot($fg_bars);
$fg_bar->SetFillColor('black');
# initialize the graph
$graph = new Graph($width, $height, 'auto');
$graph->SetScale('textlin', 0, $max_y, 0, $this->bar_count);
$graph->Set90AndMargin($left, $right, $top, $bottom);
$graph->SetMarginColor('white');
$graph->SetFrame(false);
$graph->Add($fg_bar);
$graph->Add($bg_bar);
# add text labels
foreach ($this->crew_order as $i => $crew_id) {
$label = $this->value_label(
$nextDayValueLabels[$crew_id],
$i, $nextDayValues[$crew_id],
10 * $x_unit, 5 * $y_unit
);
$graph->AddText($label);
};
foreach ($this->summary as $i => $summary) {
$label = $this->value_label(
$summary['value'],
$i, $summary['value'],
10 * $x_unit, 5 * $y_unit
);
$graph->AddText($label);
};
# add title
$graph->title->Set(sprintf("%s - %s", $report->getName(),
$section->getName()));
$graph->title->SetFont(FF_VERDANA,FS_BOLD, 12);
$graph->title->SetMargin(10);
# add subtitle
$graph->subtitle->Set(date('d-M-Y g:ia'));
$graph->subtitle->SetFont(FF_VERDANA,FS_BOLD, 8);
# configure x-axis
$graph->xaxis->SetFont(FF_VERDANA, FS_NORMAL, 8);
$graph->xaxis->SetLabelAlign('right', 'center');
$graph->xaxis->SetLabelFormatCallback(array($this, 'x_axis_label'));
$graph->xaxis->scale->ticks->Set(1, 0);
# configure y-axis
$graph->yaxis->SetFont(FF_VERDANA, FS_NORMAL, 8);
$graph->yaxis->SetLabelAlign('center', 'top');
$graph->yaxis->SetLabelAngle(45);
$graph->yaxis->SetLabelFormatCallback(array($this, 'y_axis_label'));
$graph->yaxis->SetPos('max');
$graph->yaxis->SetLabelSide(SIDE_RIGHT);
$graph->yaxis->SetTickSide(SIDE_LEFT);
if (max($nextDayValues) > 28) {
$graph->yaxis->scale->ticks->Set(7, 1);
} else {
$graph->yaxis->scale->ticks->Set(1, 1);
};
# configure legend
$graph->legend->SetAbsPos(5, $height - 5, "left", "bottom");
$graph->legend->SetColumns(count($this->legend));
$graph->legend->SetFillColor('white');
$graph->legend->SetShadow(false);
$graph->SetImgFormat('png');
return $graph;
}
private function getFgBars($units_array) {
# initialize fg_bar data
$empty_crews = array_fill_keys(array_keys($this->crew_order),0);
# add segment bars
foreach ($this->summary as $summary) {
$empty_crews[] = 0;
};
$empty_segment = array();
foreach (array_keys($this->legend_colors) as $status) {
$empty_segment[$status] = $empty_crews;
};
$segments = array();
for ($day = 0; $day < $this->max_days; $day++) {
foreach (array_keys($empty_segment) as $status) {
$segment = $empty_segment;
foreach ($this->crew_order as $i => $crew_id) {
$nextDayUnitsList = $units_array[$crew_id];
if ($day + 1 < count($nextDayUnitsList)) {
$units = $nextDayUnitsList[$day];
$units_status = $units->getNextDayStatus();
$segment[$units_status][$i] = 1;
} elseif ($day + 1 == count($nextDayUnitsList)) {
$units = $nextDayUnitsList[$day];
$units_status = $units->getNextDayStatus();
$avail = $units->getUsedRatio();
$segment[$units_status][$i] = $avail;
} elseif ($day + 1 > count($nextDayUnitsList)) {
$segment[$units_status][$i] = 0;
};
};
};
foreach ($this->summary as $i => $summary) {
$diff = $summary['value'] - $day;
if ($diff >= 1) {
$segment['summary'][$i] = 1;
} elseif ($diff >= 0) {
$segment['summary'][$i] = $diff;
} else {
$segment['summary'][$i] = 0;
}
};
$segments[$day] = $segment;
};
# create legend
$fg_bars = array();
foreach (array_keys($empty_segment) as $status) {
$fg_bar = new BarPlot($empty_crews);
$fg_bar->setFillColor($this->legend_colors[$status]);
if ($status <> 'summary') {
$fg_bar->SetLegend($this->legend[$status]);
};
$fg_bars[] = $fg_bar;
};
# add segments
foreach ($segments as $day => $segment) {
foreach (array_keys($empty_segment) as $status) {
$fg_bar = new BarPlot($segment[$status]);
$fg_bar->setColor($this->legend_colors[$status]);
$fg_bar->setFillColor($this->legend_colors[$status]);
$fg_bars[] = $fg_bar;
};
};
return $fg_bars;
}
?>
Now the same graph on the new server using JpGraph 2.3.5 or JpGraph 3.5.0b1
A few problems can be seen:
Colours are not applied
x/y coord are offset (margins?)
scale is off
scale ticks are also removed
I am aware that to correct the colour you place $graph->Add($x); before modifying the properties. As such I had to move the code within the getFgBars() into Graph().
Which is documented in the manual under /docs/chunkhtml/ch29s02.html
With revising the code I broke apart the FgBars into $fg_bars[] & $lg_bars[] being the foreground data (color) & Legend bar.
If I remove the legend bar, the graph is displayed as intended.
Question:
What would be causing the margin / scale to go wonkey when adding on a second AccBarPlot()?
Solution:
Remove default theme. $graph->graph_theme = null; will restore both colour & margins without requiring to rewrite any code.
# initialize the graph
$graph = new Graph($width, $height, 'auto');
$graph->SetScale('textlin', 0, $max_y, 0, $this->bar_count);
$graph->Set90AndMargin($left, $right, $top, $bottom);
$graph->SetMarginColor('white');
$graph->SetFrame(false);
$graph->graph_theme = null;
Solution:
Remove default theme. $graph->graph_theme = null; after initalizing Graph() will restore both colour & margins without requiring to rewrite any code.
# initialize the graph
$graph = new Graph($width, $height, 'auto');
$graph->SetScale('textlin', 0, $max_y, 0, $this->bar_count);
$graph->Set90AndMargin($left, $right, $top, $bottom);
$graph->SetMarginColor('white');
$graph->SetFrame(false);
$graph->graph_theme = null;
Resources:
/docs/chunkhtml/ch29.html#id2619634 - The order of SetTheme() and changing settings
/docs/chunkhtml/ch29s02.html - Changing the display settings of line/bar graphs
I have a container rectangle and element rectangle, both are specified only in height and width. Using PHP I need to figure out what positions and orientation would allow for the most possible blits of the element within the container having absolutely no overlap.
An Example :
$container = array(5000,2000);
$element = array(300,200);
The output should be an array of "blit" arrays (or objects) like so
$blit_object = array($x,$y,$rotation_degrees);
Well, since no one is answering me, I am just going to show my "trial and error" solution in case anyone else ever needs it.
It calculates the two basic layout options and sees which one has the most and uses that.
function MakeBlits($container,$element)
{
$container_x = $container[0];
$container_y = $container[1];
$options = array();
for($i = 0;$i<=1;$i++)
{
if($i == 0)
{
$element_x = $element[0];
$element_y = $element[1];
}
else
{
$element_x = $element[1];
$element_y = $element[0];
}
$test_x = floor($container_x/$element_x);
$test_y = floor($container_y/$element_y);
$test_remainder_x = $container_x - $test_x*$element_x;
$test_remainder_y = $container_y - $test_y*$element_y;
$test_x2 = 0;
$test_x3 = 0;
$test_y2 = 0;
$test_y3 = 0;
if($test_remainder_x > $element_y)
{
$test_x2 = floor($test_remainder_x/$element_y);
$test_y2 = floor($container_y/$element_x);
}
if($test_remainder_y > $element_x)
{
$test_x3 = floor($container_x/$element_y);
$test_y3 = floor($test_remainder_y/$element_x);
}
$options[] = array(
'total'=>($test_x*$test_y)+($test_x2*$test_y2)+($test_x3*$test_y3),
'x'=>$test_x,
'y'=>$test_y,
'x2'=>$test_x2,
'y2'=>$test_y2,
'x3'=>$test_x3,
'y3'=>$test_y3
);
}
if($options[0]['total']>=$options[1]['total'])
{
$option = $options[0];
$rotate = 0;
$width = $element[0];
$height = $element[1];
}
else
{
$option = $options[1];
$rotate = -90;
$width = $element[1];
$height = $element[0];
}
$blit_objects = array();
for($i=0;$i<$option['x'];$i++)
{
for($j=0;$j<$option['y'];$j++)
{
$blit_objects[] = array(
'x'=>$i*$width,
'y'=>$j*$height,
'rotation'=>$rotate);
}
}
for($k = 0;$k < $option['x2'];$k++)
{
for($l = 0;$l < $option['y2'];$l++)
{
$blit_objects[] = array(
'x'=>$i*$width + $k*$height,
'y'=>$l*$width,
'rotation'=>$rotate+90);
}
}
for($k = 0;$k < $option['x3'];$k++)
{
for($l = 0;$l < $option['y3'];$l++)
{
$blit_objects[] = array(
'x'=>$k*$height,
'y'=>$j*$height+$l*$width,
'rotation'=>$rotate+90);
}
}
return $blit_objects;
}
I am trying to add arrowheads to my radar/spider plot coded in Canvas. Because I use PHP/SQL to retrieve some database values I echo most of the needed JS to plot it on the Canvas. So far I have drawn the axis (7) and guidelines (5 values).
How can I rotate the arrowheads so they show up correctly; with an angle and at the end of the 7 axis?
function drawArrows(context){
context.beginPath();
context.strokeStyle = "#ccc";
context.lineWidth = 2;
context.save();
<?php
$arrowHoek = 35;
$cHoek = (360/7);
$arrowLength = 10;
for ($i = 1 ; $i < 8 ; $i++) {
$arrow_xleft = round((getCoordinates($i,6,x))-(sin(deg2rad($arrowHoek))*$arrowLength),2);
$arrow_yleft = round((getCoordinates($i,6,y))+(cos(deg2rad($arrowHoek))*$arrowLength),2);
$arrow_xright = round((getCoordinates($i,6,x))+(sin(deg2rad($arrowHoek))*$arrowLength),2);
$arrow_yright = $arrow_yleft;
$arrow_rotation = deg2rad(($cHoek*$i)-$cHoek);
echo "\tcontext.moveTo(";
getCoordinates($i,6,null);
echo ");\n";
echo "\tcontext.lineTo($arrow_xleft, $arrow_yleft);";
echo "\n\tcontext.moveTo(";
getCoordinates($i,6,null);
echo ");\n";
echo "\tcontext.lineTo($arrow_xright, $arrow_yright);\n";
echo "\tcontext.rotate($arrow_rotation);\n";
echo "\tcontext.restore();\n";
echo "\tcontext.save();\n";
}
?>
context.stroke();
}
Seems I worked it out! As found in other topics this uses the save(), translate(), rotate() and restore() functions. If you put this in a seperate Javascript function you can make transformations to individual Canvas parts without messing up any other drawings.
Below is the solution (note this uses PHP to echo the Javascript function).
function drawArrows(context){
context.beginPath();
context.strokeStyle = "#ccc";
context.lineWidth = 2;
<?php
$arrowHoek = 35;
$cHoek = (360/7);
$arrowLength = 10;
for ($i = 1 ; $i < 8 ; $i++) {
$arrow_xleft = -(sin(deg2rad($arrowHoek))*$arrowLength);
$arrow_yleft = (cos(deg2rad($arrowHoek))*$arrowLength);
$arrow_xright = (sin(deg2rad($arrowHoek))*$arrowLength);
$arrow_yright = $arrow_yleft;
$arrow_rotation = deg2rad(($cHoek*$i)-$cHoek);
echo "\tcontext.save();\n";
echo "\tcontext.translate(";
getCoordinates($i,6,null);
echo ");\n";
echo "\tcontext.rotate($arrow_rotation);\n";
echo "\tcontext.moveTo($arrow_xleft, $arrow_yleft);";
echo "\n\tcontext.lineTo(0,0);\n";
echo "\tcontext.lineTo($arrow_xright, $arrow_yright);\n";
echo "\tcontext.restore();\n";
}
?>
context.stroke();
}
Note: The declaration of the context and canvas elements are in a container function.