sandy1028 Posted December 3, 2007 Share Posted December 3, 2007 $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 Quote Link to comment Share on other sites More sharing options...
sKunKbad Posted December 3, 2007 Share Posted December 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 3, 2007 Share Posted December 3, 2007 I think you just set the color ect. one time and thats why everything is black. Each time you go through your loop you assign values to $lineplot.... Aren't you overwriting your $lineplot all the time? Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted December 3, 2007 Author Share Posted December 3, 2007 In the array value[], the values onlu which are zero I should mark as balck. But now all the values are taking as balck. I am not undersatnding why it is taking as balck. If I use foreach($value as $val) also it takes black for all the values Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted December 3, 2007 Author Share Posted December 3, 2007 hi, The black is taken even for the value greater than zero as in the attached figure How can the problem be solved [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 3, 2007 Share Posted December 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted December 3, 2007 Author Share Posted December 3, 2007 But when I plot the graph the color is getting plotted for all the values in the array. Do you have any idea why it is plotting black circle. I have attached the image [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 3, 2007 Share Posted December 3, 2007 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); Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted December 4, 2007 Author Share Posted December 4, 2007 <?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); ?> Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 4, 2007 Share Posted December 4, 2007 Does $lineplot hold an array with plots? (Value, Type, Color ect.) If it does try to output / print that array using print_r() and tell us about the result. This should show what is going on.... Quote Link to comment Share on other sites More sharing options...
sandy1028 Posted December 4, 2007 Author Share Posted December 4, 2007 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.