Jump to content

for loop


sandy1028

Recommended Posts

$value=array(23,56,23,46,3,0,56,34);
for($i=0;$i<count($value);$i++){
if($value[$i] == '0'){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("black");
$lineplot->mark->SetWidth(2);
}
elseif($value[$i] < '0'){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("red");
$lineplot->mark->SetWidth(2);
}

 

 

Please anyone tell me what is the problem with the code.

 

I should mark only the value '0' as black color in Jpgraphs

 

But the whole array is taking black color.

How to loop an array to take only value 0 as black circle

 

Link to comment
https://forums.phpfreaks.com/topic/79940-for-loop/
Share on other sites

try foreach:

 

$value=array(23,56,23,46,3,0,56,34);
foreach ($value as $x){
if($x == '0'){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("black");
$lineplot->mark->SetWidth(2);
}elseif($x < '0'){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("red");
$lineplot->mark->SetWidth(2);
}
}

um, i don't know if that is what you are after

Link to comment
https://forums.phpfreaks.com/topic/79940-for-loop/#findComment-404903
Share on other sites

The only problem with your code is the way you assign colors to your values ($lineplot->mark->) because your loop functions properly detecting values of 0 and <0 perfectly. This this (which is your code with an echo statement added):

 

<?php
$value=array(23,56,23,46,3,0,56,34,-3,-13);

for($i=0;$i<count($value);$i++){

if($value[$i] == '0'){

	//Echo the value
	echo $value[$i] . " black<br>";
}
elseif($value[$i] < '0'){

	//Echo the value
	echo $value[$i] . " red<br>";
}
}
?>

 

This outputs values and colors as expected. So tell us more about how you assign the colors.

Link to comment
https://forums.phpfreaks.com/topic/79940-for-loop/#findComment-404949
Share on other sites

Post some more code because right now it seems like you only assign the color black once and then  use it throughout the creation of your circles:

 

$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("black");
$lineplot->mark->SetWidth(2);

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/79940-for-loop/#findComment-404968
Share on other sites

<?php
include ("/var/www/jpgraph-1.19/jpgraph.php");
include ("/var/www/jpgraph-1.19/jpgraph_line.php");

$value=array(23,56,34,23,54,0,23,-34,24);
$max=max($value);


$graph = new Graph(720,200,"auto");
$graph->SetScale("textlin",0,$max);
$graph->img->SetMargin(40,40,40,40);
$lineplot = new LinePlot($value);
$lineplot->SetColor("red");

foreach($value as $val){
if($val == '0'){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("black");
$lineplot->mark->SetWidth(2);
}
elseif($val < '0'){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("red");
$lineplot->mark->SetWidth(2);
}
}
$graph->Add($lineplot);
?>

Link to comment
https://forums.phpfreaks.com/topic/79940-for-loop/#findComment-405784
Share on other sites

include ("/var/www/jpgraph-1.19/jpgraph.php");
include ("/var/www/jpgraph-1.19/jpgraph_line.php");

$value=array();
$value=array(23,34,6,23,56,34,0,3,34,56,3);
$max=max($value);


$graph = new Graph(720,200,"auto");
$graph->SetScale("textlin",0,$max);
$graph->img->SetMargin(40,40,40,40);
$lineplot = new LinePlot($value);
$lineplot->SetColor("red");
$graph->xaxis-> title->Set("Time(hr)--------->" );
$graph->yaxis-> title->Set("%---------------->" );

$graph->title->SetFont(FF_FONT1,FS_BOLD);

$graph->SetMarginColor("red");
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->yaxis->SetLayout();
//$graph->yaxis->Pos(.4,.9,"right","right");
//$graph->legend->Pos(.01,.8,"right","center");
$lineplot->SetLegend("Value Up");
$graph->legend->SetLayout(LEGEND_HOR);
$graph->legend->Pos(.5,.9,"center","right");
$graph->legend->SetFillColor("lemonchiffon2:1.3");
// To set the y axos color and font color*****************/
$graph->yaxis->SetColor("red","black");
$graph->xaxis->SetColor("red","black");

$lineplot->SetWeight(1);
$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->SetTitleMargin(30);
$graph->xaxis->SetTitleMargin(45);
$graph->SetBackgroundGradient('lemonchiffon','white',GRAD_MIDHOR,BGRAD_PLOT);
$graph->SetMarginColor('lemonchiffon2:1.3');
$lineplot->SetFillColor("chocolate1");

$graph->xaxis->scale->ticks->SetColor('black','black');

$graph->SetBox(true,'red',1);


$graph->SetShadow();
$graph->ygrid->SetColor('orange');

/**************** To add the filled circle if the value is 0 i.e if it is down*/

foreach($value as $val){
if($val == 0){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("black");
$lineplot->mark->SetWidth(2);
}
elseif($val < 0){
$lineplot->mark->SetType(MARK_FILLEDCIRCLE);
$lineplot->mark->SetFillColor("red");
$lineplot->mark->SetWidth(2);
}
}

$graph->yaxis->SetTickSide(SIDE_LEFT);
$graph->xaxis->SetTickSide(SIDE_DOWN);

//$graph->xgrid->SetFill();
$graph->xgrid->Show();
$graph->Add($lineplot);
//$graph->Add($lineplot2);
$graph->Stroke();
?>

 

The balck circle is marked for all the values.

But I am not understanding why it is not just plotting for the value which is 0

 

The output is attached figure

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/79940-for-loop/#findComment-405833
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.