--- ./include/defines.inc.php.orig	2015-08-11 04:43:33.000000000 +0200
+++ ./include/defines.inc.php	2015-10-24 17:22:56.000000000 +0200
@@ -954,3 +954,6 @@
 if (function_exists('bcscale')) {
 	bcscale(7);
 }
+
+define('GRAPH_LEGEND_TYPE_DEFAULT',	0);
+define('GRAPH_LEGEND_TYPE_COMPACT',	1);
--- ./include/classes/graphdraw/CLineGraphDraw.php.orig	2015-08-10 12:37:30.000000000 +0200
+++ ./include/classes/graphdraw/CLineGraphDraw.php	2015-10-25 13:52:03.000000000 +0100
@@ -57,6 +57,9 @@
 		$this->gridStep = array(); // grid step
 		$this->gridPixels = 25; // optimal grid size
 		$this->gridPixelsVert = 40;
+		$this->legendType = GRAPH_LEGEND_TYPE_DEFAULT;
+		$this->legendRows = 3;
+		$this->legendMaxChars = 20;
 	}
 
 	/********************************************************************************************************/
@@ -178,6 +181,18 @@
 		$this->percentile['right']['percent'] = $percentile;
 	}
 
+	public function setLegendType($legendType) {
+		$this->legendType = $legendType;
+	}
+
+	public function setLegendRows($legendRows) {
+		$this->legendRows = $legendRows;
+	}
+
+	public function setLegendMaxChars($legendMaxChars) {
+		$this->legendMaxChars = $legendMaxChars;
+	}
+
 	protected function selectData() {
 		$this->data = array();
 		$now = time(null);
@@ -1825,17 +1840,20 @@
 		$legend->fontsize = 9;
 
 		// item legend table header
-		$row = array(
-			array('text' => '', 'marginRight' => 5),
-			array('text' => ''),
-			array('text' => ''),
-			array('text' => _('last'), 'align' => 1, 'fontsize' => 9),
-			array('text' => _('min'), 'align' => 1, 'fontsize' => 9),
-			array('text' => _('avg'), 'align' => 1, 'fontsize' => 9),
-			array('text' => _('max'), 'align' => 1, 'fontsize' => 9)
-		);
+		if (GRAPH_LEGEND_TYPE_DEFAULT == $this->legendType) {
+			$row = array(
+				array('text' => '', 'marginRight' => 5),
+				array('text' => ''),
+				array('text' => ''),
+				array('text' => _('last'), 'align' => 1, 'fontsize' => 9),
+				array('text' => _('min'), 'align' => 1, 'fontsize' => 9),
+				array('text' => _('avg'), 'align' => 1, 'fontsize' => 9),
+				array('text' => _('max'), 'align' => 1, 'fontsize' => 9)
+			);
+
+			$legend->addRow($row);
+		}
 
-		$legend->addRow($row);
 		$rowNum = $legend->getNumRows();
 
 		$i = ($this->type == GRAPH_TYPE_STACKED) ? $this->num - 1 : 0;
@@ -1875,56 +1893,67 @@
 				? $this->items[$i]['name_expanded']
 				: $this->items[$i]['hostname'].NAME_DELIMITER.$this->items[$i]['name_expanded'];
 
-			// draw legend of an item with data
-			if (isset($data) && isset($data['min'])) {
-				if ($this->items[$i]['axisside'] == GRAPH_YAXIS_SIDE_LEFT) {
-					$units['left'] = $this->items[$i]['units'];
-				}
-				else {
-					$units['right'] = $this->items[$i]['units'];
+			if (GRAPH_LEGEND_TYPE_COMPACT == $this->legendType) {
+				if (zbx_strlen($itemCaption) > $this->legendMaxChars)
+				{
+					$itemCaption = zbx_substr($itemCaption, 0, $this->legendMaxChars).'...';
 				}
 
-				$legend->addCell($rowNum, array('image' => $colorSquare, 'marginRight' => 5));
-				$legend->addCell($rowNum, array('text' => $itemCaption));
-				$legend->addCell($rowNum, array('text' => '['.$fncRealName.']'));
-				$legend->addCell($rowNum, array(
-					'text' => convert_units(array(
-						'value' => $this->getLastValue($i),
-						'units' => $this->items[$i]['units'],
-						'convert' => ITEM_CONVERT_NO_UNITS
-					)),
-					'align' => 2
-				));
-				$legend->addCell($rowNum, array(
-					'text' => convert_units(array(
-						'value' => min($data['min']),
-						'units' => $this->items[$i]['units'],
-						'convert' => ITEM_CONVERT_NO_UNITS
-					)),
-					'align' => 2
-				));
-				$legend->addCell($rowNum, array(
-					'text' => convert_units(array(
-						'value' => $data['avg_orig'],
-						'units' => $this->items[$i]['units'],
-						'convert' => ITEM_CONVERT_NO_UNITS
-					)),
-					'align' => 2
-				));
-				$legend->addCell($rowNum, array(
-					'text' => convert_units(array(
-						'value' => max($data['max']),
-						'units' => $this->items[$i]['units'],
-						'convert' => ITEM_CONVERT_NO_UNITS
-					)),
-					'align' => 2
-				));
+				$legend->addCell($rowNum % $this->legendRows, array('image' => $colorSquare, 'marginRight' => 5));
+				$legend->addCell($rowNum % $this->legendRows, array('text' => $itemCaption));
 			}
-			// draw legend of an item without data
 			else {
-				$legend->addCell($rowNum, array('image' => $colorSquare, 'marginRight' => 5));
-				$legend->addCell($rowNum, array('text' => $itemCaption));
-				$legend->addCell($rowNum, array('text' => '[ '._('no data').' ]'));
+				// draw legend of an item with data
+				if (isset($data) && isset($data['min'])) {
+					if ($this->items[$i]['axisside'] == GRAPH_YAXIS_SIDE_LEFT) {
+						$units['left'] = $this->items[$i]['units'];
+					}
+					else {
+						$units['right'] = $this->items[$i]['units'];
+					}
+
+					$legend->addCell($rowNum, array('image' => $colorSquare, 'marginRight' => 5));
+					$legend->addCell($rowNum, array('text' => $itemCaption));
+					$legend->addCell($rowNum, array('text' => '['.$fncRealName.']'));
+					$legend->addCell($rowNum, array(
+						'text' => convert_units(array(
+							'value' => $this->getLastValue($i),
+							'units' => $this->items[$i]['units'],
+							'convert' => ITEM_CONVERT_NO_UNITS
+						)),
+						'align' => 2
+					));
+					$legend->addCell($rowNum, array(
+						'text' => convert_units(array(
+							'value' => min($data['min']),
+							'units' => $this->items[$i]['units'],
+							'convert' => ITEM_CONVERT_NO_UNITS
+						)),
+						'align' => 2
+					));
+					$legend->addCell($rowNum, array(
+						'text' => convert_units(array(
+							'value' => $data['avg_orig'],
+							'units' => $this->items[$i]['units'],
+							'convert' => ITEM_CONVERT_NO_UNITS
+						)),
+						'align' => 2
+					));
+					$legend->addCell($rowNum, array(
+						'text' => convert_units(array(
+							'value' => max($data['max']),
+							'units' => $this->items[$i]['units'],
+							'convert' => ITEM_CONVERT_NO_UNITS
+						)),
+						'align' => 2
+					));
+				}
+				// draw legend of an item without data
+				else {
+					$legend->addCell($rowNum, array('image' => $colorSquare, 'marginRight' => 5));
+					$legend->addCell($rowNum, array('text' => $itemCaption));
+					$legend->addCell($rowNum, array('text' => '[ '._('no data').' ]'));
+				}
 			}
 
 			$rowNum++;
@@ -2001,6 +2030,11 @@
 
 		$legend->draw();
 
+		if (GRAPH_LEGEND_TYPE_COMPACT == $this->legendType)
+		{
+			return true;
+		}
+
 		$legend = new CImageTextTable(
 			$this->im,
 			$leftXShift + 10,
@@ -2383,7 +2417,12 @@
 		$this->fullSizeY = $this->sizeY + $this->shiftY + $this->legendOffsetY;
 
 		if ($this->drawLegend) {
-			$this->fullSizeY += 14 * ($this->num + 1 + (($this->sizeY < 120) ? 0 : count($this->triggers))) + 8;
+			if (GRAPH_LEGEND_TYPE_COMPACT == $this->legendType) {
+				$this->fullSizeY += 14 * ($this->legendRows) + 8;
+			}
+			else {
+				$this->fullSizeY += 14 * ($this->num + 1 + (($this->sizeY < 120) ? 0 : count($this->triggers))) + 8;
+			}
 		}
 
 		// if graph height is big enough, we reserve space for percent line legend
--- ./chart2.php.orig	2015-08-10 12:37:30.000000000 +0200
+++ ./chart2.php	2015-10-25 13:48:46.000000000 +0100
@@ -37,7 +37,8 @@
 	'updateProfile' =>	array(T_ZBX_STR, O_OPT, null,		null,		null),
 	'border' =>			array(T_ZBX_INT, O_OPT, P_NZERO,	IN('0,1'),	null),
 	'width' =>			array(T_ZBX_INT, O_OPT, P_NZERO,	'{}>0',		null),
-	'height' =>			array(T_ZBX_INT, O_OPT, P_NZERO,	'{}>0',		null)
+	'height' =>			array(T_ZBX_INT, O_OPT, P_NZERO,	'{}>0',		null),
+	'legendType' =>			array(T_ZBX_INT, O_OPT, P_NZERO,	IN('0,1'),	null)
 );
 check_fields($fields);
 
@@ -100,6 +101,7 @@
 if ($height <= 0) {
 	$height = $dbGraph['height'];
 }
+$legendType = get_request('legendType', GRAPH_LEGEND_TYPE_DEFAULT);
 
 $graph->showLegend($dbGraph['show_legend']);
 $graph->showWorkPeriod($dbGraph['show_work_period']);
@@ -130,6 +132,50 @@
 		$dbGraphItem['drawtype'],
 		$dbGraphItem['type']
 	);
+
+	if (GRAPH_YAXIS_SIDE_LEFT == $dbGraphItem['yaxisside'])
+	{
+		$yAxisLeft = 1;
+	}
+	elseif (GRAPH_YAXIS_SIDE_RIGHT == $dbGraphItem['yaxisside'])
+	{
+		$yAxisRight = 1;
+	}
+
+	$itemCount++;
+}
+
+if (GRAPH_LEGEND_TYPE_COMPACT == $legendType) {
+	$suppEntLeng = 200;
+	$entCharsDefault = 20;
+	$entCharsMax = $entCharsDefault;
+
+	if ($yAxisLeft && $yAxisRight)
+	{
+		$widthCorr = 170;
+	}
+	else
+	{
+		$widthCorr = 115;
+	}
+
+	$legendCols = round(($width + $widthCorr) / $suppEntLeng, 0);
+
+	if (1 > $legendCols)
+	{
+		$legendCols = 1;
+	}
+
+	if ($legendCols > $itemCount)
+	{
+		$entCharsMax = floor($legendCols / $itemCount * $entCharsDefault);
+	}
+
+	$legendRows = ceil($itemCount / $legendCols);
+
+	$graph->setLegendType($legendType);
+	$graph->setLegendRows($legendRows);
+	$graph->setLegendMaxChars($entCharsMax);
 }
 
 $graph->draw();
--- ./chart3.php.orig	2015-08-10 12:37:30.000000000 +0200
+++ ./chart3.php	2015-10-26 00:29:30.000000000 +0100
@@ -50,7 +50,8 @@
 	'yaxismax' =>		array(T_ZBX_DBL, O_OPT, null,		null,				null),
 	'percent_left' =>	array(T_ZBX_DBL, O_OPT, null,		BETWEEN(0, 100),	null),
 	'percent_right' =>	array(T_ZBX_DBL, O_OPT, null,		BETWEEN(0, 100),	null),
-	'items' =>			array(T_ZBX_STR, O_OPT, null,		null,				null)
+	'items' =>			array(T_ZBX_STR, O_OPT, null,		null,				null),
+	'legendType' =>		array(T_ZBX_INT, O_OPT, P_NZERO,		IN('0,1'),			null)
 );
 $isDataValid = check_fields($fields);
 
