1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\ImageGraph;
use CpChart\Chart\Data;
use CpChart\Chart\Image;
use Piwik\Container\StaticContainer;
use Piwik\NumberFormatter;
use Piwik\Piwik;
use Piwik\BaseFactory;
/**
* The StaticGraph abstract class is used as a base class for different types of static graphs.
*
*/
abstract class StaticGraph extends BaseFactory
{
const GRAPH_TYPE_BASIC_LINE = "evolution";
const GRAPH_TYPE_VERTICAL_BAR = "verticalBar";
const GRAPH_TYPE_HORIZONTAL_BAR = "horizontalBar";
const GRAPH_TYPE_3D_PIE = "3dPie";
const GRAPH_TYPE_BASIC_PIE = "pie";
private static $availableStaticGraphTypes = array(
self::GRAPH_TYPE_BASIC_LINE => 'Evolution',
self::GRAPH_TYPE_VERTICAL_BAR => 'VerticalBar',
self::GRAPH_TYPE_HORIZONTAL_BAR => 'HorizontalBar',
self::GRAPH_TYPE_BASIC_PIE => 'Pie',
self::GRAPH_TYPE_3D_PIE => 'Pie3D',
);
const ABSCISSA_SERIE_NAME = 'ABSCISSA';
private $aliasedGraph;
/**
* @var Image
*/
protected $pImage;
/**
* @var Data
*/
protected $pData;
protected $ordinateLabels;
protected $showLegend;
protected $abscissaSeries;
protected $abscissaLogos;
protected $ordinateSeries;
protected $ordinateLogos;
protected $colors;
protected $font;
protected $fontSize;
protected $textColor = [];
protected $backgroundColor = [];
protected $gridColor = [];
protected $legendFontSize;
protected $width;
protected $height;
protected $forceSkippedLabels = false;
abstract protected function getDefaultColors();
abstract public function renderGraph();
protected static function getClassNameFromClassId($graphType)
{
$className = self::$availableStaticGraphTypes[$graphType];
$className = __NAMESPACE__ . "\\StaticGraph\\" . $className;
return $className;
}
protected static function getInvalidClassIdExceptionMessage($graphType)
{
return Piwik::translate(
'General_ExceptionInvalidStaticGraphType',
array($graphType, implode(', ', self::getAvailableStaticGraphTypes()))
);
}
public static function getAvailableStaticGraphTypes()
{
return array_keys(self::$availableStaticGraphTypes);
}
/**
* Save rendering to disk
*
* @param string $filename without path
* @return string path of file
*/
public function sendToDisk($filename)
{
$filePath = self::getOutputPath($filename);
$this->pImage->render($filePath);
return $filePath;
}
/**
* @return resource rendered static graph
*/
public function getRenderedImage()
{
return $this->pImage->Picture;
}
/**
* Output rendering to browser
*/
public function sendToBrowser()
{
$this->pImage->stroke();
}
public function setWidth($width)
{
$this->width = $width;
}
public function setHeight($height)
{
$this->height = $height;
}
public function setFontSize($fontSize)
{
if (!is_numeric($fontSize)) {
$fontSize = API::DEFAULT_FONT_SIZE;
}
$this->fontSize = $fontSize;
}
public function setLegendFontSize($legendFontSize)
{
$this->legendFontSize = $legendFontSize;
}
public function setFont($font)
{
$this->font = $font;
}
public function setTextColor($textColor)
{
$this->textColor = self::hex2rgb($textColor);
}
public function setBackgroundColor($backgroundColor)
{
$this->backgroundColor = self::hex2rgb($backgroundColor);
}
public function setGridColor($gridColor)
{
$this->gridColor = self::hex2rgb($gridColor);
}
public function setOrdinateSeries($ordinateSeries)
{
$this->ordinateSeries = $ordinateSeries;
}
public function setOrdinateLogos($ordinateLogos)
{
$this->ordinateLogos = $ordinateLogos;
}
public function setAbscissaLogos($abscissaLogos)
{
$this->abscissaLogos = $abscissaLogos;
}
public function setAbscissaSeries($abscissaSeries)
{
$this->abscissaSeries = $abscissaSeries;
}
public function setShowLegend($showLegend)
{
$this->showLegend = $showLegend;
}
public function setForceSkippedLabels($forceSkippedLabels)
{
$this->forceSkippedLabels = $forceSkippedLabels;
}
public function setOrdinateLabels($ordinateLabels)
{
$this->ordinateLabels = $ordinateLabels;
}
public function setAliasedGraph($aliasedGraph)
{
$this->aliasedGraph = $aliasedGraph;
}
public function setColors($colors)
{
$i = 0;
foreach ($this->getDefaultColors() as $colorKey => $defaultColor) {
if (isset($colors[$i]) && $this->hex2rgb($colors[$i])) {
$hexColor = $colors[$i];
} else {
$hexColor = $defaultColor;
}
$this->colors[$colorKey] = $this->hex2rgb($hexColor);
$i++;
}
}
/**
* Return $filename with temp directory and delete file
*
* @static
* @param $filename
* @return string path of file in temp directory
*/
protected static function getOutputPath($filename)
{
$outputFilename = StaticContainer::get('path.tmp') . '/assets/' . $filename;
@chmod($outputFilename, 0600);
@unlink($outputFilename);
return $outputFilename;
}
protected function initpData()
{
$this->pData = new Data();
foreach ($this->ordinateSeries as $column => $data) {
$this->pData->addPoints($data, $column);
$this->pData->setSerieDescription($column, $this->ordinateLabels[$column]);
if (isset($this->ordinateLogos[$column])) {
$ordinateLogo = $this->createResizedImageCopyIfNeeded($this->ordinateLogos[$column]);
$this->pData->setSeriePicture($column, $ordinateLogo);
}
}
$this->pData->setAxisDisplay(0, AXIS_FORMAT_CUSTOM, '\\Piwik\\Plugins\\ImageGraph\\formatYAxis');
$this->pData->addPoints($this->abscissaSeries, self::ABSCISSA_SERIE_NAME);
$this->pData->setAbscissa(self::ABSCISSA_SERIE_NAME);
}
protected function createResizedImageCopyIfNeeded($image)
{
$size = getimagesize($image);
if ($size[0] <= 16 && $size[1] <= 16) {
return $image; // use original image if size fits
}
$ratio = $size[0] / $size[1];
if ($ratio > 1) {
$width = 16;
$height = 16 / $ratio;
} else {
$width = 16 * $ratio;
$height = 16;
}
$newImage = self::getOutputPath(md5($image) . '.png');
$src = imagecreatefromstring(file_get_contents($image));
$dst = imagecreatetruecolor($width, $height);
imagesavealpha($dst, true);
$color = imagecolorallocatealpha($dst, 0, 0, 0, 127);
imagefill($dst, 0, 0, $color);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
imagepng($dst, $newImage);
imagedestroy($dst);
return $newImage;
}
protected function initpImage()
{
$this->pImage = new Image($this->width, $this->height, $this->pData);
$this->pImage->Antialias = $this->aliasedGraph;
$this->pImage->setFontProperties(
array_merge(
array(
'FontName' => $this->font,
'FontSize' => $this->fontSize,
),
$this->textColor
)
);
}
protected function getTextWidthHeight($text, $fontSize = false)
{
if (!$fontSize) {
$fontSize = $this->fontSize;
}
if (!$this->pImage) {
$this->initpImage();
}
// could not find a way to get pixel perfect width & height info using imageftbbox
$textInfo = $this->pImage->drawText(
0, 0, $text,
array(
'Alpha' => 0,
'FontSize' => $fontSize,
'FontName' => $this->font
)
);
return array($textInfo[1]['X'] + 1, $textInfo[0]['Y'] - $textInfo[2]['Y']);
}
protected function getMaximumTextWidthHeight($values)
{
if (array_values($values) === $values) {
$values = array('' => $values);
}
$maxWidth = 0;
$maxHeight = 0;
foreach ($values as $data) {
foreach ($data as $value) {
list($valueWidth, $valueHeight) = $this->getTextWidthHeight($value);
if ($valueWidth > $maxWidth) {
$maxWidth = $valueWidth;
}
if ($valueHeight > $maxHeight) {
$maxHeight = $valueHeight;
}
}
}
return array($maxWidth, $maxHeight);
}
protected function drawBackground()
{
$this->pImage->drawFilledRectangle(
0,
0,
$this->width,
$this->height,
array_merge(array('Alpha' => 100), $this->backgroundColor)
);
}
private static function hex2rgb($hexColor)
{
if (preg_match('/([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/', $hexColor, $matches)) {
return array(
'R' => hexdec($matches[1]),
'G' => hexdec($matches[2]),
'B' => hexdec($matches[3])
);
} else {
return [];
}
}
}
/**
* Global format method
*
* required to format y axis values using CpChart internal format callbacks
* @param $value
* @return mixed
*/
function formatYAxis($value)
{
return NumberFormatter::getInstance()->format($value);
}
|