@@ -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 */
0 commit comments