Jump to content

Recommended Posts

Hi All,

 

I am currently using jpgraph on a site, and I'm trying to get the (x,y) coordinates an a chart to have two different colors ... (x = green, y = red)

 

You can see an example here:

http://entropylinks.com/testgraph-dev.php?energy=2.2&entropy=1.75

 

Here is my code:

<?php // content="text/plain; charset=utf-8"
require_once ("graph/src/jpgraph.php");
require_once ("graph/src/jpgraph_scatter.php");
DEFINE('MATRIX','images/matrix-big-2.png');
$datax = array(0,3);
$datay = array(0,3);
if(isset($_SESSION['counter']) && $_SESSION['counter'] > 1){	
$x = 1.98;
$y = 1.2;
$count = $_SESSION['counter'];
	for($i = 0; $i < $count; $i++){
		$datax[] += $_SESSION["entropy_$i"];
		$datay[] += $_SESSION["energy_$i"];
	}
}
	else
	{
		$datax = explode('-', $_GET['energy']);
		$datay = explode('-', $_GET['entropy']);
	} 

$idx=0;
function xyCallback($yval) {
  global $datay, $idx;
  return "($yval-".$datay[$idx++].")";
}

$graph = new Graph(550,550);
$graph->SetScale("intint",0,3,0,3);
$graph->img->SetMargin(40,40,40,40);        
$graph->SetBackgroundImage(MATRIX,BGIMG_FILLPLOT);
$graph->title->SetFont(FF_ARIAL,FS_BOLD);
$graph->xaxis->SetTitle('CORPORATE ENTROPY','middle'); 
$graph->yaxis->SetTitle('CORPORATE ENERGY','middle'); 
$graph->yaxis->SetTextTickInterval(1,2);

$sp1 = new ScatterPlot($datax,$datay);
$sp1->mark->SetSize(10); 
$sp1->mark->SetFillColor("darkblue");
$sp1->mark->SetType(MARK_FILLEDCIRCLE);

$sp1->value->SetMargin(17);

$sp1->value->SetFormatCallback('xyCallback');
$sp1->value->SetColor("darkgreen");

$sp1->value->SetFont(FF_FONT0,FS_NORMAL);

$sp1->value->Show();


$txt = new Text("Low"); // store text
$txt->SetFont(FF_FONT0,FS_NORMAL); 
$txt->SetScalePos(0.5,0);
$txt->SetColor('darkblue'); 
$txt->Center(50,150,75); 
$graph->AddText( $txt); // write to position



$txt = new Text("Medium"); // store text
$txt->SetFont(FF_FONT0,FS_NORMAL); 
$txt->SetScalePos(1.5,0);
$txt->SetColor('darkblue'); 
$txt->Center(50,150,75); 
$graph->AddText( $txt); // write to position



$txt = new Text("High"); // store text
$txt->SetFont(FF_FONT0,FS_NORMAL); 
$txt->SetScalePos(2.5,0);
$txt->SetColor('darkblue'); 
$txt->Center(50,150,75); 
$graph->AddText( $txt); // write to position




$txt = new Text("Low"); // store text
$txt->SetFont(FF_FONT0,FS_NORMAL); 
$txt->SetScalePos(-0.03,0.5);
$txt->SetAngle(90); 
$txt->SetColor('darkblue'); 
$txt->Center(50,150,75); 
$graph->AddText( $txt); // write to position



$txt = new Text("Medium"); // store text
$txt->SetFont(FF_FONT0,FS_NORMAL); 
$txt->SetScalePos(-0.03,1.5);
$txt->SetAngle(90); 
$txt->SetColor('darkblue'); 
$txt->Center(50,150,75); 
$graph->AddText( $txt); // write to position



$txt = new Text("High"); // store text
$txt->SetFont(FF_FONT0,FS_NORMAL); 
$txt->SetScalePos(-0.03,2.5);
$txt->SetAngle(90); 
$txt->SetColor('darkblue'); 
$txt->Center(50,150,75); 
$graph->AddText( $txt); // write to position

$graph->Add($sp1);

$graph->Stroke(); 
?>

 

Here's the class code that controls the scatter graph

//===================================================
// CLASS ScatterPlot
// Description: Render X and Y plots
//===================================================
class ScatterPlot extends Plot {
    public $mark,$link;
    private $impuls = false;
    //---------------
    // CONSTRUCTOR
    function __construct($datay,$datax=false) {
        if( (count($datax) != count($datay)) && is_array($datax)) {
        	JpGraphError::RaiseL(20003);//("Scatterplot must have equal number of X and Y points.");
        }
        parent::__construct($datay,$datax);
        $this->mark = new PlotMark();
        $this->mark->SetType(MARK_SQUARE);
        $this->mark->SetColor($this->color);
        $this->value->SetAlign('center','center');
        $this->value->SetMargin(0);
        $this->link = new LineProperty(1,'black','solid');
        $this->link->iShow = false;
    }

