Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPGraph Subform and Legend Issues

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I have a JPGraph line plot inside a function and have showed below only the portions in question. The plot is working in pulling up multiple columns of data but I can't seem to get the legend to show properly, especially as there is also a sub form to selest what to graph. By default, all four columns are graphed, then there are checkboxes to pick more specifics. As it is, it shows all properly or I can pick only one at a time without a JPGraph crash (any two or more crashes it) but the main problem is in the legend, which has to get a title from a database table, shows all the titles for each column and it's obvious why but I'm not sure what to do about it! It's also showing all the same title so if two are being graphed, and there are four columns of data, I see the same file name eight times and it does not show the mark type.

I would also like it to show the column name at the peak of each graph.

Can anyone help?

Code:
if ($result = $mysqli->query("$Query")) :
	while ($row = $result->fetch_array()) :
		$datax[$row[0]][] = $row[1];
		$datay1[$row[0]][] = $row[2];
		$datay2[$row[0]][] = $row[3];
		$datay3[$row[0]][] = $row[4];
		$datay4[$row[0]][] = $row[5];
	endwhile;
	$result->close();
else:
	echo "<div class=\"Message\">\n";
	printf("MySQLi Error: %s\n", $mysqli->error);
	echo "</div>\n\n";
endif;

foreach($datay1 as $key=>$_datay): // for Legend
	$queryFN = "SELECT FileName FROM dcs_uploads ";
	$queryFN .= "WHERE ID = ".$key." ";
	$queryFN .= "ORDER by FileName";
	if ($result = $mysqli->query($queryFN)):
		$row = $result->fetch_row();
		$FileName = $row[0];
	endif;
endforeach;

if (!$SubData || $SubData == 'Red'):
	foreach($datay1 as $key=>$_datay): // for Red
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->mark->SetType(MARK_DIAMOND, 'red', 0.3);				
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->SetLegend(basename($FileName, ".csv"));
	endforeach;
endif;

if (!$SubData || $SubData == 'Gr'):
	foreach($datay2 as $key=>$_datay): // for Gr
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->mark->SetType(MARK_UTRIANGLE, 'green', 0.3);	
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->SetLegend(basename($FileName, ".csv"));
	endforeach;
endif;

if (!$SubData || $SubData == 'Gb'):
	foreach($datay3 as $key=>$_datay): // for Gb
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->mark->SetType(MARK_DTRIANGLE, 'teal', 0.3);	
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->SetLegend(basename($FileName, ".csv"));
	endforeach;
endif;

if (!$SubData || $SubData == 'B'):
	foreach($datay4 as $key=>$_datay): // for B
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->mark->SetType(MARK_SQUARE, 'blue', 0.3);	
		$lp[$key] = new LinePlot($_datay,$datax[$key]);
		$graph->Add($lp[$key]);
		$lp[$key]->SetLegend(basename($FileName, ".csv"));
	endforeach;
endif;
 
What I need right now is to know how to do the coding - the rest is political and not really my concern even though it is indeed a valid one. I was asked to add the free-form query window in the couple days I have left so I did. I added the filters on my own as a precaution. I am confident that it's safe enough for the couple people who will be using it on the internal network (it's not a public site) and it is not linked anywhere so knowing the URL is the only way to get to it. Can you advise on the coding?
 
i can provided I can see an example of the output data and its shape. otherwise i don't see how.
 
The data and shape can be from a couple columns to many columns, which is the primary point of this odd plotting excercise, but I am providing the user with a "starter query" in the form field that has the FileName and FileID as a beginning. In this example, Bin is the X, the other columns after it are Y. A structure like this can be expected but the number of columns and their names can vary. The plot, which is in a function, will be getting the $Query as a variable:

Code:
SELECT u.FileName, 
p.FileID, 
p.Bin, 
p.GreenR, 
p.Red,
p.Blue, 
p.GreenB, 
p.RGr, 
p.BGb, 
p.Total 
FROM dcs_uploads u, dcs_probe p 
WHERE EntryType = 'Probe' 
AND u.ID = p.FileID
AND Validity = 1 

	<< Your joins and additional WHERE clauses go here >>

ORDER BY p.FileID, p.Bin
 
using the query and iterator that was posted above something like this should work.

Nig health warning though: not tested at all.

