Improve interface of InlinePie

Method setLabel can also accept a simple string and title is mandatory in constructor
This commit is contained in:
Matthias Jentsch 2014-06-25 15:21:12 +02:00
parent ae4aa9c182
commit 7b19bd1d99
3 changed files with 43 additions and 28 deletions

View File

@ -127,7 +127,7 @@ EOD;
* *
* @var string * @var string
*/ */
private $title = ''; private $title;
/** /**
* The style for the HtmlElement * The style for the HtmlElement
@ -195,18 +195,22 @@ EOD;
/** /**
* The labels to be displayed in the pie-chart * The labels to be displayed in the pie-chart
* *
* @param null $labels * @param mixed $label The label of the displayed value, or null for no labels
* *
* @return $this * @return $this Fluent interface
*/ */
public function setLabels($labels = null) public function setLabel($label)
{ {
if ($labels != null) { if (is_array($label)) {
$this->url->setParam('labels', implode(',', $labels)); $this->url->setParam('labels', implode(',', array_keys($label)));
} elseif ($label != null) {
$labelArr = array($label, $label, $label, '');
$this->url->setParam('labels', implode(',', $labelArr));
$label = $labelArr;
} else { } else {
$this->url->removeKey('labels'); $this->url->removeKey('labels');
} }
$this->labels = $labels; $this->labels = $label;
return $this; return $this;
} }
@ -319,10 +323,12 @@ EOD;
* Create a new InlinePie * Create a new InlinePie
* *
* @param array $data The data displayed by the slices * @param array $data The data displayed by the slices
* @param string $title The title of this Pie
* @param array $colors An array of RGB-Color values to use * @param array $colors An array of RGB-Color values to use
*/ */
public function __construct(array $data, $colors = null) public function __construct(array $data, $title, $colors = null)
{ {
$this->title = $title;
$this->url = Url::fromPath('svg/chart.php'); $this->url = Url::fromPath('svg/chart.php');
if (array_key_exists('data', $data)) { if (array_key_exists('data', $data)) {
$this->data = $data['data']; $this->data = $data['data'];

View File

@ -80,7 +80,11 @@ class Monitoring_MultiController extends Controller
$this->view->downtimes = $this->getDowntimes($hosts); $this->view->downtimes = $this->getDowntimes($hosts);
$this->view->errors = $errors; $this->view->errors = $errors;
$this->view->states = $this->countStates($hosts, 'host', 'host_name'); $this->view->states = $this->countStates($hosts, 'host', 'host_name');
$this->view->pie = $this->createPie($this->view->states, $this->view->getHelper('MonitoringState')->getHostStateColors()); $this->view->pie = $this->createPie(
$this->view->states,
$this->view->getHelper('MonitoringState')->getHostStateColors(),
t('Host State')
);
// Handle configuration changes // Handle configuration changes
$this->handleConfigurationForm(array( $this->handleConfigurationForm(array(
@ -139,8 +143,16 @@ class Monitoring_MultiController extends Controller
$this->view->downtimes = $this->getDowntimes($services); $this->view->downtimes = $this->getDowntimes($services);
$this->view->service_states = $this->countStates($services, 'service'); $this->view->service_states = $this->countStates($services, 'service');
$this->view->host_states = $this->countStates($services, 'host', 'host_name'); $this->view->host_states = $this->countStates($services, 'host', 'host_name');
$this->view->service_pie = $this->createPie($this->view->service_states, $this->view->getHelper('MonitoringState')->getServiceStateColors()); $this->view->service_pie = $this->createPie(
$this->view->host_pie = $this->createPie($this->view->host_states, $this->view->getHelper('MonitoringState')->getHostStateColors()); $this->view->service_states,
$this->view->getHelper('MonitoringState')->getServiceStateColors(),
t('Service State')
);
$this->view->host_pie = $this->createPie(
$this->view->host_states,
$this->view->getHelper('MonitoringState')->getHostStateColors(),
t('Host State')
);
$this->view->errors = $errors; $this->view->errors = $errors;
$this->handleConfigurationForm(array( $this->handleConfigurationForm(array(
@ -236,10 +248,11 @@ class Monitoring_MultiController extends Controller
return $states; return $states;
} }
private function createPie($states, $colors) private function createPie($states, $colors, $title)
{ {
$chart = new InlinePie(array_values($states), $colors); $chart = new InlinePie(array_values($states), $title, $colors);
$chart->setLabels(array_keys($states))->setHeight(100)->setWidth(100); $chart->setLabel(array_keys($states))->setHeight(100)->setWidth(100);
$chart->setTitle($title);
return $chart; return $chart;
} }

View File

@ -18,18 +18,14 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) { if (!$perfdata->isPercentage() && $perfdata->getMaximumValue() === null) {
continue; continue;
} }
$pieChart = $this->createInlinePie($perfdata, $label); $pieChart = $this->createInlinePie($perfdata, $label, htmlspecialchars($label));
if ($compact) { if ($compact) {
$pieChart->setTitle(
htmlspecialchars($label) /* . ': ' . htmlspecialchars($this->formatPerfdataValue($perfdata) */
);
if (! $float) { if (! $float) {
$result .= $pieChart->render(); $result .= $pieChart->render();
} else { } else {
$result .= '<div style="float: right;">' . $pieChart->render() . '</div>'; $result .= '<div style="float: right;">' . $pieChart->render() . '</div>';
} }
} else { } else {
$pieChart->setTitle(htmlspecialchars($label));
if (! $perfdata->isPercentage()) { if (! $perfdata->isPercentage()) {
$pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)'); $pieChart->setTooltipFormat('{{label}}: {{formatted}} ({{percent}}%)');
} }
@ -85,10 +81,10 @@ class Zend_View_Helper_Perfdata extends Zend_View_Helper_Abstract
return $perfdata->getValue(); return $perfdata->getValue();
} }
protected function createInlinePie(Perfdata $perfdata, $label = '') protected function createInlinePie(Perfdata $perfdata, $title, $label = '')
{ {
$pieChart = new InlinePie($this->calculatePieChartData($perfdata)); $pieChart = new InlinePie($this->calculatePieChartData($perfdata), $title);
$pieChart->setLabels(array($label, $label, $label, '')); $pieChart->setLabel($label);
$pieChart->setHideEmptyLabel(); $pieChart->setHideEmptyLabel();
//$pieChart->setHeight(32)->setWidth(32); //$pieChart->setHeight(32)->setWidth(32);