    //---------------
    // PUBLIC METHODS
    function SetImpuls($f=true) {
        $this->impuls = $f;
    }

    function SetStem($f=true) {
        $this->impuls = $f;
    }

    // Combine the scatter plot points with a line
    function SetLinkPoints($aFlag=true,$aColor="black",$aWeight=1,$aStyle='solid') {
    	$this->link->iShow = $aFlag;
    	$this->link->iColor = $aColor;
    	$this->link->iWeight = $aWeight;
    	$this->link->iStyle = $aStyle;
    }

    function Stroke($img,$xscale,$yscale) {

        $ymin=$yscale->scale_abs[0];
        if( $yscale->scale[0] < 0 )
        $yzero=$yscale->Translate(0);
        else
        $yzero=$yscale->scale_abs[0];

        $this->csimareas = '';
        for( $i=0; $i<$this->numpoints; ++$i ) {

            // Skip null values
            if( $this->coords[0][$i]==='' || $this->coords[0][$i]==='-' || $this->coords[0][$i]==='x')
            continue;

            if( isset($this->coords[1]) )
            $xt = $xscale->Translate($this->coords[1][$i]);
            else
            $xt = $xscale->Translate($i);
            $yt = $yscale->Translate($this->coords[0][$i]);


            if( $this->link->iShow && isset($yt_old) ) {
                $img->SetColor($this->link->iColor);
                $img->SetLineWeight($this->link->iWeight);
                $old = $img->SetLineStyle($this->link->iStyle);
                $img->StyleLine($xt_old,$yt_old,$xt,$yt);
                $img->SetLineStyle($old);
            }

            if( $this->impuls ) {
                $img->SetColor($this->color);
                $img->SetLineWeight($this->weight);
                $img->Line($xt,$yzero,$xt,$yt);
            }

            if( !empty($this->csimtargets[$i]) ) {
                if( !empty($this->csimwintargets[$i]) ) {
                    $this->mark->SetCSIMTarget($this->csimtargets[$i],$this->csimwintargets[$i]);
                }
                else {
                    $this->mark->SetCSIMTarget($this->csimtargets[$i]);
                }
                $this->mark->SetCSIMAlt($this->csimalts[$i]);
            }

            if( isset($this->coords[1]) ) {
                $this->mark->SetCSIMAltVal($this->coords[0][$i],$this->coords[1][$i]);
            }
            else {
                $this->mark->SetCSIMAltVal($this->coords[0][$i],$i);
            }

            $this->mark->Stroke($img,$xt,$yt);

            $this->csimareas .= $this->mark->GetCSIMAreas();
            $this->value->Stroke($img,$this->coords[0][$i],$xt,$yt);

            $xt_old = $xt;
            $yt_old = $yt;
        }
    }

    // Framework function
    function Legend($aGraph) {
        if( $this->legend != "" ) {
            $aGraph->legend->Add($this->legend,$this->mark->fill_color,$this->mark,0,
            $this->legendcsimtarget,$this->legendcsimalt,$this->legendcsimwintarget);
        }
    }
} // Class

 

Does anyone have a solution for this?

 

Is it even possible?

 

Do I need to somehow modify the class to render these different colors?

 

Feel free to ask any questions you may have, I'm 99% complete with this, but I just can't seem to change the colors of the plot labels.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/231581-jpgraph-plot-label-color/
Share on other sites

I don't quite understand the question, but I'll try and help anyway (:

 

From what I gather you just want simply a way to calculate a different shade of red, and a different shade of green, for each level of energy/entropy..

 

ok!

 

so heres how we'd go about doing this!

<?php
$levelMax = 256; // highest shade of any color in the RGB spectrum(?)

/* change these whenever you want to */
$x = 1.8;
$y = 1.2;
$max = 3;
/* ^^ those are the editable things lol */

$red = ($x / $max) * $levelMax; // tiered red
$gre = ($y / $max) * $levelMax; // tiered green

$color = imagecolorallocate($image,$red,$gre,0 /* blue change to whatever */);
?>

 

Hi Russell,

 

The question is this ... For each point on the graph, I have the coordinates (x,y)

 

I use this callback function:

function xyCallback($yval) {
  global $datay, $idx;
  return "($yval-".$datay[$idx++].")";
}

 

to generate the label for (x,y) based on the values of (x,y) ... So in the example, the label is (2.25 - 1.75) where x = 2.25 and y = 1.75 ...

 

There are several points on this graph, but I only used one example ... What I need is to render "2.25" in Green, and "1.75" in red ... I'm not sure it's possible, but I'm trying to make it happen ... With no luck, of course.

 

Hope this helps!

use imageftbbox with '(' then '2.25' then '-' then '1.75' then ')' and place them mathematically, I gotta run to the eye doctors so I can't do this for ya, but ^^ thats the way you would do it, the 1 string '(2.25-1.75)' would be output with four calls to imagefttext, cya

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.