Skip to content

Commit

Permalink
Support fill and fillColour in data label options.
Browse files Browse the repository at this point in the history
  • Loading branch information
goat1000 committed Dec 2, 2022
1 parent df9ebc2 commit 4e5371b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 3.18 (02/12/2022)
------------
- Added shading of flat 3D pie graph sides.
- Added support for colouring data labels using "fill" and "fillColour".
- Fixed axis_text structure option on multi-dataset graphs.
- Fixed explode "all" doing nothing on pie graphs with all values equal.

Version 3.17 (16/09/2022)
------------
- Added axis_tightness_x option for removing space at ends of X-axis.
Expand Down
6 changes: 4 additions & 2 deletions ColourGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ class ColourGroup {
private $stroke;

public function __construct(&$graph, $item, $key, $dataset,
$stroke_opt = 'stroke_colour', $fill = null, $item_opt = null)
$stroke_opt = 'stroke_colour', $fill = null, $item_opt = null,
$stroke_opt_is_colour = false)
{
$stroke = $graph->getItemOption($stroke_opt, $dataset, $item, $item_opt);
$stroke = $stroke_opt_is_colour ? $stroke_opt :
$graph->getItemOption($stroke_opt, $dataset, $item, $item_opt);
if(is_array($stroke)) {
$this->stroke = new Colour($graph, $stroke);
return;
Expand Down
3 changes: 2 additions & 1 deletion Colours.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function getColour($index, $dataset = null)
return $this->colours[$dataset][$index];

// try mod
$dataset = $dataset % $this->dataset_count;
if(is_numeric($dataset))
$dataset = $dataset % $this->dataset_count;
if(array_key_exists($dataset, $this->colours))
return $this->colours[$dataset][$index];

Expand Down
42 changes: 31 additions & 11 deletions DataLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ class DataLabels {
'align' => 'data_label_align',
];

/**
* Options that are colours, so need translation
*/
protected $colour_options = [
'colour', 'altcolour', 'back_colour', 'back_altcolour', 'stroke', 'fill',
];

function __construct(&$graph)
{
$this->graph =& $graph;
Expand Down Expand Up @@ -378,9 +385,19 @@ protected function getStyle($dataset, $index, &$gobject)

// structured styles and user defined label styles
if($gobject['item'] !== null)
$this->itemStyles($style, $gobject['item']);
$this->itemStyles($style, $gobject['item'], $index, $dataset);
elseif($dataset === '_user')
$this->userStyles($style, $gobject);

// deal with fill/fillColour
foreach($this->colour_options as $s) {
if(isset($style[$s]) && is_string($style[$s]) &&
strpos($style[$s], 'fill') !== false) {
$cg = new ColourGroup($this->graph, $gobject['item'], $index, $dataset,
$style[$s], null, null, true);
$style[$s] = $cg->stroke();
}
}
return $style;
}

Expand Down Expand Up @@ -435,13 +452,15 @@ protected function getTypeInfo($type, $field = null)
protected function getColours($hpos, $vpos, $style)
{
// if the position is outside, use the alternative colours
$colour = $style['colour'];
$back_colour = $style['back_colour'];
$colour = new Colour($this->graph, $style['colour']);
$back_colour = new Colour($this->graph, $style['back_colour']);
if(strpos($hpos . $vpos, 'o') !== false) {
if(!empty($style['altcolour']))
$colour = $style['altcolour'];
if(!empty($style['back_altcolour']))
$back_colour = $style['back_altcolour'];
$alt = new Colour($this->graph, $style['altcolour']);
$back_alt = new Colour($this->graph, $style['back_altcolour']);
if(!$alt->isNone())
$colour = $alt;
if(!$back_alt->isNone())
$back_colour = $back_alt;
}
return [$colour, $back_colour];
}
Expand Down Expand Up @@ -493,7 +512,7 @@ protected function drawLabel($content, $label_w, $label_h, $dataset, $index,
$text = [
'font-family' => $style['font'],
'font-size' => $font_size,
'fill' => new Colour($this->graph, $colour),
'fill' => $colour,
];

$label_markup = '';
Expand Down Expand Up @@ -603,10 +622,11 @@ protected function drawLabel($content, $label_w, $label_h, $dataset, $index,
}
}

if(!empty($back_colour)) {
//$back_colour = new Colour($this, $back_colour);
if(!$back_colour->isNone()) {
$outline = [
'stroke-width' => '3px',
'stroke' => new Colour($this->graph, $back_colour),
'stroke' => $back_colour,
'stroke-linejoin' => 'round',
];
$t1 = array_merge($outline, $text);
Expand Down Expand Up @@ -640,7 +660,7 @@ public function getStyleMap()
/**
* Individual label styles from the structured data item
*/
protected function itemStyles(&$style, &$item)
protected function itemStyles(&$style, &$item, $index, $dataset)
{
// overwrite any style options that the item has set
$v = $item->data_label_padding;
Expand Down
2 changes: 1 addition & 1 deletion README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SVGGraph Library version 3.17
SVGGraph Library version 3.18
=============================

This library provides PHP classes and functions for easily creating SVG
Expand Down
2 changes: 1 addition & 1 deletion SVGGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SVGGraph {

use SVGGraphTrait;

const VERSION = 'SVGGraph 3.17';
const VERSION = 'SVGGraph 3.18';
private $width = 100;
private $height = 100;
private $settings = [];
Expand Down

0 comments on commit 4e5371b

Please sign in to comment.