@@ -138,6 +139,8 @@
 
 	CProfile::update($profileIdx.'.httptestid', $profileIdx2, PROFILE_TYPE_ID);
 
+	$legendType = get_request('legendType', GRAPH_LEGEND_TYPE_DEFAULT);
+
 	$graph = new CLineGraphDraw(getRequest('graphtype', GRAPH_TYPE_NORMAL));
 	$graph->setHeader($name);
 	$graph->setPeriod($timeline['period']);
@@ -165,6 +168,51 @@
 			isset($item['drawtype']) ? $item['drawtype'] : null,
 			isset($item['type']) ? $item['type'] : null
 		);
+
+		if (GRAPH_YAXIS_SIDE_LEFT == $item['yaxisside'])
+		{
+			$yAxisLeft = 1;
+		}
+		elseif (GRAPH_YAXIS_SIDE_RIGHT == $item['yaxisside'])
+		{
+			$yAxisRight = 1;
+		}
+
+		$itemCount++;
+	}
+
+	if (GRAPH_LEGEND_TYPE_COMPACT == $legendType) {
+		$width = getRequest('width', 900);
+		$suppEntLeng = 200;
+		$entCharsDefault = 20;
+		$entCharsMax = $entCharsDefault;
+
+		if ($yAxisLeft && $yAxisRight)
+		{
+			$widthCorr = 170;
+		}
+		else
+		{
+			$widthCorr = 115;
+		}
+
+		$legendCols = round(($width + $widthCorr) / $suppEntLeng, 0);
+
+		if (1 > $legendCols)
+		{
+			$legendCols = 1;
+		}
+
+		if ($legendCols > $itemCount)
+		{
+			$entCharsMax = floor($legendCols / $itemCount * $entCharsDefault);
+		}
+
+		$legendRows = ceil($itemCount / $legendCols);
+
+		$graph->setLegendType($legendType);
+		$graph->setLegendRows($legendRows);
+		$graph->setLegendMaxChars($entCharsMax);
 	}
 
 	$graph->draw();
