Eliminate gap on the x-axis for lineplot

112 views
Skip to first unread message

Christoph Marbach

unread,
Mar 30, 2023, 10:29:59 AM3/30/23
to JpGraph
Hi everybody, I am relatively new with jpgraph but quite desperate with the following problem: 
I draw an altitude chart from data in a gpx file. I store the altitude-data in an array $elevations for the y-axis and distance-data in $abs_distances for the x-axis. The graph plots correctly, but I can not get rid of the gap between the last value in the array and the right margin of the graph. The gap is variable dependent on the data in $abs_distances. 
The graph looks as follows: 

Screenshot 2023-03-30 161818.png
This is my jpgraph code: 
//-------------------- start jpgraph section -----------------
//Include der JPGraph library
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');

// create new JpGraph-Diagram
$width=800;
$height=450;
 
// Create the graph and set a scale.
$graph = new Graph($width,$height);
 
// Setup the graph
$graph->SetMargin(80,20,0,20);
$graph->title->Set($tourentitel);
$graph->title->SetFont( FF_VERDANA , FS_BOLD , 16);
$graph->subtitle->Set($tourendatum);
$graph->SetScale('linlin');

$graph->xaxis->title->Set('Distanz (km)');
$graph->xaxis->SetColor('blue');
$graph->xaxis->title->SetFont( FF_VERDANA , FS_BOLD , 10);

$graph->yaxis->title->Set('Höhe (m)');
$graph->yaxis->SetColor('blue');
$graph->yaxis->title->SetMargin(25);
$graph->yaxis->title->SetFont( FF_VERDANA , FS_BOLD , 10);

// Create the linear plot
$lineplot=new LinePlot($elevations,$abs_distances);
$lineplot->SetColor("blue");
$lineplot->SetWeight(0);
$lineplot->SetFillGradient('#739560:1.8','darkblue'); //color setting from turqoise to darkblue

// Add the plot to the graph
$graph->Add($lineplot);

// draw and save the jpgraph Diagram as jpg image
$graph->Stroke($altprofiledir.'/tbaltprofile_'.$tourendatum.'.jpg');
//-------------------- end jpgraph section -----------------

Any ideas to solve this? I have tried a lot of different approaches (setting manual ticks, define right margin etc.), but no success. Thanks for any hints. - Chris

w1djm...@gmail.com

unread,
Mar 30, 2023, 10:50:55 AM3/30/23
to JpGraph
Does your distance array contain values going out to 18 as shown on the graph?

Jon Taylor

unread,
Mar 30, 2023, 3:00:37 PM3/30/23
to JpGraph
The code looks ok. It looks to me like you have some rogue data values in your file with the distance out to 18km.

Christoph Marbach

unread,
Mar 30, 2023, 4:26:05 PM3/30/23
to JpGraph
There are no rogue data values in the array. I checked it's content with print_r. The array $abs_distances contains calculated distances between track points. It always start with 0 ands end with something between 5.00 and 150 km. 
I also changed $graph->SetScale('linlin'); to $graph->SetScale('linint');  -> no change
Setting the ticks manually to 0,2,4,6,8,10,12,14 does not help either. 


Scott Evans

unread,
Mar 30, 2023, 4:42:44 PM3/30/23
to JpGraph
Have you confirmed you have the same number of entries in both  your $elevations and $abs_distances tables?  I believe (though please correct me if I'm wrong) that the two arrays are used to create x/y coordinates of each graph point, so if you don't have the same number in each array, you'll possibly get incomplete graphs. 

Scott

w1djm...@gmail.com

unread,
Mar 31, 2023, 8:18:37 AM3/31/23
to JpGraph
Would you be able to post, as plain text files or inline, a sample of data for both the X and Y axis so we can test things to try and help you further?

Christoph Marbach

unread,
Mar 31, 2023, 9:00:33 AM3/31/23
to JpGraph
Thanks for the help and thoughts so far. Yes, I have the same number of entries in both arrays. I have attached a csv file with full data. No problem to play around with it -> not critical. 
These exact arrays produce the problem (x-axis gap) which is shown in my first entry. 

ele_dist.csv

w1djm...@gmail.com

unread,
Mar 31, 2023, 11:28:16 AM3/31/23
to JpGraph
I don't think you are going to be able to remove it based on the documentation and some testing. You are using INTeger for the X axis which implies that your values are integers but they are, in fact, not integers. Your largest value is 16.364 which is why the last X value is 17. Where the X axis ends is where 16.364 would be (the scale is too large to actually see that).

I tried changing all of the X axis values to integers but the graph ends up choppy and has steps up and down each side because the values on the X axis have very small differences. If you use the optional values on SetScale to end the X axis at 16 the chart spills off of the right side of the graph (as expected). It does remove the extra space but I think it looks bad/broken like that (see first image below). For that I used $graph->SetScale('intint',0,0,0,16);

You could still use that and increase the right margin slight so the X axis wouldn't run off of the right side as shown in the 2nd image below. For that I used $graph->SetMargin(80,25,0,20);

Bottom line is that JPGraph is doing exactly what it should be doing given the values you are asking it to graph.

Screenshot 2023-03-31 112240.png
Screenshot 2023-03-31 112550.png

Christoph Marbach

unread,
Mar 31, 2023, 2:57:10 PM3/31/23
to JpGraph
Thnx for testing. I also tried linear scale setting for both the x- and y-axis. Same result. I am able to determine the max-value=end-value of $abs_distances with something like $maxval=max($abs_distances); Then use $maxval to calculate the end of the lineplot. I can also determine the last tick which in my example data would be 16 (and omitting 17). Even if ticks are manually set: the gap remains. 
Reply all
Reply to author
Forward
0 new messages