Code:
[COLOR=#990000]<?php[/color]
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]wrapLegend[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$legend[/color][COLOR=#990000])[/color][COLOR=#FF0000]{[/color]
[tab][b][COLOR=#0000FF]return[/color][/b] [b][COLOR=#000000]wordwrap[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$legend[/color][COLOR=#990000],[/color] [COLOR=#993399]40[/color][COLOR=#990000]);[/color] [i][COLOR=#9A1900]//change the number as you need[/color][/i]
[COLOR=#FF0000]}[/color]
[b][COLOR=#0000FF]function[/color][/b] [b][COLOR=#000000]getColor[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$type[/color][COLOR=#990000],[/color] [COLOR=#009900]$increment[/color][COLOR=#990000])[/color] [COLOR=#FF0000]{[/color]
[tab][b][COLOR=#0000FF]static[/color][/b] [COLOR=#009900]$red[/color] [COLOR=#990000]=[/color] [COLOR=#993399]255[/color][COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]static[/color][/b] [COLOR=#009900]$green[/color] [COLOR=#990000]=[/color] [COLOR=#993399]255[/color][COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]static[/color][/b] [COLOR=#009900]$blue[/color] [COLOR=#990000]=[/color] [COLOR=#993399]255[/color][COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color]$[COLOR=#FF0000]{[/color][COLOR=#009900]$type[/color][COLOR=#FF0000]}[/color] [COLOR=#990000]<[/color] [COLOR=#993399]0[/color][COLOR=#990000])[/color] $[COLOR=#FF0000]{[/color][COLOR=#009900]$type[/color][COLOR=#FF0000]}[/color] [COLOR=#990000]=[/color] [COLOR=#993399]255[/color][COLOR=#990000];[/color]
[tab][COLOR=#009900]$increment[/color] [COLOR=#990000]=[/color] [COLOR=#990000](![/color][COLOR=#009900]$increment[/color][COLOR=#990000])[/color] [COLOR=#990000]?[/color] [COLOR=#993399]30[/color][COLOR=#990000]:[/color] [COLOR=#009900]$increment[/color][COLOR=#990000];[/color]
[tab][COLOR=#009900]$format[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$type[/color] [COLOR=#990000]==[/color] [COLOR=#FF0000]'red'[/color] [COLOR=#990000]?[/color] [COLOR=#FF0000]'#%02s0000'[/color] [COLOR=#990000]:[/color] [COLOR=#990000]([/color][COLOR=#009900]$type[/color] [COLOR=#990000]==[/color] [COLOR=#FF0000]'green'[/color] [COLOR=#990000]?[/color] [COLOR=#FF0000]'#00%02s00'[/color] [COLOR=#990000]:[/color] [COLOR=#FF0000]'#0000%02s'[/color][COLOR=#990000]);[/color]
[tab][COLOR=#009900]$col[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]sprintf[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$format[/color][COLOR=#990000],[/color] [b][COLOR=#000000]dechex[/color][/b][COLOR=#990000]([/color]$[COLOR=#FF0000]{[/color][COLOR=#009900]$type[/color][COLOR=#FF0000]}[/color][COLOR=#990000])[/color] [COLOR=#990000]);[/color] 
[tab]$[COLOR=#FF0000]{[/color][COLOR=#009900]$type[/color][COLOR=#FF0000]}[/color] [COLOR=#990000]=[/color] $[COLOR=#FF0000]{[/color][COLOR=#009900]$type[/color][COLOR=#FF0000]}[/color] [COLOR=#990000]-[/color] [COLOR=#009900]$increment[/color][COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]return[/color][/b] [b][COLOR=#000000]str_replace[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]' '[/color][COLOR=#990000],[/color][COLOR=#FF0000]''[/color][COLOR=#990000],[/color][COLOR=#009900]$col[/color][COLOR=#990000]);[/color]
[COLOR=#FF0000]}[/color] 

[b][COLOR=#000080]require_once[/color][/b] [COLOR=#FF0000]'src/jpgraph.php'[/color][COLOR=#990000];[/color]
[b][COLOR=#000080]require_once[/color][/b] [COLOR=#FF0000]'src/jpgraph_line.php'[/color][COLOR=#990000];[/color]
[b][COLOR=#000080]require_once[/color][/b] [COLOR=#FF0000]'src/jpgraph_scatter.php'[/color][COLOR=#990000];[/color]
[COLOR=#009900]$graph[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b] [b][COLOR=#000000]graph[/color][/b][COLOR=#990000]([/color][COLOR=#993399]800[/color][COLOR=#990000],[/color][COLOR=#993399]800[/color][COLOR=#990000]);[/color]
[COLOR=#009900]$graph[/color][COLOR=#990000]->[/color][b][COLOR=#000000]setScale[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'lin'[/color][COLOR=#990000]);[/color]
[COLOR=#009900]$marks[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color][tab] MARK_SQUARE[COLOR=#990000],[/color] 
[tab][tab][tab][tab][tab]MARK_X[COLOR=#990000],[/color]
[tab][tab][tab][tab][tab]MARK_DIAMOND[COLOR=#990000],[/color] 
[tab][tab][tab][tab][tab]MARK_CIRCLE[COLOR=#990000],[/color] 
[tab][tab][tab][tab][tab]MARK_STAR
[tab][tab][tab][tab][COLOR=#990000]);[/color]
[i][COLOR=#9A1900]//use this for the colour types distinguished by datayX[/color][/i]
[COLOR=#009900]$types[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color][tab] [COLOR=#FF0000]'red'[/color][COLOR=#990000],[/color]
[tab][tab][tab][tab][tab][COLOR=#FF0000]'green'[/color][COLOR=#990000],[/color]
[tab][tab][tab][tab][tab][COLOR=#FF0000]'something'[/color][COLOR=#990000],[/color] [i][COLOR=#9A1900]//replace with something meaningful[/color][/i]
[tab][tab][tab][tab][tab][COLOR=#FF0000]'blue'[/color]
[tab][tab][tab][COLOR=#990000]);[/color]
[COLOR=#009900]$colours[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color] 
[tab][tab][tab][tab][tab][COLOR=#FF0000]'red'[/color][COLOR=#990000]=>[/color][b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'darkred'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'indianred'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'lightred'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'mediumred'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'orangered'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'palevioletred'[/color][COLOR=#990000]),[/color] 
[tab][tab][tab][tab][tab][COLOR=#FF0000]'green'[/color][COLOR=#990000]=>[/color][b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'darkgreen'[/color][COLOR=#990000],[/color] [COLOR=#FF0000]'darkseagreen'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'forestgreen'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'green'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'lightseagreen'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'palegreen3'[/color][COLOR=#990000]),[/color]
[tab][tab][tab][tab][tab][COLOR=#FF0000]'something'[/color][COLOR=#990000]=>[/color][b][COLOR=#0000FF]array[/color][/b][COLOR=#990000](),[/color]
[tab][tab][tab][tab][tab][COLOR=#FF0000]'blue'[/color][COLOR=#990000]=>[/color][b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'blue'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'cadetblue'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'cornflowerblue'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'darkblue'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'darkslateblue'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'dodgerblue'[/color][COLOR=#990000])[/color]
[tab][tab][tab][tab][tab][COLOR=#990000]);[/color]
[COLOR=#009900]$datax[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]();[/color]
[COLOR=#009900]$datay[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]();[/color]
[COLOR=#009900]$fileNames[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]();[/color]
[b][COLOR=#0000FF]while[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$row[/color] [COLOR=#990000]=[/color] [COLOR=#009900]$result[/color][COLOR=#990000]->[/color][b][COLOR=#000000]fetch_array[/color][/b][COLOR=#990000]()):[/color]
[tab][COLOR=#009900]$numCols[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]count[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$row[/color][COLOR=#990000]);[/color]
[tab][COLOR=#009900]$fileNames[/color][COLOR=#990000][[/color][COLOR=#009900]$fileID[/color][COLOR=#990000]][/color] [COLOR=#990000]=[/color] [COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#993399]0[/color][COLOR=#990000]];[/color]
[tab][COLOR=#009900]$datax[/color][COLOR=#990000][[/color][COLOR=#009900]$fileID[/color][COLOR=#990000]][][/color] [COLOR=#990000]=[/color] [COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#993399]1[/color][COLOR=#990000]];[/color]
[tab][b][COLOR=#0000FF]for[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$col[/color][COLOR=#990000]=[/color][COLOR=#993399]0[/color][COLOR=#990000];[/color] [COLOR=#009900]$col[/color] [COLOR=#990000]<[/color] [COLOR=#009900]$numCols[/color] [COLOR=#990000]-[/color] [COLOR=#993399]3[/color][COLOR=#990000];[/color] [COLOR=#009900]$col[/color][COLOR=#990000]++):[/color]
[tab][tab][COLOR=#009900]$datay[/color][COLOR=#990000][[/color][COLOR=#009900]$col[/color][COLOR=#990000]][[/color][COLOR=#009900]$fileID[/color][COLOR=#990000]][][/color] [COLOR=#990000]=[/color] [COLOR=#009900]$row[/color][COLOR=#990000][[/color][COLOR=#009900]$col[/color] [COLOR=#990000]+[/color] [COLOR=#993399]3[/color][COLOR=#990000]];[/color]
[tab][b][COLOR=#0000FF]endfor[/color][/b][COLOR=#990000];[/color]
[b][COLOR=#0000FF]endwhile[/color][/b][COLOR=#990000];[/color]

[COLOR=#009900]$plots[/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]array[/color][/b][COLOR=#990000]();[/color]
[b][COLOR=#0000FF]foreach[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$datay[/color] [b][COLOR=#0000FF]as[/color][/b] [COLOR=#009900]$key[/color][COLOR=#990000]=>[/color][COLOR=#009900]$data[/color][COLOR=#990000]):[/color]

[tab][COLOR=#009900]$mark[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]current[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$marks[/color][COLOR=#990000]);[/color] 
[tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$mark[/color] [COLOR=#990000]===[/color] false[COLOR=#990000])[/color] [COLOR=#009900]$mark[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]reset[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$marks[/color][COLOR=#990000]);[/color]
[tab][b][COLOR=#000000]next[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$marks[/color][COLOR=#990000]);[/color]

[tab][b][COLOR=#0000FF]foreach[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$data[/color] [b][COLOR=#0000FF]as[/color][/b] [COLOR=#009900]$y[/color][COLOR=#990000]):[/color]
[tab][tab][COLOR=#009900]$plots[/color][COLOR=#990000][][/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b] [b][COLOR=#000000]lineplot[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]$y[/color][COLOR=#990000],[/color] [COLOR=#009900]$datax[/color][COLOR=#990000][[/color][COLOR=#009900]$key[/color][COLOR=#990000]]);[/color]
[tab][tab][COLOR=#009900]$lp[/color] [COLOR=#990000]=&[/color] [b][COLOR=#000000]end[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$plots[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$Mark[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]mark[COLOR=#990000]->[/color][b][COLOR=#000000]setType[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$mark[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$colour[/color] [COLOR=#990000]=[/color]  [b][COLOR=#0000FF]isset[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$colours[/color][COLOR=#990000][[/color][COLOR=#009900]$types[/color][COLOR=#990000][[/color][COLOR=#009900]$i[/color][COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000]]][[/color][COLOR=#009900]$i[/color][COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000]])[/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]?[/color] [COLOR=#009900]$colours[/color][COLOR=#990000][[/color][COLOR=#009900]$types[/color][COLOR=#990000][[/color][COLOR=#009900]$i[/color][COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000]]][[/color][COLOR=#009900]$i[/color][COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000]][/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]:[/color] [COLOR=#FF0000]'black'[/color][COLOR=#990000];[/color]  [i][COLOR=#9A1900]//picks a colour[/color][/i]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Fill[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetFillColor[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]$col[/color] [COLOR=#990000].[/color][COLOR=#FF0000]'@0.7'[/color] [COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Weight[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetWeight[/color][/b][COLOR=#990000]([/color][COLOR=#993399]3[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Legend[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetLegend[/color][/b][COLOR=#990000]([/color][b][COLOR=#000000]wraplegend[/color][/b][COLOR=#990000]([/color][b][COLOR=#000000]sprintf[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"%s-%s"[/color][COLOR=#990000],[/color][b][COLOR=#000000]ucfirst[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$types[/color][COLOR=#990000][[/color][COLOR=#009900]$i[/color][COLOR=#990000]-[/color][COLOR=#993399]1[/color][COLOR=#990000]]),[/color][b][COLOR=#000000]basename[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$fileNames[/color][COLOR=#990000][[/color][COLOR=#009900]$key[/color][COLOR=#990000]],[/color] [COLOR=#FF0000]".csv"[/color][COLOR=#990000]))));[/color]
[tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]mark[COLOR=#990000]->[/color][b][COLOR=#000000]setWidth[/color][/b][COLOR=#990000]([/color][COLOR=#993399]3[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetColor[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]$col[/color] [COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Values[/color][COLOR=#990000]):[/color]
[tab][tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]value[COLOR=#990000]->[/color][b][COLOR=#000000]Show[/color][/b][COLOR=#990000]();[/color]
[tab][tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]value[COLOR=#990000]->[/color][b][COLOR=#000000]SetMargin[/color][/b][COLOR=#990000]([/color][COLOR=#993399]0[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]endif[/color][/b][COLOR=#990000];[/color]
[tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetStyle[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'dashed'[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$graph[/color][COLOR=#990000]->[/color][b][COLOR=#000000]Add[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$lp[/color][COLOR=#990000]);[/color]
[tab][b][COLOR=#0000FF]endforeach[/color][/b][COLOR=#990000];[/color]
[b][COLOR=#0000FF]endforeach[/color][/b][COLOR=#990000];[/color]

[COLOR=#009900]$graph[/color][COLOR=#990000]->[/color]legend[COLOR=#990000]->[/color][b][COLOR=#000000]setColumns[/color][/b][COLOR=#990000]([/color][COLOR=#993399]1[/color][COLOR=#990000]);[/color]
[COLOR=#009900]$graph[/color][COLOR=#990000]->[/color]legend[COLOR=#990000]->[/color][b][COLOR=#000000]setAbsPos[/color][/b][COLOR=#990000]([/color][COLOR=#993399]0[/color][COLOR=#990000],[/color][COLOR=#993399]0[/color][COLOR=#990000],[/color][COLOR=#FF0000]'left'[/color][COLOR=#990000],[/color][COLOR=#FF0000]'top'[/color][COLOR=#990000]);[/color]
[COLOR=#009900]$graph[/color][COLOR=#990000]->[/color][b][COLOR=#000000]stroke[/color][/b][COLOR=#990000]();[/color]
[COLOR=#990000]?>[/color]
 
Thank you! It looks good but JPGraph gives an error. I did change the "green" to "lightgreen" and the "something" to "darkgreen" (and simply copied, for now, the same values from lightgreen) after I got the error but it did not help:

Unknown color specification: 5, size=-1
 
my fault. by changing to a foreach loop i lost the $i variable. try this instead

Code:
[b][COLOR=#0000FF]foreach[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$datay[/color] [b][COLOR=#0000FF]as[/color][/b] [COLOR=#009900]$key[/color][COLOR=#990000]=>[/color][COLOR=#009900]$data[/color][COLOR=#990000]):[/color]

[tab][COLOR=#009900]$mark[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]current[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$marks[/color][COLOR=#990000]);[/color] 
[tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$mark[/color] [COLOR=#990000]===[/color] false[COLOR=#990000])[/color] [COLOR=#009900]$mark[/color] [COLOR=#990000]=[/color] [b][COLOR=#000000]reset[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$marks[/color][COLOR=#990000]);[/color]
[tab][b][COLOR=#000000]next[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$marks[/color][COLOR=#990000]);[/color]

[tab][COLOR=#009900]$i[/color][COLOR=#990000]=[/color][COLOR=#993399]0[/color][COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]foreach[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$data[/color] [b][COLOR=#0000FF]as[/color][/b] [COLOR=#009900]$y[/color][COLOR=#990000]):[/color]
[tab][tab][COLOR=#009900]$plots[/color][COLOR=#990000][][/color] [COLOR=#990000]=[/color] [b][COLOR=#0000FF]new[/color][/b] [b][COLOR=#000000]lineplot[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]$y[/color][COLOR=#990000],[/color] [COLOR=#009900]$datax[/color][COLOR=#990000][[/color][COLOR=#009900]$key[/color][COLOR=#990000]]);[/color]
[tab][tab][COLOR=#009900]$lp[/color] [COLOR=#990000]=&[/color] [b][COLOR=#000000]end[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$plots[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$Mark[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]mark[COLOR=#990000]->[/color][b][COLOR=#000000]setType[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$mark[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$colour[/color] [COLOR=#990000]=[/color]  [b][COLOR=#0000FF]isset[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$colours[/color][COLOR=#990000][[/color][COLOR=#009900]$types[/color][COLOR=#990000][[/color][COLOR=#009900]$i[/color][COLOR=#990000]]][[/color][COLOR=#009900]$i[/color][COLOR=#990000]])[/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]?[/color] [COLOR=#009900]$colours[/color][COLOR=#990000][[/color][COLOR=#009900]$types[/color][COLOR=#990000][[/color][COLOR=#009900]$i[/color][COLOR=#990000]]][[/color][COLOR=#009900]$i[/color][COLOR=#990000]][/color] 
[tab][tab][tab][tab][tab][COLOR=#990000]:[/color] [COLOR=#FF0000]'black'[/color][COLOR=#990000];[/color]  [i][COLOR=#9A1900]//picks a colour[/color][/i]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Fill[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetFillColor[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]$col[/color] [COLOR=#990000].[/color][COLOR=#FF0000]'@0.7'[/color] [COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Weight[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetWeight[/color][/b][COLOR=#990000]([/color][COLOR=#993399]3[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Legend[/color][COLOR=#990000])[/color] [COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetLegend[/color][/b][COLOR=#990000]([/color][b][COLOR=#000000]wraplegend[/color][/b][COLOR=#990000]([/color][b][COLOR=#000000]sprintf[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]"%s-%s"[/color][COLOR=#990000],[/color][b][COLOR=#000000]ucfirst[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$types[/color][COLOR=#990000][[/color][COLOR=#009900]$i[/color][COLOR=#990000]]),[/color][b][COLOR=#000000]basename[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$fileNames[/color][COLOR=#990000][[/color][COLOR=#009900]$key[/color][COLOR=#990000]],[/color] [COLOR=#FF0000]".csv"[/color][COLOR=#990000]))));[/color]
[tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]mark[COLOR=#990000]->[/color][b][COLOR=#000000]setWidth[/color][/b][COLOR=#990000]([/color][COLOR=#993399]3[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetColor[/color][/b][COLOR=#990000]([/color] [COLOR=#009900]$col[/color] [COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]if[/color][/b] [COLOR=#990000]([/color][COLOR=#009900]$Values[/color][COLOR=#990000]):[/color]
[tab][tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]value[COLOR=#990000]->[/color][b][COLOR=#000000]Show[/color][/b][COLOR=#990000]();[/color]
[tab][tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color]value[COLOR=#990000]->[/color][b][COLOR=#000000]SetMargin[/color][/b][COLOR=#990000]([/color][COLOR=#993399]0[/color][COLOR=#990000]);[/color]
[tab][tab][b][COLOR=#0000FF]endif[/color][/b][COLOR=#990000];[/color]
[tab][tab][COLOR=#009900]$lp[/color][COLOR=#990000]->[/color][b][COLOR=#000000]SetStyle[/color][/b][COLOR=#990000]([/color][COLOR=#FF0000]'dashed'[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$graph[/color][COLOR=#990000]->[/color][b][COLOR=#000000]Add[/color][/b][COLOR=#990000]([/color][COLOR=#009900]$lp[/color][COLOR=#990000]);[/color]
[tab][tab][COLOR=#009900]$i[/color][COLOR=#990000]++;[/color]
[tab][b][COLOR=#0000FF]endforeach[/color][/b][COLOR=#990000];[/color]
[b][COLOR=#0000FF]endforeach[/color][/b][COLOR=#990000];[/color]
 
Thank you. I applied the changes but it's still giving the same error.
 
Shouldn't this:

Code:
$lp->SetColor( $col );

be

Code:
$lp->SetColor( $colour );

?
 
this line is wrong
Code:
$colour =  isset($colours[$types[$i]][$i]) 
                    ? $colours[$types[$i]][$i] 
                    : 'black';  //picks a colour

it should refer to $col and not $colour.

my bad (again).
 
That's okay, it is working now but I'll make the adjustment. The legend isn't showing the file name but I haven't had a chance yet to look into why that might be. Anyway, thank you again!
 
I've been moving along on this plot and it seems to be working well. It is in a function now and I've added check boxes to the page that allow for filling, showing the legend, changing from linear to logarithmic etc. easily. I do have a couple of issues though that perhaps you can see how to solve, especially the first one.

The first one is in the legend. It is showing too many values and all are Red- with no file name. Even if I select data for only a single plot, the legend has five entries so the test query I am using, with seven Y values, is showing 17 legend entries for a single plot, all labeled "Red-"!

Also on colors, when I enable fill, JPGraph crashes on:

Code:
if ($Fill) $lp->SetFillColor( $col .'@0.7' );

The third, once the colors are working for the plotting and legend, is in the colors themselves. How does it know which color to use? For this use it probably doesn't matter much but if there is some way for it to get the first letter of the column name and "presume" that it is a color to use, that would be great. For example, if the column were GreenR, then to presume that the column should be Green. I know it wouldn't be foolproof so there needs to be something other than black as a fallback color range.

And finally, I see that it is including both scatter and line plot functionality, but how do I switch between them?

Anyway, thank you again!

 
The "generic" plotting code that you provide a few posting up seems to be working well but for some odd reason, I am unable to get the legend moved to the upper right in spite having the same code from other plots to locate it. It is always on the left:

Code:
$graph->legend->SetPos(0.02,0.07,'right','top');

Also, the legend seems to contain many of the same entry and they are all the same color. Is there a fix?

In a function, this is what I have now:

Code:
function genericPlot ($JPGraphPath, $Query, $mysqli) {

	if ($_POST['SubData']):
		$SubData = $_POST['SubData'];
		foreach ($SubData as $key => $val):
			$SubData[$key][0] = ${$val};
			${$val} = $val;
		endforeach;
	endif;

	// If Minimum and maximum values are submitted, give a value
	$PlotXMin = (isset($_POST['PlotXMin'])) ? $_POST['PlotXMin']: "";
	$PlotXMax = (isset($_POST['PlotXMax'])) ? $_POST['PlotXMax']: "";
	$PlotYMin = (isset($_POST['PlotYMin'])) ? $_POST['PlotYMin']: "";
	$PlotYMax = (isset($_POST['PlotYMax'])) ? $_POST['PlotYMax']: "";
	
	require_once ($JPGraphPath."/jpgraph.php");
	require_once ($JPGraphPath."/jpgraph_line.php");
	require_once ($JPGraphPath."/jpgraph_scatter.php");
	require_once ($JPGraphPath."/jpgraph_log.php");	

	$graph = new Graph(800,450);
	$graph->SetImgFormat('png',60);
	$graph->title->Set('Data Plot From Custom Query');
	$graph->title->SetFont(FF_ARIAL,FS_BOLD,12);

	if ($Logarithmic) $graph->SetScale('linlog',0,0,$PlotXMin,$PlotXMax);
	if (!$Logarithmic) $graph->SetScale('linlin',0,0,$PlotXMin,$PlotXMax);

	$graph->yaxis->scale->SetAutoMin($PlotYMin);
	$graph->yaxis->scale->SetAutoMax($PlotYMax); 
	$graph->xaxis->scale->SetAutoMin($PlotXMin);
	$graph->xaxis->scale->SetAutoMax($PlotXMax); 
	$graph->xaxis->scale->ticks->Set(50,25);
	$graph->xgrid->Show(true,false);
	
	$graph->xaxis->SetTitle("",'middle');
	$graph->xaxis->scale->ticks->Set(50,25);

	$graph->xaxis->title->Set('X-Axis Data');
	$graph->xaxis->title->SetFont(FF_ARIAL,FS_BOLD,10);
	$graph->xaxis->title->SetMargin(20);

	$graph->ygrid->Show(true,false);
	$graph->yaxis->SetLabelFormatCallback('number_format');
	$graph->yaxis->title->Set('Y-Axis Data');
	$graph->yaxis->title->SetFont(FF_ARIAL,FS_BOLD,10);
	$graph->yaxis->title->SetMargin(50);
	$graph->yaxis->scale->SetGrace(10);

	$graph->img->SetMargin(100,40,40,75);
	$graph->SetShadow();
	$graph->legend->SetPos(0.02,0.07,'right','top');	

	$marks = array(     MARK_SQUARE, 
		                MARK_X,
		                MARK_DIAMOND, 
		                MARK_CIRCLE, 
		                MARK_STAR
		            );

	//use this for the color types distinguished by datayX
	$types = array(     'red',
		                'lightgreen',
		                'darkgreen',
		                'blue'
		        );

	$colors = array( 
		                'red'=>array('darkred', 'indianred','lightred','mediumred','orangered','palevioletred'), 
		                'lightgreen'=>array('forestgreen','green','lightseagreen','palegreen3'),
		                'darkgreen'=>array('darkgreen', 'darkseagreen','forestgreen','green'),
		                'blue'=>array('blue','cadetblue','cornflowerblue','darkblue','darkslateblue','dodgerblue')
		                );

	$datax = array();
	$datay = array();
	$fileNames = array();

	if ($result = $mysqli->query("$Query")):
		while ($row = $result->fetch_array()):
			$numCols = count($row);
			$fileNames[$fileID] = $row[0];
			$datax[$fileID][] = $row[1];
			for($col=0; $col < $numCols - 3; $col++):
				$datay[$col][$fileID][] = $row[$col + 3];
			endfor;
		endwhile;
	endif;

	$plots = array();
	foreach($datay as $key=>$data):

		$mark = current($marks); 
		if($mark === false) $mark = reset($marks);
		next($marks);

		$i=0;
		foreach ($data as $y):
		    $plots[] = new lineplot( $y, $datax[$key]);
		    $lp =& end($plots);
		    if($Mark) $lp->mark->setType($mark);
			$col =  isset($colors[$types[$i]][$i]) ? $colors[$types[$i]][$i]: 'black';  //picks a colour 
		    if ($Fill) $lp->SetFillColor( $col .'@0.7' );
		    if ($Weight) $lp->SetWeight(3);
		    if ($Legend) $lp->SetLegend(wraplegend(sprintf("%s-%s",ucfirst($types[$i]),basename($fileNames[$key], ".csv"))));
		    if ($Weight && $Mark) $lp->mark->setWidth(3);
		    $lp->SetColor( $col );
		    if ($Values):
		        $lp->value->Show();
		        $lp->value->SetMargin(0);
		    endif;
		    if ($Style) $lp->SetStyle('dashed');
		    $graph->Add($lp);
		    $i++;
		endforeach;
	endforeach;

	$graph->legend->setColumns(1);
	$graph->legend->setAbsPos(0,0,'left','top');

	$ImageName = date(now)+rand();

		$SubFolderPath = $JPGraphPath."/Temp/Freeform";
		$ImagePath = "jpgraph/Temp/Freeform";
		// Check if subfolder(s) exist; if not, create
		if (!is_dir($SubFolderPath)) mkdir($SubFolderPath, 01777);
			
		// Produce an image, display the graph
		$fileName = $SubFolderPath."/".$ImageName.".png";
		$graph->Stroke($fileName); // Creates image file

		$GraphImage = "<div class=\"ImageGraph\">\n";
		$GraphImage .= "<img src=\"$ImagePath/$ImageName.png?dummy=".time()."\">\n";
		$GraphImage .= "</div>\n\n";

		clean_old_tmp_files();

	echo $GraphImage;
}
 
Because lower down in the code the abspos of the legend is set to top left.

Not sure about the colours as it is hard to visualise in the abstract. Post a sample image?
 
Thank you! I looked everywhere (I thought) but couldn't find the alternate setting legend position until you confirmed that it was there. The legend is now where it belongs but each entry has only Red- over and over and over for more times than there are items being plotted. Unfortunately, here at work, I have no way to send an attachment and, as I'll be gone after tomorrow morning (unless the contract is extended), there is no time to do it. Any guesses without a capture?
 
i see

I am surprised that you are getting more legends than there are plots. you should get one per plot (of course). so if there are three sets of four datay then you will have twelve legends. that may not be what you want. if not, let me know.

as for just the Red-, that is indicative that

1. the types are not working. it assumes that in each set of y-data there are four plots going red, lgr, dgr and blue in that order. if that is not the case then is there any way of deriving the type from other data?

2. the filename is not being correctly accessed.

i think this can be fixed by the following

change this line
Code:
foreach ($data as [COLOR=#CE5C00]$fileID=>[/color]$y):

and this line

Code:
if ($Legend) $lp->SetLegend(wraplegend(sprintf("%s-%s",ucfirst($types[[COLOR=#F57900]$key[/color]]),basename($fileNames[[COLOR=#F57900]$fileID[/color]], ".csv"))));
 
Thank you, the changes fixed it partially. After applying the changes, it is now showing the color and the file name for the first four Legend entries just as it should but after that there are 13 more with only the -FileName. Also, enabling the Marks they are each different as they should be but when enabling the line Styles, they change from solod to sashed but they are all the same and they all have the same color.

The y-data, though, can be any colors in any order and in any number of columns so the assumption of red, lgr, dgr and blue in that order is not a valid one so possibly that is the problem. It is possible that the colors can be derived from the column names as there are only so many in the database that are colors but not all data being plotted using this code would necessarily be releated to colors at all so, while being able to color-code it properly would be nice, it's not too critical as long as the legend can descern one from another.
 
I see. I think there is a partial solution. Will post back in half an hour or so.
 
Thank you for your help. It also appears that instead of putting the plots on top of one another based on the X values, it is putting them side by side. This type of plotting needs to be on top of each other to be useful so somehow the X values are being misinterpreted. Can you look at that too? Thanks again.
 
sorry. fell asleep!

in honesty I am at the limits of my ability to assist given that i have no test data and cannot see the actual graphs that you are generating.

debugging with jpgraph is difficult. however i suggest that you footprint the data to make sure that the right stuff is being put into each array.

for the overlapping, I believe that $datax[$key] should refer to $datax[$fileID]

the typing i cannot fix without you providing a certain method of discerning the colour or other prefix for the filename. in the meantime I suggest that you add this as the last line of the internal foreach. at least it will display something

Code:
$i = $i == 4 ? 0 : $i;

i have not understood your comments about solid and dashed. are you saying that the marks change colour when you change the line style? or that the line changes colour when you change the linestyle? either way the style should not change anything.
 
i should add that the code we have been using presupposes that all the plots use the same y-axis. this need not be the case but if you want to have multiple y-axis then you need to provide an algorithm for determining which curve goes against which y-axis.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top