如何让GD :: Graph不要写最后的X标签并覆盖我的其他标签?

时间:2012-04-01 02:54:42

标签: perl gd gd-graph

我设置x_label_skip以跳过标签,但它仍然会尝试显示最后一个标签,并且它会覆盖另一个标签,看起来很乱。它不应该写最后一个标签。它应该跳过最后一个标签。我根据有多少数据点设置要跳过的标签数量。

这就是它的样子:

代码:

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;

2 个答案:

答案 0 :(得分:5)

好的,我找到了。只需使用modulo将条目数除以要跳过的标签数,并将其用作偏移量。看起来GD :: Graph总是要打印最后一个标签,所以无法控制,但你可以控制第一个标签打印。似乎向后看,但无论如何。

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;

答案 1 :(得分:0)

x_last_label_skip => 1完全按照您的需要工作,只是跳过最后一个标签。可能它没有在2012年实施,但现在已经实现了。