1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace CMEN\GoogleChartsBundle\GoogleCharts;
|
4
|
|
|
|
5
|
|
|
use CMEN\GoogleChartsBundle\Exception\GoogleChartsException;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* @author Christophe Meneses
|
9
|
|
|
*/
|
10
|
|
|
class Data
|
11
|
|
|
{
|
12
|
|
|
/**
|
13
|
|
|
* @var array
|
14
|
|
|
*/
|
15
|
|
|
private $arrayToDataTable;
|
16
|
|
|
|
17
|
|
|
/**
|
18
|
|
|
* @var boolean
|
19
|
|
|
*/
|
20
|
|
|
private $firstRowIsData;
|
21
|
|
|
|
22
|
|
|
/**
|
23
|
|
|
* @return array
|
24
|
|
|
*/
|
25
|
|
|
public function getArrayToDataTable()
|
26
|
|
|
{
|
27
|
|
|
return $this->arrayToDataTable;
|
28
|
|
|
}
|
29
|
|
|
|
30
|
|
|
/**
|
31
|
|
|
* This method takes in a two-dimensional array. The data types of each column are interpreted automatically from
|
32
|
|
|
* the data given. If a cell has no value, specify a null or empty value as appropriate. Column data types can also
|
33
|
|
|
* be specified using the object literal notation in the first row (the column header row) of the array
|
34
|
|
|
* (i.e. ['label' => 'Start Date', 'type' => 'date']). Optional data roles may be used as well, but they must be
|
35
|
|
|
* defined explicitly using object literal notation. Object literal notation may also be used for any cell,
|
36
|
|
|
* allowing you to define Cell Objects).
|
37
|
|
|
*
|
38
|
|
|
* @param array $arrayToDataTable A two-dimensional array, where each row represents a row in the data table.
|
39
|
|
|
* @param boolean $firstRowIsData If firstRowIsData is false (the default), the first row will be interpreted
|
40
|
|
|
* as header labels.
|
41
|
|
|
*/
|
42
|
2 |
|
public function setArrayToDataTable($arrayToDataTable, $firstRowIsData = false)
|
43
|
|
|
{
|
44
|
2 |
|
$this->arrayToDataTable = $arrayToDataTable;
|
45
|
2 |
|
$this->firstRowIsData = $firstRowIsData;
|
46
|
2 |
|
}
|
47
|
|
|
|
48
|
|
|
/**
|
49
|
|
|
* Returns Javascript of data.
|
50
|
|
|
*
|
51
|
|
|
* @param string $dataName Variable name who will contain the data Javascript
|
52
|
|
|
*
|
53
|
|
|
* @return string A Javascript string
|
54
|
|
|
*
|
55
|
|
|
* @throws GoogleChartsException
|
56
|
|
|
*/
|
57
|
2 |
|
public function draw($dataName)
|
58
|
|
|
{
|
59
|
2 |
|
if (!$this->arrayToDataTable) {
|
60
|
1 |
|
throw new GoogleChartsException(
|
61
|
|
|
'There is no data for chart. Use method setArrayToDataTable() to provide data.'
|
62
|
1 |
|
);
|
63
|
|
|
}
|
64
|
|
|
|
65
|
1 |
|
$js = "var $dataName = new google.visualization.arrayToDataTable([";
|
66
|
|
|
|
67
|
1 |
|
end($this->arrayToDataTable);
|
68
|
1 |
|
$lastKeyRow = key($this->arrayToDataTable);
|
69
|
1 |
|
foreach ($this->arrayToDataTable as $keyRow => $row) {
|
70
|
1 |
|
$js .= '[';
|
71
|
|
|
|
72
|
1 |
|
end($row);
|
73
|
1 |
|
$lastKeyValue = key($row);
|
74
|
1 |
|
foreach ($row as $key => $value) {
|
75
|
1 |
|
if ($value instanceof \DateTime) {
|
76
|
|
|
$js .= 'new Date(' . $value->format('Y') . ', ' . ($value->format('n') - 1) . ', ' .
|
77
|
|
|
$value->format('d') . ', ' . $value->format('H') . ', ' . $value->format('i') . ', ' .
|
78
|
|
|
$value->format('s') . ')';
|
79
|
|
|
} else {
|
80
|
1 |
|
$js .= json_encode($value);
|
81
|
|
|
}
|
82
|
|
|
|
83
|
1 |
|
if ($key != $lastKeyValue) {
|
84
|
1 |
|
$js .= ', ';
|
85
|
1 |
|
}
|
86
|
1 |
|
}
|
87
|
1 |
|
unset($value);
|
88
|
1 |
|
$js .= ']';
|
89
|
|
|
|
90
|
1 |
|
if ($keyRow != $lastKeyRow) {
|
91
|
1 |
|
$js .= ', ';
|
92
|
1 |
|
}
|
93
|
1 |
|
}
|
94
|
1 |
|
unset($row);
|
95
|
|
|
|
96
|
1 |
|
$this->firstRowIsData ? $js .= '], true);' : $js .= '], false);';
|
97
|
|
|
|
98
|
1 |
|
return $js;
|
99
|
|
|
}
|
100
|
|
|
}
|
101
|
|
|
|