Skip to content

Commit c390f29

Browse files
committed
Merge remote-tracking branch 'github-magento2ce/MAGETWO-98584' into EPAM-PR-51
2 parents c21a64e + 07a1570 commit c390f29

File tree

3 files changed

+79
-96
lines changed

3 files changed

+79
-96
lines changed

app/code/Magento/Backend/Block/Dashboard/Graph.php

Lines changed: 66 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Backend\Block\Dashboard;
79

810
/**
@@ -15,7 +17,7 @@ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
1517
/**
1618
* Api URL
1719
*/
18-
const API_URL = 'http://chart.apis.google.com/chart';
20+
const API_URL = 'https://image-charts.com/chart';
1921

2022
/**
2123
* All series
@@ -76,6 +78,7 @@ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
7678
/**
7779
* Google chart api data encoding
7880
*
81+
* @deprecated since the Google Image Charts API not accessible from March 14, 2019
7982
* @var string
8083
*/
8184
protected $_encoding = 'e';
@@ -187,11 +190,12 @@ public function getChartUrl($directUrl = true)
187190
{
188191
$params = [
189192
'cht' => 'lc',
190-
'chf' => 'bg,s,ffffff',
191-
'chco' => 'ef672f',
192193
'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',
194+
'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
195+
'chm' => 'B,f4d4b2,0,0,0',
196+
'chco' => 'db4814',
197+
'chxs' => '0,0,11|1,0,11',
198+
'chma' => '15,15,15,15'
195199
];
196200

197201
$this->_allSeries = $this->getRowsData($this->_dataRows);
@@ -279,20 +283,11 @@ public function getChartUrl($directUrl = true)
279283
$this->_axisLabels['x'] = $dates;
280284
$this->_allSeries = $datas;
281285

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-
}
286+
// Image-Charts Awesome data format values
287+
$params['chd'] = "a:";
288+
$dataDelimiter = ",";
289+
$dataSetdelimiter = "|";
290+
$dataMissing = "_";
296291

297292
// process each string in the array, and find the max length
298293
$localmaxvalue = [0];
@@ -306,7 +301,6 @@ public function getChartUrl($directUrl = true)
306301
$minvalue = min($localminvalue);
307302

308303
// default values
309-
$yrange = 0;
310304
$yLabels = [];
311305
$miny = 0;
312306
$maxy = 0;
@@ -321,52 +315,21 @@ public function getChartUrl($directUrl = true)
321315
$maxy = ceil($maxvalue + 1);
322316
$yLabels = range($miny, $maxy, 1);
323317
}
324-
$yrange = $maxy;
325318
$yorigin = 0;
326319
}
327320

328321
$chartdata = [];
329322

330323
foreach ($this->getAllSeries() as $index => $serie) {
331324
$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-
}
325+
$count = count($thisdataarray);
326+
for ($j = 0; $j < $count; $j++) {
327+
$currentvalue = $thisdataarray[$j];
328+
if (is_numeric($currentvalue)) {
329+
$ylocation = $yorigin + $currentvalue;
330+
$chartdata[] = $ylocation . $dataDelimiter;
331+
} else {
332+
$chartdata[] = $dataMissing . $dataDelimiter;
370333
}
371334
}
372335
$chartdata[] = $dataSetdelimiter;
@@ -381,45 +344,13 @@ public function getChartUrl($directUrl = true)
381344

382345
$valueBuffer = [];
383346

384-
if (sizeof($this->_axisLabels) > 0) {
347+
if (count($this->_axisLabels) > 0) {
385348
$params['chxt'] = implode(',', array_keys($this->_axisLabels));
386349
$indexid = 0;
387350
foreach ($this->_axisLabels as $idx => $labels) {
388351
if ($idx == 'x') {
389-
/**
390-
* Format date
391-
*/
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-
352+
$this->formatAxisLabelDate($idx, $timezoneLocal);
421353
$tmpstring = implode('|', $this->_axisLabels[$idx]);
422-
423354
$valueBuffer[] = $indexid . ":|" . $tmpstring;
424355
} elseif ($idx == 'y') {
425356
$valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
@@ -447,6 +378,46 @@ public function getChartUrl($directUrl = true)
447378
}
448379
}
449380

381+
/**
382+
* Format dates for axis labels
383+
*
384+
* @param string $idx
385+
* @param string $timezoneLocal
386+
*
387+
* @return void
388+
*/
389+
private function formatAxisLabelDate($idx, $timezoneLocal)
390+
{
391+
foreach ($this->_axisLabels[$idx] as $_index => $_label) {
392+
if ($_label != '') {
393+
$period = new \DateTime($_label, new \DateTimeZone($timezoneLocal));
394+
switch ($this->getDataHelper()->getParam('period')) {
395+
case '24h':
396+
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
397+
$period->setTime($period->format('H'), 0, 0),
398+
\IntlDateFormatter::NONE,
399+
\IntlDateFormatter::SHORT
400+
);
401+
break;
402+
case '7d':
403+
case '1m':
404+
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
405+
$period,
406+
\IntlDateFormatter::SHORT,
407+
\IntlDateFormatter::NONE
408+
);
409+
break;
410+
case '1y':
411+
case '2y':
412+
$this->_axisLabels[$idx][$_index] = date('m/Y', strtotime($_label));
413+
break;
414+
}
415+
} else {
416+
$this->_axisLabels[$idx][$_index] = '';
417+
}
418+
}
419+
}
420+
450421
/**
451422
* Get rows data
452423
*
@@ -540,6 +511,8 @@ protected function getHeight()
540511
}
541512

542513
/**
514+
* Sets data helper
515+
*
543516
* @param \Magento\Backend\Helper\Dashboard\AbstractDashboard $dataHelper
544517
* @return void
545518
*/

dev/tests/integration/testsuite/Magento/Backend/Block/Dashboard/GraphTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Backend\Block\Dashboard;
79

810
/**
@@ -27,6 +29,6 @@ protected function setUp()
2729

2830
public function testGetChartUrl()
2931
{
30-
$this->assertStringStartsWith('http://chart.apis.google.com/chart', $this->_block->getChartUrl());
32+
$this->assertStringStartsWith('https://image-charts.com/chart', $this->_block->getChartUrl());
3133
}
3234
}

dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/DashboardTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Backend\Controller\Adminhtml;
79

810
/**
@@ -19,10 +21,15 @@ public function testAjaxBlockAction()
1921
$this->assertContains('dashboard-diagram', $actual);
2022
}
2123

24+
/**
25+
* Tests tunnelAction
26+
*
27+
* @throws \Exception
28+
* @return void
29+
*/
2230
public function testTunnelAction()
2331
{
24-
$this->markTestSkipped('MAGETWO-98800: TunnelAction fails when Google Chart API is not available');
25-
32+
// phpcs:disable Magento2.Functions.DiscouragedFunction
2633
$testUrl = \Magento\Backend\Block\Dashboard\Graph::API_URL . '?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World';
2734
$handle = curl_init();
2835
curl_setopt($handle, CURLOPT_URL, $testUrl);
@@ -36,6 +43,7 @@ public function testTunnelAction()
3643
curl_close($handle);
3744
throw $e;
3845
}
46+
// phpcs:enable
3947

4048
$gaData = [
4149
'cht' => 'lc',

0 commit comments

Comments
 (0)