Skip to content

Commit

Permalink
Added tooltip and data_label callback options, shading on 3D bars.
Browse files Browse the repository at this point in the history
  • Loading branch information
goat1000 committed Jul 3, 2015
1 parent 2ca65f2 commit 418e17b
Show file tree
Hide file tree
Showing 24 changed files with 117 additions and 39 deletions.
49 changes: 31 additions & 18 deletions SVGGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -1379,31 +1379,35 @@ protected function AutoHide(&$element)
* Default tooltip contents are key and value, or whatever
* $key is if $value is not set
*/
protected function SetTooltip(&$element, &$item, $key, $value = NULL,
protected function SetTooltip(&$element, &$item, $dataset, $key, $value = NULL,
$duplicate = FALSE)
{
// use structured data tooltips if specified
if(is_array($this->structure) && isset($this->structure['tooltip'])) {
if(is_callable($this->tooltip_callback)) {
if(is_null($value))
$value = $key;
$text = call_user_func($this->tooltip_callback, $dataset, $key, $value);
} elseif(is_array($this->structure) && isset($this->structure['tooltip'])) {
// use structured data tooltips if specified
$text = $item->Data('tooltip');
if(is_null($text))
return;
} else {
// use a sprintf format for the tooltip
$format = '%s, %s';
if(is_null($value)) {
$value = $key;
$format = '%2$s'; // value only
}
$k = is_numeric($key) ? $this->units_before_tooltip_key .
Graph::NumString($key) . $this->units_tooltip_key : $key;
$v = is_numeric($value) ? $this->units_before_tooltip .
Graph::NumString($value) . $this->units_tooltip : $value;
$text = sprintf($format, $k, $v);
$text = $this->FormatTooltip($item, $dataset, $key, $value);
}
if(is_null($text))
return;
$text = addslashes(str_replace("\n", '\n', $text));
Graph::$javascript->SetTooltip($element, $text, $duplicate);
}

/**
* Default format is value only
*/
protected function FormatTooltip(&$item, $dataset, $key, $value)
{
return $this->units_before_tooltip . Graph::NumString($value) .
$this->units_tooltip;
}


/**
* Sets the fader for an element
* @param array &$element Element that should cause fading
Expand Down Expand Up @@ -1612,8 +1616,8 @@ public function Fetch($header = TRUE, $defer_javascript = TRUE)
Graph::$precision = $this->settings['precision'];
$old_precision = ini_set('precision', Graph::$precision);
// set decimal and thousands for NumString
Graph::$decimal = $this->settings['decimal'];
Graph::$thousands = $this->settings['thousands'];
Graph::SetNumStringOptions($this->settings['decimal'],
$this->settings['thousands']);

// display title and description if available
$heading = '';
Expand Down Expand Up @@ -1794,6 +1798,15 @@ public static function NumString($n, $decimals = null, $precision = null)
return $s;
}

/**
* Sets the number format characters
*/
public static function SetNumStringOptions($decimal, $thousands)
{
Graph::$decimal = $decimal;
Graph::$thousands = $thousands;
}

/**
* Returns the minimum value in the array, ignoring NULLs
*/
Expand Down
36 changes: 35 additions & 1 deletion SVGGraphBar3DGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected function Draw()

$group = array_merge($group, $bar_style);
if($this->show_tooltips)
$this->SetTooltip($group, $item, $item->value);
$this->SetTooltip($group, $item, 0, $item->key, $item->value);
$this->SetStroke($group, $item, 0, 'round');
$bars .= $this->Element('g', $group, NULL, $link);
unset($group['id']); // make sure a new one is generated
Expand Down Expand Up @@ -144,6 +144,10 @@ protected function Bar3D($item, &$bar, &$top, $index, $dataset = NULL,
if(is_null($pos) || $pos > $this->height - $this->pad_bottom)
return '';

$side_overlay = min(1, max(0, $this->bar_side_overlay_opacity));
$top_overlay = min(1, max(0, $this->bar_top_overlay_opacity));
$front_overlay = min(1, max(0, $this->bar_front_overlay_opacity));

$bar_side = '';
$bw = $this->block_width;
$bh = $bar['height'];
Expand All @@ -164,18 +168,48 @@ protected function Bar3D($item, &$bar, &$top, $index, $dataset = NULL,
);
if($this->skew_side)
$side['fill'] = 'none';
if($side_overlay)
$side['stroke'] = 'none'; // only stroke top layer
$bar_side .= $this->Element('path', $side);

if($side_overlay) {
$side['fill-opacity'] = $side_overlay;
$side['fill'] = $this->bar_side_overlay_colour;
unset($side['stroke']);
$bar_side .= $this->Element('path', $side);
}

if(is_null($top)) {
$bar_top = '';
} else {
$top['transform'] = "translate($bar[x],$bar[y])";
$top['fill'] = $this->GetColour($item, $index, $dataset,
$this->skew_top ? FALSE : TRUE);
if($top_overlay)
$top['stroke'] = 'none';
$bar_top = $this->Element('use', $top, null, $this->empty_use ? '' : null);

if($top_overlay) {
unset($top['stroke']);
$otop = $top;
$otop['fill-opacity'] = $top_overlay;
$otop['fill'] = $this->bar_top_overlay_colour;
$bar_top .= $this->Element('use', $otop, null, $this->empty_use ? '' : null);
}
}

if($front_overlay)
$bar['stroke'] = 'none';
$rect = $this->Element('rect', $bar);

if($front_overlay) {
unset($bar['stroke']);
$obar = $bar;
$obar['fill-opacity'] = $front_overlay;
$obar['fill'] = $this->bar_front_overlay_colour;
$rect .= $this->Element('rect', $obar);
}

return $rect . $bar_top . $bar_side;
}

Expand Down
2 changes: 1 addition & 1 deletion SVGGraphBarAndLineGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, $j, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected function Draw()
$show_label = $this->AddDataLabel(0, $bnum, $bar, $item, $bar['x'],
$bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, 0, $item->key, $item->value,
!$this->compat_events && $show_label);
$rect = $this->Element('rect', $bar, $bar_style);
$bars .= $this->GetLink($item, $item->key, $rect);
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphBoxAndWhiskerGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function Draw()
$this->GridY($item->Data('bottom')) - $this->GridY($item->Data('top'))
);
if($this->show_tooltips)
$this->SetTooltip($g, $item, $item->value, null,
$this->SetTooltip($g, $item, 0, $item->key, $item->value,
!$this->compat_events && $this->show_label);

if($this->semantic_classes)
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphBubbleGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function Draw()
$x - $r, $y - $r, $r * 2, $r * 2);

if($this->show_tooltips)
$this->SetTooltip($circle, $item, $area, null,
$this->SetTooltip($circle, $item, 0, $item->key, $area,
!$this->compat_events);
if($this->semantic_classes)
$circle['class'] = "series0";
Expand Down
9 changes: 8 additions & 1 deletion SVGGraphDataLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,14 @@ protected function DrawLabel($dataset, $index, &$gobject)
// convert to string - numbers will confuse TextSize()
$content = (string)$gobject['content'];
} else {
$content = $gobject['item']->Data('label');
if(is_callable($this->data_label_callback)) {
$content = call_user_func($this->data_label_callback, $dataset,
$gobject['item']->key, $gobject['item']->value);
if(is_null($content))
$content = '';
} else {
$content = $gobject['item']->Data('label');
}
if(is_null($content)) {
$content = !is_null($gobject['content']) ? $gobject['content'] :
$this->units_before_label . Graph::NumString($gobject['item']->value) .
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphFloatingBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function Draw()
$bar['x'], $bar['y'], $bar['width'], $bar['height']);

if($this->show_tooltips)
$this->SetTooltip($bar, $item, $value, null,
$this->SetTooltip($bar, $item, 0, $item->key, $value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series0";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphGroupedBar3DGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function Draw()
$bar['x'] + $tx, $bar['y'] + $ty, $bar['width'], $bar['height']);

if($this->show_tooltips)
$this->SetTooltip($group, $item, $item->value);
$this->SetTooltip($group, $item, $j, $item->key, $item->value);
$link = $this->GetLink($item, $k, $bar_sections);
$this->SetStroke($group, $item, $j, 'round');
if($this->semantic_classes)
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphGroupedBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, $j, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphGroupedCylinderGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected function Draw()
$bar['x'] + $tx, $bar['y'] + $ty, $bar['width'], $bar['height']);

if($this->show_tooltips)
$this->SetTooltip($group, $item, $item->value);
$this->SetTooltip($group, $item, $j, $item->key, $item->value);
$link = $this->GetLink($item, $k, $bar_sections);
$this->SetStroke($group, $item, $j, 'round');
if($this->semantic_classes)
Expand Down
4 changes: 4 additions & 0 deletions SVGGraphHistogram.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Histogram extends BarGraph {
protected $label_centre = FALSE;
protected $force_assoc = TRUE;
protected $minimum_units_y = 1;
protected $repeated_keys = 'accept'; // allow repeated keys

protected $increment = NULL;
protected $percentage = false;
Expand Down Expand Up @@ -74,6 +75,9 @@ public function Values($values)
$map = array();
$start = $this->Interval($min);
$end = $this->Interval($max, true) + $this->increment / 2;

Graph::SetNumStringOptions($this->settings['decimal'],
$this->settings['thousands']);
for($i = $start; $i < $end; $i += $this->increment) {
$key = Graph::NumString($i);
$map[$key] = null;
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphHorizontalBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function Draw()
$show_label = $this->AddDataLabel(0, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, 0, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series0";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphHorizontalFloatingBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function Draw()
$bar['x'], $bar['y'], $bar['width'], $bar['height']);

if($this->show_tooltips)
$this->SetTooltip($bar, $item, $value, null,
$this->SetTooltip($bar, $item, 0, $item->key, $value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series0";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphHorizontalGroupedBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, $j, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphHorizontalStackedBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, 0, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphPieGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected function Draw()

if($radius_x || $radius_y) {
if($this->show_tooltips)
$this->SetTooltip($attr, $item, $key, $value, !$this->compat_events);
$this->SetTooltip($attr, $item, 0, $key, $value, !$this->compat_events);

$this->CalcSlice($angle_start, $angle_end, $radius_x, $radius_y,
$x1, $y1, $x2, $y2);
Expand Down
14 changes: 13 additions & 1 deletion SVGGraphPointGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function GetMarker($marker, $set)
if($this->semantic_classes)
$use['class'] = "series{$set}";
if($this->show_tooltips)
$this->SetTooltip($use, $marker->item, $marker->key, $marker->value);
$this->SetTooltip($use, $marker->item, $set, $marker->key, $marker->value);

if($this->GetLinkURL($marker->item, $marker->key)) {
$id = $this->marker_link_ids[$id];
Expand Down Expand Up @@ -466,6 +466,18 @@ protected function BestFit($type, $dataset, $colour, $stroke_width, $dash,

return $this->Element('path', $path);
}

/**
* Override to show key and value
*/
protected function FormatTooltip(&$item, $dataset, $key, $value)
{
$text = is_numeric($key) ? $this->units_before_tooltip_key .
Graph::NumString($key) . $this->units_tooltip_key : $key;
$text .= ', ' . $this->units_before_tooltip . Graph::NumString($value) .
$this->units_tooltip;
return $text;
}
}

class Marker {
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphPopulationPyramid.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, $j, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphStackedBar3DGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function Draw()
$bar['x'] + $tx, $bar['y'] + $ty, $bar['width'], $bar['height']);

if($this->show_tooltips)
$this->SetTooltip($group, $item, $value);
$this->SetTooltip($group, $item, $j, $k, $value);
$link = $this->GetLink($item, $k, $bar_sections);
$this->SetStroke($group, $item, $j, 'round');
if($this->semantic_classes)
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphStackedBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item, $bar['x'],
$bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, $j, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphStackedCylinderGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function Draw()
$bar['x'] + $tx, $bar['y'] + $ty, $bar['width'], $bar['height']);

if($this->show_tooltips)
$this->SetTooltip($group, $item, $value);
$this->SetTooltip($group, $item, $j, $k, $value);
$link = $this->GetLink($item, $k, $bar_sections);
$this->SetStroke($group, $item, $j, 'round');
if($this->semantic_classes)
Expand Down
2 changes: 1 addition & 1 deletion SVGGraphStackedGroupedBarGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function Draw()
$show_label = $this->AddDataLabel($j, $bnum, $bar, $item,
$bar['x'], $bar['y'], $bar['width'], $bar['height']);
if($this->show_tooltips)
$this->SetTooltip($bar, $item, $item->value, null,
$this->SetTooltip($bar, $item, $j, $item->key, $item->value,
!$this->compat_events && $show_label);
if($this->semantic_classes)
$bar['class'] = "series{$j}";
Expand Down
Loading

0 comments on commit 418e17b

Please sign in to comment.