Skip to content

Commit

Permalink
Add axis_pad_* and axus_extend_* options.
Browse files Browse the repository at this point in the history
  • Loading branch information
goat1000 committed Aug 1, 2022
1 parent 1f21686 commit e05e9ee
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
57 changes: 57 additions & 0 deletions DisplayAxis.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DisplayAxis {
protected $minimum_subdivision;
protected $minimum_units;
protected $subdivisions_fixed;
protected $offset = [0, 0];

/**
* $orientation = 'h' or 'v'
Expand Down Expand Up @@ -72,6 +73,23 @@ public function __construct(&$graph, &$axis, $axis_no, $orientation, $type,
$this->show_text = $graph->getOption('show_axis_text_' . $o);
}

// offset caused by padding between axis and grid
if($o == 'v') {
switch($axis_no) {
case 0: $this->offset[0] = -$graph->getOption('axis_pad_left');
break;
case 1: $this->offset[0] = $graph->getOption('axis_pad_right');
break;
}
} else {
switch($axis_no) {
case 0: $this->offset[1] = $graph->getOption('axis_pad_bottom');
break;
case 1: $this->offset[1] = -$graph->getOption('axis_pad_top');
break;
}
}

// lambda to make retrieving options simpler
$get_axis_option = function($option) use ($graph, $o, $axis_no) {
return $graph->getOption([$option . '_' . $o, $axis_no], $option);
Expand All @@ -94,6 +112,17 @@ public function __construct(&$graph, &$axis, $axis_no, $orientation, $type,
if($this->show_axis) {
$styles['overlap'] = $graph->getOption('axis_overlap');
$styles['stroke_width'] = $get_axis_option('axis_stroke_width');
if($o == 'v') {
$styles['extend'] = [
$graph->getOption('axis_extend_top', 'axis_pad_top'),
$graph->getOption('axis_extend_bottom', 'axis_pad_bottom'),
];
} else {
$styles['extend'] = [
$graph->getOption('axis_extend_left', 'axis_pad_left'),
$graph->getOption('axis_extend_right', 'axis_pad_right'),
];
}

if($graph->getOption('show_divisions')) {
$this->show_divisions = true;
Expand Down Expand Up @@ -226,7 +255,23 @@ public function measure($with_label = true)
$bbox->grow($lpos['x'], $lpos['y'], $lpos['x'] + $lpos['width'],
$lpos['y'] + $lpos['height']);
}
$bbox = $this->addOffset($bbox);

return $bbox;
}

/**
* Adds the padding offset to the bounding box
*/
protected function addOffset(BoundingBox $bbox)
{
if($this->axis_no > 0) {
$bbox->x2 += $this->offset[0];
$bbox->y1 += $this->offset[1];
} else {
$bbox->x1 += $this->offset[0];
$bbox->y2 += $this->offset[1];
}
return $bbox;
}

Expand Down Expand Up @@ -404,6 +449,10 @@ protected function getTextPositions($x, $y, $xoff, $yoff, $points, $anchor)
public function draw($x, $y, $gx, $gy, $g_width, $g_height)
{
$content = '';
$x += $this->offset[0];
$y += $this->offset[1];
$gx += $this->offset[0];
$gy += $this->offset[1];
if($this->show_axis) {
if($this->show_divisions) {
$content .= $this->drawDivisions($x, $y, $g_width, $g_height);
Expand Down Expand Up @@ -433,8 +482,12 @@ public function drawAxisLine($x, $y, $len)
$reversed = $this->axis->reversed();
if($this->orientation == 'h') {
$x = $reversed ? $x - $overlap - $len : $x - $overlap;
$x -= $this->styles['extend'][0];
$length += $this->styles['extend'][0] + $this->styles['extend'][1];
} else {
$y = $reversed ? $y - $overlap - $len : $y - $overlap;
$y -= $this->styles['extend'][0];
$length += $this->styles['extend'][0] + $this->styles['extend'][1];
}

$colour = $this->styles['colour'];
Expand Down Expand Up @@ -534,6 +587,7 @@ public function drawText($x, $y, $gx, $gy, $g_width, $g_height)
if(!$opposite) {
$anchor = 'start';
$x_offset = $bbox->x1 + $space;
$x_offset -= $this->offset[0];
}
} else {
$anchor = 'start';
Expand Down Expand Up @@ -664,6 +718,7 @@ protected function getLabelPosition()
} else {
$y = $bbox->y2 + $space;
}
$y -= $this->offset[1]; // handle padding offset
if(is_numeric($align)) {
$tx = $a_length * $align;
} else {
Expand All @@ -690,9 +745,11 @@ protected function getLabelPosition()
$reversed = $this->axis->reversed();
if($this->axis_no > 0) {
$x = $bbox->x2 + $space;
$x -= $this->offset[0]; // handle padding offset
$tx = $x + $width - $baseline;
} else {
$x = $bbox->x1 - $space - $width;
$x -= $this->offset[0]; // handle padding offset
$tx = $x + $baseline;
$x -= $space;
}
Expand Down
15 changes: 12 additions & 3 deletions DisplayAxisLevels.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ public function measure($with_label = true)
$tbox->offset(0, $bbox->y2);
}
} else {
$dbox->offset($bbox->x1, 0);
$tbox->offset($bbox->x1, 0);
if($this->axis_no > 0) {
$dbox->offset($bbox->x2, 0);
$tbox->offset($bbox->x2, 0);
} else {
$dbox->offset($bbox->x1, 0);
$tbox->offset($bbox->x1, 0);
}
}
// store the text box for this level
$this->levels[$i]['text_box'] = $tbox;
Expand All @@ -90,6 +95,7 @@ public function measure($with_label = true)
$bbox->grow($lpos['x'], $lpos['y'], $lpos['x'] + $lpos['width'],
$lpos['y'] + $lpos['height']);
}
$bbox = $this->addOffset($bbox);

return $bbox;
}
Expand Down Expand Up @@ -174,7 +180,10 @@ public function drawText($x, $y, $gx, $gy, $g_width, $g_height)
break;
}
} else {
$x1 = $x_offset + $tbox->x2;
if($this->axis_no > 0)
$x1 = $x_offset + $tbox->x1;
else
$x1 = $x_offset + $tbox->x2;
switch($this->styles['t_align']) {
case 'left':
if(!$opposite) {
Expand Down
4 changes: 3 additions & 1 deletion GanttChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,10 @@ private function autoHeight($items)
$v_style = $v_d_a->getStyling();
$h_style = $h_d_a->getStyling();

// fixed space allows for two lines of labels, plus two of headings
// fixed space allows for two lines of labels, plus two of headings, plus padding
$fixed = $this->pad_bottom + $this->pad_top;
$fixed += $this->getOption('axis_pad_top') + $this->getOption('axis_pad_bottom');

if(isset($h_style['t_font_size'])) {
$space = isset($h_style['t_space']) ? $h_style['t_space'] : 1;
if($h_style['d_style'] == 'box')
Expand Down
8 changes: 8 additions & 0 deletions svggraph.ini
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,14 @@ grid_clip_overlap_x = 2
grid_clip_overlap_y = 10
axis_double_x = false
axis_double_y = false
axis_pad_left = 0
axis_pad_right = 0
axis_pad_top = 0
axis_pad_bottom = 0
axis_extend_left = null
axis_extend_right = null
axis_extend_top = null
axis_extend_bottom = null

[BarGraph]
bar_space = 10
Expand Down

0 comments on commit e05e9ee

Please sign in to comment.