Skip to content

Commit 3a5f51f

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98833' into 2.1-develop-pr72
2 parents 20f41db + 19fc750 commit 3a5f51f

File tree

3 files changed

+93
-108
lines changed

3 files changed

+93
-108
lines changed

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

Lines changed: 77 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
1515
/**
1616
* Api URL
1717
*/
18-
const API_URL = 'http://chart.apis.google.com/chart';
18+
const API_URL = 'https://image-charts.com/chart';
1919

2020
/**
2121
* All series
@@ -76,6 +76,7 @@ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
7676
/**
7777
* Google chart api data encoding
7878
*
79+
* @deprecated since the Google Image Charts API not accessible from March 14, 2019
7980
* @var string
8081
*/
8182
protected $_encoding = 'e';
@@ -111,8 +112,8 @@ public function __construct(
111112
\Magento\Backend\Helper\Dashboard\Data $dashboardData,
112113
array $data = []
113114
) {
114-
$this->_dashboardData = $dashboardData;
115115
parent::__construct($context, $collectionFactory, $data);
116+
$this->_dashboardData = $dashboardData;
116117
}
117118

118119
/**
@@ -126,9 +127,9 @@ protected function _getTabTemplate()
126127
}
127128

128129
/**
129-
* Set data rows
130+
* Set data rows.
130131
*
131-
* @param array $rows
132+
* @param string $rows
132133
* @return void
133134
*/
134135
public function setDataRows($rows)
@@ -149,18 +150,18 @@ public function addSeries($seriesId, array $options)
149150
}
150151

151152
/**
152-
* Get series
153+
* Get series.
153154
*
154155
* @param string $seriesId
155-
* @return array|false
156+
* @return array|bool
156157
*/
157158
public function getSeries($seriesId)
158159
{
159160
if (isset($this->_allSeries[$seriesId])) {
160161
return $this->_allSeries[$seriesId];
161-
} else {
162-
return false;
163162
}
163+
164+
return false;
164165
}
165166

166167
/**
@@ -187,11 +188,12 @@ public function getChartUrl($directUrl = true)
187188
{
188189
$params = [
189190
'cht' => 'lc',
190-
'chf' => 'bg,s,ffffff',
191-
'chco' => 'ef672f',
192191
'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',
192+
'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
193+
'chm' => 'B,f4d4b2,0,0,0',
194+
'chco' => 'db4814',
195+
'chxs' => '0,0,11|1,0,11',
196+
'chma' => '15,15,15,15',
195197
];
196198

197199
$this->_allSeries = $this->getRowsData($this->_dataRows);
@@ -279,20 +281,11 @@ public function getChartUrl($directUrl = true)
279281
$this->_axisLabels['x'] = $dates;
280282
$this->_allSeries = $datas;
281283

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

297290
// process each string in the array, and find the max length
298291
$localmaxvalue = [0];
@@ -306,67 +299,35 @@ public function getChartUrl($directUrl = true)
306299
$minvalue = min($localminvalue);
307300

308301
// default values
309-
$yrange = 0;
310302
$yLabels = [];
311303
$miny = 0;
312304
$maxy = 0;
313305
$yorigin = 0;
314306

315307
if ($minvalue >= 0 && $maxvalue >= 0) {
316308
if ($maxvalue > 10) {
317-
$p = pow(10, $this->_getPow($maxvalue));
309+
$p = pow(10, $this->_getPow((int)$maxvalue));
318310
$maxy = ceil($maxvalue / $p) * $p;
319311
$yLabels = range($miny, $maxy, $p);
320312
} else {
321313
$maxy = ceil($maxvalue + 1);
322314
$yLabels = range($miny, $maxy, 1);
323315
}
324-
$yrange = $maxy;
325316
$yorigin = 0;
326317
}
327318

328319
$chartdata = [];
329320

330321
foreach ($this->getAllSeries() as $index => $serie) {
331322
$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-
}
323+
$count = count($thisdataarray);
324+
for ($j = 0; $j < $count; $j++) {
325+
$currentvalue = $thisdataarray[$j];
326+
if (is_numeric($currentvalue)) {
327+
$ylocation = $yorigin + $currentvalue;
328+
$chartdata[] = $ylocation . $dataDelimiter;
329+
} else {
330+
$chartdata[] = $dataMissing . $dataDelimiter;
370331
}
371332
}
372333
$chartdata[] = $dataSetdelimiter;
@@ -381,45 +342,13 @@ public function getChartUrl($directUrl = true)
381342

382343
$valueBuffer = [];
383344

384-
if (sizeof($this->_axisLabels) > 0) {
345+
if (count($this->_axisLabels) > 0) {
385346
$params['chxt'] = implode(',', array_keys($this->_axisLabels));
386347
$indexid = 0;
387348
foreach ($this->_axisLabels as $idx => $labels) {
388349
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-
350+
$this->formatAxisLabelDate($idx, $timezoneLocal);
421351
$tmpstring = implode('|', $this->_axisLabels[$idx]);
422-
423352
$valueBuffer[] = $indexid . ":|" . $tmpstring;
424353
} elseif ($idx == 'y') {
425354
$valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
@@ -438,12 +367,52 @@ public function getChartUrl($directUrl = true)
438367
foreach ($params as $name => $value) {
439368
$p[] = $name . '=' . urlencode($value);
440369
}
370+
441371
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]);
372+
}
373+
$gaData = urlencode(base64_encode(json_encode($params)));
374+
$gaHash = $this->_dashboardData->getChartDataHash($gaData);
375+
$params = ['ga' => $gaData, 'h' => $gaHash];
376+
377+
return $this->getUrl('adminhtml/*/tunnel', ['_query' => $params]);
378+
}
379+
380+
/**
381+
* Format dates for axis labels.
382+
*
383+
* @param string $idx
384+
* @param string $timezoneLocal
385+
* @return void
386+
*/
387+
private function formatAxisLabelDate($idx, $timezoneLocal)
388+
{
389+
foreach ($this->_axisLabels[$idx] as $_index => $_label) {
390+
if ($_label != '') {
391+
$period = new \DateTime($_label, new \DateTimeZone($timezoneLocal));
392+
switch ($this->getDataHelper()->getParam('period')) {
393+
case '24h':
394+
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
395+
$period->setTime((int)$period->format('H'), 0, 0),
396+
\IntlDateFormatter::NONE,
397+
\IntlDateFormatter::SHORT
398+
);
399+
break;
400+
case '7d':
401+
case '1m':
402+
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
403+
$period,
404+
\IntlDateFormatter::SHORT,
405+
\IntlDateFormatter::NONE
406+
);
407+
break;
408+
case '1y':
409+
case '2y':
410+
$this->_axisLabels[$idx][$_index] = date('m/Y', strtotime($_label));
411+
break;
412+
}
413+
} else {
414+
$this->_axisLabels[$idx][$_index] = '';
415+
}
447416
}
448417
}
449418