--- ./include/classes/screens/CScreenGraph.php.orig	2015-08-10 12:37:30.000000000 +0200
+++ ./include/classes/screens/CScreenGraph.php	2015-10-26 00:36:04.000000000 +0100
@@ -95,7 +95,7 @@
 			// get url
 			$this->screenitem['url'] = ($graph['graphtype'] == GRAPH_TYPE_PIE || $graph['graphtype'] == GRAPH_TYPE_EXPLODED)
 				? 'chart7.php'
-				: 'chart3.php';
+				: 'chart3.php?legendType='.GRAPH_LEGEND_TYPE_COMPACT;
 			$this->screenitem['url'] = new CUrl($this->screenitem['url']);
 
 			foreach ($graph as $name => $value) {
@@ -147,7 +147,8 @@
 		}
 		else {
 			if ($this->screenitem['dynamic'] == SCREEN_SIMPLE_ITEM || !$this->screenitem['url']) {
-				$this->screenitem['url'] = 'chart2.php?graphid='.$resourceId.'&screenid='.$this->screenitem['screenid'];
+				$this->screenitem['url'] = 'chart2.php?graphid='.$resourceId.'&screenid='.$this->screenitem['screenid'].
+					'&legendType='.GRAPH_LEGEND_TYPE_COMPACT;
 				$isDefault = true;
 			}
 
