1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22/*
23 * Class to draw graph line. Single data points will be drawn as points instead of lines.
24 */
25class CSvgGraphLineGroup extends CSvgGroup {
26
27	protected $paths;
28	protected $metric;
29	protected $options;
30
31	public function __construct($paths, $metric) {
32		parent::__construct();
33
34		$this->paths = $paths;
35		$this->metric = $metric;
36
37		$this->options = $metric['options'] + [
38			'transparency' => CSvgGraph::SVG_GRAPH_DEFAULT_TRANSPARENCY,
39			'width' => CSvgGraph::SVG_GRAPH_DEFAULT_LINE_WIDTH,
40			'color' => CSvgGraph::SVG_GRAPH_DEFAULT_COLOR,
41			'type' => SVG_GRAPH_TYPE_LINE,
42			'order' => 1
43		];
44
45		// Minimal point size is 3 to make single data points visible even for thin lines.
46		$this->options['pointsize'] = max($this->options['width'], 3);
47	}
48
49	public function makeStyles() {
50		$this
51			->addClass(CSvgTag::ZBX_STYLE_GRAPH_LINE)
52			->addClass(CSvgTag::ZBX_STYLE_GRAPH_LINE.'-'.$this->metric['itemid'].'-'.$this->options['order']);
53
54		$line_style = ($this->options['type'] == SVG_GRAPH_TYPE_LINE) ? ['stroke-linejoin' => 'round'] : [];
55
56		return [
57			'.'.CSvgTag::ZBX_STYLE_GRAPH_LINE => [
58				'fill' => 'none'
59			],
60			'.'.CSvgTag::ZBX_STYLE_GRAPH_LINE.'-'.$this->metric['itemid'].'-'.$this->options['order'] => [
61				'stroke-opacity' => $this->options['transparency'] * 0.1,
62				'stroke' => $this->options['color'],
63				'stroke-width' => $this->options['width']
64			] + $line_style,
65			'.'.CSvgTag::ZBX_STYLE_GRAPH_LINE.'-'.$this->metric['itemid'].'-'.$this->options['order'].' circle' => [
66				'fill-opacity' => $this->options['transparency'] * 0.1,
67				'fill' => $this->options['color'],
68				'stroke-width' => 0
69			]
70		];
71	}
72
73	protected function draw() {
74		foreach ($this->paths as $path) {
75			// Draw single data point paths as circles instead of lines.
76			$this->addItem((count($path) > 1)
77				? new CSvgGraphLine($path, $this->metric)
78				: (new CSvgCircle($path[0][0], $path[0][1], $this->options['pointsize']))
79					->setAttribute('label', $path[0][2])
80			);
81		}
82	}
83
84	public function toString($destroy = true) {
85		$this->setAttribute('data-set', $this->options['type'] == SVG_GRAPH_TYPE_LINE ? 'line' : 'staircase')
86			->setAttribute('data-metric', CHtml::encode($this->metric['name']))
87			->setAttribute('data-color', $this->options['color'])
88			->addItem((new CSvgCircle(-10, -10, $this->options['width'] + 4))
89				->addClass(CSvgTag::ZBX_STYLE_GRAPH_HIGHLIGHTED_VALUE))
90			->draw();
91
92		return parent::toString($destroy);
93	}
94}
95