@@ -540,6 +509,8 @@ protected function getHeight()
540509
}
541510

542511
/**
512+
* Sets data helper.
513+
*
543514
* @param \Magento\Backend\Helper\Dashboard\AbstractDashboard $dataHelper
544515
* @return void
545516
*/

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class GraphTest extends \PHPUnit_Framework_TestCase
1515
*/
1616
protected $_block;
1717

18+
/**
19+
* @inheritdoc
20+
*/
1821
protected function setUp()
1922
{
2023
parent::setUp();
@@ -25,8 +28,13 @@ protected function setUp()
2528
$this->_block->setDataHelper($objectManager->get('Magento\Backend\Helper\Dashboard\Order'));
2629
}
2730

31+
/**
32+
* Tests getChartUrl.
33+
*
34+
* @return void
35+
*/
2836
public function testGetChartUrl()
2937
{
30-
$this->assertStringStartsWith('http://chart.apis.google.com/chart', $this->_block->getChartUrl());
38+
$this->assertStringStartsWith('https://image-charts.com/chart', $this->_block->getChartUrl());
3139
}
3240
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public function testAjaxBlockAction()
1919
$this->assertContains('dashboard-diagram', $actual);
2020
}
2121

22+
/**
23+
* Tests tunnelAction.
24+
*
25+
* @return void
26+
* @throws \Exception
27+
*/
2228
public function testTunnelAction()
2329
{
2430
$testUrl = \Magento\Backend\Block\Dashboard\Graph::API_URL . '?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World';
@@ -50,7 +56,7 @@ public function testTunnelAction()
5056

5157
/** @var $helper \Magento\Backend\Helper\Dashboard\Data */
5258
$helper = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
53-
'Magento\Backend\Helper\Dashboard\Data'
59+
\Magento\Backend\Helper\Dashboard\Data::class
5460
);
5561
$hash = $helper->getChartDataHash($gaFixture);
5662
$this->getRequest()->setParam('ga', $gaFixture)->setParam('h', $hash);

0 commit comments

Comments
 (0)