@@ -15,7 +15,7 @@ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
15
15
/**
16
16
* Api URL
17
17
*/
18
- const API_URL = 'http ://chart.apis.google .com/chart ' ;
18
+ const API_URL = 'https ://image-charts .com/chart ' ;
19
19
20
20
/**
21
21
* All series
@@ -76,6 +76,7 @@ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
76
76
/**
77
77
* Google chart api data encoding
78
78
*
79
+ * @deprecated since the Google Image Charts API not accessible from March 14, 2019
79
80
* @var string
80
81
*/
81
82
protected $ _encoding = 'e ' ;
@@ -111,8 +112,8 @@ public function __construct(
111
112
\Magento \Backend \Helper \Dashboard \Data $ dashboardData ,
112
113
array $ data = []
113
114
) {
114
- $ this ->_dashboardData = $ dashboardData ;
115
115
parent ::__construct ($ context , $ collectionFactory , $ data );
116
+ $ this ->_dashboardData = $ dashboardData ;
116
117
}
117
118
118
119
/**
@@ -126,9 +127,9 @@ protected function _getTabTemplate()
126
127
}
127
128
128
129
/**
129
- * Set data rows
130
+ * Set data rows.
130
131
*
131
- * @param array $rows
132
+ * @param string $rows
132
133
* @return void
133
134
*/
134
135
public function setDataRows ($ rows )
@@ -149,18 +150,18 @@ public function addSeries($seriesId, array $options)
149
150
}
150
151
151
152
/**
152
- * Get series
153
+ * Get series.
153
154
*
154
155
* @param string $seriesId
155
- * @return array|false
156
+ * @return array|bool
156
157
*/
157
158
public function getSeries ($ seriesId )
158
159
{
159
160
if (isset ($ this ->_allSeries [$ seriesId ])) {
160
161
return $ this ->_allSeries [$ seriesId ];
161
- } else {
162
- return false ;
163
162
}
163
+
164
+ return false ;
164
165
}
165
166
166
167
/**
@@ -187,11 +188,12 @@ public function getChartUrl($directUrl = true)
187
188
{
188
189
$ params = [
189
190
'cht ' => 'lc ' ,
190
- 'chf ' => 'bg,s,ffffff ' ,
191
- 'chco ' => 'ef672f ' ,
192
191
'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 ' ,
195
197
];
196
198
197
199
$ this ->_allSeries = $ this ->getRowsData ($ this ->_dataRows );
@@ -279,20 +281,11 @@ public function getChartUrl($directUrl = true)
279
281
$ this ->_axisLabels ['x ' ] = $ dates ;
280
282
$ this ->_allSeries = $ datas ;
281
283
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 = "_ " ;
296
289
297
290
// process each string in the array, and find the max length
298
291
$ localmaxvalue = [0 ];
@@ -306,67 +299,35 @@ public function getChartUrl($directUrl = true)
306
299
$ minvalue = min ($ localminvalue );
307
300
308
301
// default values
309
- $ yrange = 0 ;
310
302
$ yLabels = [];
311
303
$ miny = 0 ;
312
304
$ maxy = 0 ;
313
305
$ yorigin = 0 ;
314
306
315
307
if ($ minvalue >= 0 && $ maxvalue >= 0 ) {
316
308
if ($ maxvalue > 10 ) {
317
- $ p = pow (10 , $ this ->_getPow ($ maxvalue ));
309
+ $ p = pow (10 , $ this ->_getPow (( int ) $ maxvalue ));
318
310
$ maxy = ceil ($ maxvalue / $ p ) * $ p ;
319
311
$ yLabels = range ($ miny , $ maxy , $ p );
320
312
} else {
321
313
$ maxy = ceil ($ maxvalue + 1 );
322
314
$ yLabels = range ($ miny , $ maxy , 1 );
323
315
}
324
- $ yrange = $ maxy ;
325
316
$ yorigin = 0 ;
326
317
}
327
318
328
319
$ chartdata = [];
329
320
330
321
foreach ($ this ->getAllSeries () as $ index => $ serie ) {
331
322
$ 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 ;
370
331
}
371
332
}
372
333
$ chartdata [] = $ dataSetdelimiter ;
@@ -381,45 +342,13 @@ public function getChartUrl($directUrl = true)
381
342
382
343
$ valueBuffer = [];
383
344
384
- if (sizeof ($ this ->_axisLabels ) > 0 ) {
345
+ if (count ($ this ->_axisLabels ) > 0 ) {
385
346
$ params ['chxt ' ] = implode (', ' , array_keys ($ this ->_axisLabels ));
386
347
$ indexid = 0 ;
387
348
foreach ($ this ->_axisLabels as $ idx => $ labels ) {
388
349
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 );
421
351
$ tmpstring = implode ('| ' , $ this ->_axisLabels [$ idx ]);
422
-
423
352
$ valueBuffer [] = $ indexid . ":| " . $ tmpstring ;
424
353
} elseif ($ idx == 'y ' ) {
425
354
$ valueBuffer [] = $ indexid . ":| " . implode ('| ' , $ yLabels );
@@ -438,12 +367,52 @@ public function getChartUrl($directUrl = true)
438
367
foreach ($ params as $ name => $ value ) {
439
368
$ p [] = $ name . '= ' . urlencode ($ value );
440
369
}
370
+
441
371
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
+ }
447
416
}
448
417
}
449
418
@@ -540,6 +509,8 @@ protected function getHeight()
540
509
}
541
510
542
511
/**
512
+ * Sets data helper.
513
+ *
543
514
* @param \Magento\Backend\Helper\Dashboard\AbstractDashboard $dataHelper
544
515
* @return void
545
516
*/
0 commit comments