Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Graph.php
Go to the documentation of this file.
1 <?php
7 
13 class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
14 {
18  const API_URL = 'http://chart.apis.google.com/chart';
19 
25  protected $_allSeries = [];
26 
32  protected $_axisLabels = [];
33 
39  protected $_axisMaps = [];
40 
46  protected $_dataRows = [];
47 
53  protected $_simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
54 
60  protected $_extendedEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
61 
67  protected $_width = '780';
68 
74  protected $_height = '384';
75 
81  protected $_encoding = 'e';
82 
88  protected $_htmlId = '';
89 
93  protected $_template = 'Magento_Backend::dashboard/graph.phtml';
94 
100  protected $_dashboardData = null;
101 
108  public function __construct(
109  \Magento\Backend\Block\Template\Context $context,
110  \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
111  \Magento\Backend\Helper\Dashboard\Data $dashboardData,
112  array $data = []
113  ) {
114  $this->_dashboardData = $dashboardData;
115  parent::__construct($context, $collectionFactory, $data);
116  }
117 
123  protected function _getTabTemplate()
124  {
125  return 'dashboard/graph.phtml';
126  }
127 
134  public function setDataRows($rows)
135  {
136  $this->_dataRows = (array)$rows;
137  }
138 
146  public function addSeries($seriesId, array $options)
147  {
148  $this->_allSeries[$seriesId] = $options;
149  }
150 
157  public function getSeries($seriesId)
158  {
159  if (isset($this->_allSeries[$seriesId])) {
160  return $this->_allSeries[$seriesId];
161  } else {
162  return false;
163  }
164  }
165 
171  public function getAllSeries()
172  {
173  return $this->_allSeries;
174  }
175 
186  public function getChartUrl($directUrl = true)
187  {
188  $params = [
189  'cht' => 'lc',
190  'chf' => 'bg,s,ffffff',
191  'chco' => 'ef672f',
192  'chls' => '7',
193  'chxs' => '0,676056,15,0,l,676056|1,676056,15,0,l,676056',
194  'chm' => 'h,f2ebde,0,0:1:.1,1,-1',
195  ];
196 
197  $this->_allSeries = $this->getRowsData($this->_dataRows);
198 
199  foreach ($this->_axisMaps as $axis => $attr) {
200  $this->setAxisLabels($axis, $this->getRowsData($attr, true));
201  }
202 
203  $timezoneLocal = $this->_localeDate->getConfigTimezone();
204 
207  list($dateStart, $dateEnd) = $this->_collectionFactory->create()->getDateRange(
208  $this->getDataHelper()->getParam('period'),
209  '',
210  '',
211  true
212  );
213 
214  $dateStart->setTimezone(new \DateTimeZone($timezoneLocal));
215  $dateEnd->setTimezone(new \DateTimeZone($timezoneLocal));
216 
217  if ($this->getDataHelper()->getParam('period') == '24h') {
218  $dateEnd->modify('-1 hour');
219  } else {
220  $dateEnd->setTime(23, 59, 59);
221  $dateStart->setTime(0, 0, 0);
222  }
223 
224  $dates = [];
225  $datas = [];
226 
227  while ($dateStart <= $dateEnd) {
228  switch ($this->getDataHelper()->getParam('period')) {
229  case '7d':
230  case '1m':
231  $d = $dateStart->format('Y-m-d');
232  $dateStart->modify('+1 day');
233  break;
234  case '1y':
235  case '2y':
236  $d = $dateStart->format('Y-m');
237  $dateStart->modify('+1 month');
238  break;
239  default:
240  $d = $dateStart->format('Y-m-d H:00');
241  $dateStart->modify('+1 hour');
242  }
243  foreach ($this->getAllSeries() as $index => $serie) {
244  if (in_array($d, $this->_axisLabels['x'])) {
245  $datas[$index][] = (double)array_shift($this->_allSeries[$index]);
246  } else {
247  $datas[$index][] = 0;
248  }
249  }
250  $dates[] = $d;
251  }
252 
256  if (count($dates) > 8 && count($dates) < 15) {
257  $c = 1;
258  } else {
259  if (count($dates) >= 15) {
260  $c = 2;
261  } else {
262  $c = 0;
263  }
264  }
268  $i = 0;
269  foreach ($dates as $k => $d) {
270  if ($i == $c) {
271  $dates[$k] = $d;
272  $i = 0;
273  } else {
274  $dates[$k] = '';
275  $i++;
276  }
277  }
278 
279  $this->_axisLabels['x'] = $dates;
280  $this->_allSeries = $datas;
281 
282  //Google encoding values
283  if ($this->_encoding == "s") {
284  // simple encoding
285  $params['chd'] = "s:";
286  $dataDelimiter = "";
287  $dataSetdelimiter = ",";
288  $dataMissing = "_";
289  } else {
290  // extended encoding
291  $params['chd'] = "e:";
292  $dataDelimiter = "";
293  $dataSetdelimiter = ",";
294  $dataMissing = "__";
295  }
296 
297  // process each string in the array, and find the max length
298  $localmaxvalue = [0];
299  $localminvalue = [0];
300  foreach ($this->getAllSeries() as $index => $serie) {
301  $localmaxvalue[$index] = max($serie);
302  $localminvalue[$index] = min($serie);
303  }
304 
305  $maxvalue = max($localmaxvalue);
306  $minvalue = min($localminvalue);
307 
308  // default values
309  $yrange = 0;
310  $yLabels = [];
311  $miny = 0;
312  $maxy = 0;
313  $yorigin = 0;
314 
315  if ($minvalue >= 0 && $maxvalue >= 0) {
316  if ($maxvalue > 10) {
317  $p = pow(10, $this->_getPow($maxvalue));
318  $maxy = ceil($maxvalue / $p) * $p;
319  $yLabels = range($miny, $maxy, $p);
320  } else {
321  $maxy = ceil($maxvalue + 1);
322  $yLabels = range($miny, $maxy, 1);
323  }
324  $yrange = $maxy;
325  $yorigin = 0;
326  }
327 
328  $chartdata = [];
329 
330  foreach ($this->getAllSeries() as $index => $serie) {
331  $thisdataarray = $serie;
332  if ($this->_encoding == "s") {
333  // SIMPLE ENCODING
334  for ($j = 0; $j < sizeof($thisdataarray); $j++) {
335  $currentvalue = $thisdataarray[$j];
336  if (is_numeric($currentvalue)) {
337  $ylocation = round(
338  (strlen($this->_simpleEncoding) - 1) * ($yorigin + $currentvalue) / $yrange
339  );
340  $chartdata[] = substr($this->_simpleEncoding, $ylocation, 1) . $dataDelimiter;
341  } else {
342  $chartdata[] = $dataMissing . $dataDelimiter;
343  }
344  }
345  } else {
346  // EXTENDED ENCODING
347  for ($j = 0; $j < sizeof($thisdataarray); $j++) {
348  $currentvalue = $thisdataarray[$j];
349  if (is_numeric($currentvalue)) {
350  if ($yrange) {
351  $ylocation = 4095 * ($yorigin + $currentvalue) / $yrange;
352  } else {
353  $ylocation = 0;
354  }
355  $firstchar = floor($ylocation / 64);
356  $secondchar = $ylocation % 64;
357  $mappedchar = substr(
358  $this->_extendedEncoding,
359  $firstchar,
360  1
361  ) . substr(
362  $this->_extendedEncoding,
363  $secondchar,
364  1
365  );
366  $chartdata[] = $mappedchar . $dataDelimiter;
367  } else {
368  $chartdata[] = $dataMissing . $dataDelimiter;
369  }
370  }
371  }
372  $chartdata[] = $dataSetdelimiter;
373  }
374  $buffer = implode('', $chartdata);
375 
376  $buffer = rtrim($buffer, $dataSetdelimiter);
377  $buffer = rtrim($buffer, $dataDelimiter);
378  $buffer = str_replace($dataDelimiter . $dataSetdelimiter, $dataSetdelimiter, $buffer);
379 
380  $params['chd'] .= $buffer;
381 
382  $valueBuffer = [];
383 
384  if (sizeof($this->_axisLabels) > 0) {
385  $params['chxt'] = implode(',', array_keys($this->_axisLabels));
386  $indexid = 0;
387  foreach ($this->_axisLabels as $idx => $labels) {
388  if ($idx == 'x') {
392  foreach ($this->_axisLabels[$idx] as $_index => $_label) {
393  if ($_label != '') {
394  $period = new \DateTime($_label, new \DateTimeZone($timezoneLocal));
395  switch ($this->getDataHelper()->getParam('period')) {
396  case '24h':
397  $this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
398  $period->setTime($period->format('H'), 0, 0),
399  \IntlDateFormatter::NONE,
400  \IntlDateFormatter::SHORT
401  );
402  break;
403  case '7d':
404  case '1m':
405  $this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
406  $period,
407  \IntlDateFormatter::SHORT,
408  \IntlDateFormatter::NONE
409  );
410  break;
411  case '1y':
412  case '2y':
413  $this->_axisLabels[$idx][$_index] = date('m/Y', strtotime($_label));
414  break;
415  }
416  } else {
417  $this->_axisLabels[$idx][$_index] = '';
418  }
419  }
420 
421  $tmpstring = implode('|', $this->_axisLabels[$idx]);
422 
423  $valueBuffer[] = $indexid . ":|" . $tmpstring;
424  } elseif ($idx == 'y') {
425  $valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
426  }
427  $indexid++;
428  }
429  $params['chxl'] = implode('|', $valueBuffer);
430  }
431 
432  // chart size
433  $params['chs'] = $this->getWidth() . 'x' . $this->getHeight();
434 
435  // return the encoded data
436  if ($directUrl) {
437  $p = [];
438  foreach ($params as $name => $value) {
439  $p[] = $name . '=' . urlencode($value);
440  }
441  return self::API_URL . '?' . implode('&', $p);
442  } else {
443  $gaData = urlencode(base64_encode(json_encode($params)));
444  $gaHash = $this->_dashboardData->getChartDataHash($gaData);
445  $params = ['ga' => $gaData, 'h' => $gaHash];
446  return $this->getUrl('adminhtml/*/tunnel', ['_query' => $params]);
447  }
448  }
449 
457  protected function getRowsData($attributes, $single = false)
458  {
459  $items = $this->getCollection()->getItems();
460  $options = [];
461  foreach ($items as $item) {
462  if ($single) {
463  $options[] = max(0, $item->getData($attributes));
464  } else {
465  foreach ((array)$attributes as $attr) {
466  $options[$attr][] = max(0, $item->getData($attr));
467  }
468  }
469  }
470  return $options;
471  }
472 
480  public function setAxisLabels($axis, $labels)
481  {
482  $this->_axisLabels[$axis] = $labels;
483  }
484 
491  public function setHtmlId($htmlId)
492  {
493  $this->_htmlId = $htmlId;
494  }
495 
501  public function getHtmlId()
502  {
503  return $this->_htmlId;
504  }
505 
512  protected function _getPow($number)
513  {
514  $pow = 0;
515  while ($number >= 10) {
516  $number = $number / 10;
517  $pow++;
518  }
519  return $pow;
520  }
521 
527  protected function getWidth()
528  {
529  return $this->_width;
530  }
531 
537  protected function getHeight()
538  {
539  return $this->_height;
540  }
541 
546  public function setDataHelper(\Magento\Backend\Helper\Dashboard\AbstractDashboard $dataHelper)
547  {
548  $this->_dataHelper = $dataHelper;
549  }
550 
556  protected function _prepareData()
557  {
558  if ($this->_dataHelper !== null) {
559  $availablePeriods = array_keys($this->_dashboardData->getDatePeriods());
560  $period = $this->getRequest()->getParam('period');
561  $this->getDataHelper()->setParam(
562  'period',
563  $period && in_array($period, $availablePeriods) ? $period : '24h'
564  );
565  }
566  }
567 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$attr
Definition: text.phtml:8
$number
Definition: details.phtml:22
getRowsData($attributes, $single=false)
Definition: Graph.php:457
addSeries($seriesId, array $options)
Definition: Graph.php:146
$value
Definition: gender.phtml:16
setDataHelper(\Magento\Backend\Helper\Dashboard\AbstractDashboard $dataHelper)
Definition: Graph.php:546
$attributes
Definition: matrix.phtml:13
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Reports\Model\ResourceModel\Order\CollectionFactory $collectionFactory, \Magento\Backend\Helper\Dashboard\Data $dashboardData, array $data=[])
Definition: Graph.php:108
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44
$items
if(!isset($_GET['name'])) $name
Definition: log.php:14