Index: frontends/php/include/classes/graphdraw/CLineGraphDraw.php
===================================================================
--- frontends/php/include/classes/graphdraw/CLineGraphDraw.php	(revision 44276)
+++ frontends/php/include/classes/graphdraw/CLineGraphDraw.php	(working copy)
@@ -1922,7 +1922,7 @@
 			else {
 				$legend->addCell($rowNum, array('image' => $colorSquare, 'marginRight' => 5));
 				$legend->addCell($rowNum, array('text' => $itemCaption));
-				$legend->addCell($rowNum, array('text' => '[ '._('no data').' ]'));
+				$legend->addCell($rowNum, array('text' => '['._('no data').']'));
 			}
 
 			$rowNum++;
Index: frontends/php/include/classes/graphdraw/CPieGraphDraw.php
===================================================================
--- frontends/php/include/classes/graphdraw/CPieGraphDraw.php	(revision 44276)
+++ frontends/php/include/classes/graphdraw/CPieGraphDraw.php	(working copy)
@@ -283,25 +283,31 @@
 
 	protected function drawLegend() {
 		$shiftY = $this->shiftY + $this->shiftYLegend;
-		$max_host_len = 0;
-		$max_name_len = 0;
+		$fontSize = 8;
 
-		for ($i = 0; $i < $this->num; $i++) {
-			if (zbx_strlen($this->items[$i]['hostname']) > $max_host_len) {
-				$max_host_len = zbx_strlen($this->items[$i]['hostname']);
-			}
-			if (zbx_strlen($this->items[$i]['name_expanded']) > $max_name_len) {
-				$max_name_len = zbx_strlen($this->items[$i]['name_expanded']);
+		// check if host name will be displayed
+		$displayHostName = (count(array_unique(zbx_objectValues($this->items, 'hostname'))) > 1);
+
+		// calculate function name X shift
+		$functionNameXShift = 0;
+
+		foreach ($this->items as $item) {
+			$name = $displayHostName ? $item['hostname'].': '.$item['name_expanded'] : $item['name_expanded'];
+			$dims = imageTextSize($fontSize, 0, $name);
+
+			if ($dims['width'] > $functionNameXShift) {
+				$functionNameXShift = $dims['width'];
 			}
 		}
 
-		for ($i = 0; $i < $this->num; $i++) {
-			$color = $this->getColor($this->items[$i]['color'], 0);
-			$type = $this->items[$i]['calc_type'];
+		// display items
+		$i = 0;
 
-			$data = &$this->data[$this->items[$i]['itemid']][$type];
+		foreach ($this->items as $item) {
+			$color = $this->getColor($item['color'], 0);
 
-			switch ($this->items[$i]['calc_fnc']) {
+			// function name
+			switch ($item['calc_fnc']) {
 				case CALC_FNC_MIN:
 					$fncName = 'min';
 					$fncRealName = _('min');
@@ -319,33 +325,51 @@
 					$fncName = 'avg';
 					$fncRealName = _('avg');
 			}
-			$datavalue = $this->data[$this->items[$i]['itemid']][$type][$fncName];
 
-			$proc = $this->sum == 0 ? 0 : ($datavalue * 100) / $this->sum;
+			if (isset($this->data[$item['itemid']][$item['calc_type']])
+					&& isset($this->data[$item['itemid']][$item['calc_type']][$fncName])) {
+				$dataValue = $this->data[$item['itemid']][$item['calc_type']][$fncName];
+				$proc = ($this->sum == 0) ? 0 : ($dataValue * 100) / $this->sum;
 
-			if (isset($data) && isset($datavalue)) {
-				$strvalue = sprintf(_('Value').': %s ('.(round($proc) != round($proc, 2) ? '%0.2f' : '%0.0f').'%%)',
+				$strValue = sprintf(_('Value').': %s ('.(round($proc) != round($proc, 2) ? '%0.2f' : '%0.0f').'%%)',
 					convert_units(array(
-						'value' => $datavalue,
+						'value' => $dataValue,
 						'units' => $this->items[$i]['units']
 					)),
 					$proc
 				);
 
-				$str = sprintf('%s: %s [%s] ',
-					str_pad($this->items[$i]['hostname'], $max_host_len, ' '),
-					str_pad($this->items[$i]['name_expanded'], $max_name_len, ' '),
-					$fncRealName
-				);
+				$str = '['.$fncRealName.']';
 			}
 			else {
-				$strvalue = sprintf(_('Value: no data'));
-				$str = sprintf('%s: %s [ '._('no data').' ]',
-					str_pad($this->items[$i]['hostname'], $max_host_len, ' '),
-					str_pad($this->items[$i]['name_expanded'], $max_name_len, ' ')
-				);
+				$strValue = _('Value: no data');
+
+				$str = '['._('no data').']';
 			}
 
+			// item name
+			imageText(
+				$this->im,
+				$fontSize,
+				0,
+				$this->shiftXleft + 15,
+				$this->sizeY + $shiftY + 14 * $i + 5,
+				$this->getColor($this->graphtheme['textcolor'], 0),
+				$displayHostName ? $item['hostname'].': '.$item['name_expanded'] : $item['name_expanded']
+			);
+
+			// function name
+			imageText(
+				$this->im,
+				$fontSize,
+				0,
+				$this->shiftXleft + $functionNameXShift + 30,
+				$this->sizeY + $shiftY + 14 * $i + 5,
+				$this->getColor($this->graphtheme['textcolor'], 0),
+				$str
+			);
+
+			// left square fill
 			imagefilledrectangle(
 				$this->im,
 				$this->shiftXleft,
@@ -355,6 +379,7 @@
 				$color
 			);
 
+			// left square frame
 			imagerectangle(
 				$this->im,
 				$this->shiftXleft,
@@ -364,18 +389,9 @@
 				$this->getColor('Black No Alpha')
 			);
 
-			imageText(
-				$this->im,
-				8,
-				0,
-				$this->shiftXleft + 15,
-				$this->sizeY + $shiftY + 14 * $i + 5,
-				$this->getColor($this->graphtheme['textcolor'], 0),
-				$str
-			);
-
 			$shiftX = $this->fullSizeX - $this->shiftlegendright - $this->shiftXright + 25;
 
+			// right square fill
 			imagefilledrectangle(
 				$this->im,
 				$shiftX - 10,
@@ -385,6 +401,7 @@
 				$color
 			);
 
+			// right square frame
 			imagerectangle(
 				$this->im,
 				$shiftX - 10,
@@ -394,15 +411,18 @@
 				$this->GetColor('Black No Alpha')
 			);
 
+			// item value
 			imagetext(
 				$this->im,
-				8,
+				$fontSize,
 				0,
 				$shiftX + 5,
 				$this->shiftY + 10 + 14 * $i + 10,
 				$this->getColor($this->graphtheme['textcolor'], 0),
-				$strvalue
+				$strValue
 			);
+
+			$i++;
 		}
 
 		if ($this->sizeY < 120) {
