PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How do I keep GD::Graph from writing the last X label and overwriting my other labels?

 October 10, 2022     gd, gd-graph, perl     No comments   

Issue

I set x_label_skip to skip labels, but it still tries to display the very last label and it is overwriting the other label, and looks messy. It shouldn't be writing the last label. It should be skipping the last label. I set the number of labels to skip as a function of how many data points there are.

This is what it looks like:

Code:

my $graph = GD::Graph::lines->new(400, 500);
$graph->set(
  r_margin          => 2,
  x_label           => 'Date',
  y_label           => 'Price',
  title             => "$symbol1, $symbol2",
  dclrs             => [ qw(lred lblue lgreen lyellow lpurple cyan lorange) ],
  transparent       => 0,
  x_labels_vertical => 1,
  x_label_skip      => int ((@tmpDate * 8)/(400-50) + 1), # a function of # of data points, each label 8px. More labels, more skip.
) or die $graph->error;

Solution

Ok I found it. Just use modulo to divide the # of entries by # of labels to skip, and use that as the offset. It seems GD::Graph will always want to print the last label, so can't control that, but you can control the 1st label to print. Seems backwards to me but whatever.

my $graph = GD::Graph::lines->new(400, 500);
my $skip = int ((@tmpDate * 8)/(400-50) + 1); # a function of # of data points, each label 8px. More labels, more skip. 
$graph->set( 
  r_margin          => 2,
  x_label           => 'Date',
  y_label           => 'Price',
  title             => "$symbol1, $symbol2",
  dclrs             => [ qw(lred lblue lgreen lyellow lpurple cyan lorange) ],
  transparent       => 0,
  x_labels_vertical => 1,
  x_label_skip      => $skip,
  x_tick_offset     => @tmpDate % $skip, # ensure last label doesn't overwrite second-to-last label
) or die $graph->error;


Answered By - Chloe
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing