1
1
<?php
2
2
// Fix for UTF-8 required for local use
3
- require_once ('encoding.php ' );
3
+ require_once ('encoding.php ' );
4
4
use \ForceUTF8 \Encoding ;
5
5
6
6
function renderChart ($ target ,$ layout ,$ color ,$ title ,$ data ,$ labels ,$ categories ,$ width ,$ height ,$ charttype ) {
7
7
8
8
// Add categories if required
9
- if (!empty ($ categories ) && count ($ categories )>0 ) {
9
+ if (!empty ($ categories ) && count ($ categories )>0 ) {
10
10
$ categoriesString = "categories: " .json_encode ($ categories ).", " ;
11
11
$ categoriesStringPos = strpos ($ layout ,"{ " ,strpos ($ layout ,"xAxis " ))+1 ;
12
12
if ($ categoriesStringPos !== false )
13
13
$ layout = substr_replace ($ layout ,$ categoriesString ,$ categoriesStringPos ,0 );
14
14
}
15
-
15
+
16
16
// Replace title if required
17
- if (!empty ($ title ) && strlen ($ title )>0 ) {
17
+ if (!empty ($ title ) && strlen ($ title )>0 ) {
18
18
$ titleStringPosStart = strpos ($ layout ,": " ,strpos ($ layout ,"text " ))+1 ;
19
19
$ titleStringPosEnd = strpos ($ layout ,"} " ,strpos ($ layout ,"text " ))-1 ;
20
-
20
+
21
21
if ($ categoriesStringPos !== false )
22
22
$ layout = substr_replace ($ layout ,json_encode ($ title ),$ titleStringPosStart ,$ titleStringPosEnd -$ titleStringPosStart );
23
23
}
24
24
25
25
$ chart = "
26
- <div id=' $ target' style='width: " .$ width ."px; height: " .$ height ."px; margin: 0em'></div>
26
+ <div id=' $ target' style='width: " .$ width ."px; height: " .$ height ."px; margin: 0em'></div>
27
27
<script type='text/javascript'>
28
28
29
29
var chart = new Highcharts.Chart({
30
30
chart: {
31
31
renderTo: ' $ target' " ;
32
-
33
- if ($ charttype == RENDERER_CHARTTYPE_RADAR )
32
+
33
+ if ($ charttype == RENDERER_CHARTTYPE_RADAR )
34
34
$ chart .= ", polar: true, type: 'area' " ;
35
-
35
+
36
36
// Encoding::toUTF8() required for $data & $labels in offline use
37
37
$ chart .= "}, " .Encoding::toUTF8 ($ layout )." ,series: [
38
38
" .jsonEncodeSeries (Encoding::toUTF8 ($ data ),Encoding::toUTF8 ($ labels ),$ color ,$ charttype )."
39
39
]
40
40
});
41
41
</script> " ;
42
-
42
+
43
43
return $ chart ;
44
44
}
45
45
46
- function jsonEncodeMultiSeries ($ data ,$ labels ,$ color ,$ charttype ) {
46
+ function jsonEncodeMultiSeries ($ data ,$ labels ,$ color ,$ charttype ) {
47
47
$ countSeries = count ($ data );
48
-
48
+
49
49
for ($ i =0 ;$ i <$ countSeries ;$ i ++) {
50
-
50
+
51
51
// Reset vars per serie
52
52
$ layoutSerie = '' ;
53
53
$ colorTransparant = false ;
54
54
$ charttypeSingle = $ charttype ;
55
-
55
+
56
56
// Check if this is the last one
57
- if ($ i == ($ countSeries -1 )) {
58
-
57
+ if ($ i == ($ countSeries -1 )) {
58
+
59
59
if ($ charttype == RENDERER_CHARTTYPE_LINESTEPCOLUMN )
60
60
$ charttypeSingle = RENDERER_CHARTTYPE_LINESTEP ;
61
-
61
+
62
62
} else {
63
-
63
+
64
64
if ($ charttype == RENDERER_CHARTTYPE_LINECOLUMN || $ charttype == RENDERER_CHARTTYPE_LINESTEPCOLUMN ) {
65
65
$ colorTransparant = true ;
66
66
$ charttypeSingle = RENDERER_CHARTTYPE_COLUMN ;
67
-
68
-
67
+
68
+
69
69
$ layoutSerie .= "
70
70
showInLegend: false,
71
71
yAxis: 1,
72
72
" ;
73
73
}
74
-
75
- }
76
-
74
+
75
+ }
76
+
77
77
$ jsonData .= jsonEncodeSingleSerie ($ data [$ i ],$ labels ,$ color ,$ charttypeSingle ,$ layoutSerie ,$ colorTransparant );
78
-
78
+
79
79
if ($ i <($ countSeries -1 ))
80
80
$ jsonData .= ", " ;
81
-
81
+
82
82
}
83
-
83
+
84
84
return $ jsonData ;
85
85
}
86
86
87
87
88
88
89
- function jsonEncodeSingleSerie ($ data ,$ labels ,$ color ,$ charttype ,$ layoutSerie ,$ colorTransparant =false ) {
89
+ function jsonEncodeSingleSerie ($ data ,$ labels ,$ color ,$ charttype ,$ layoutSerie= '' ,$ colorTransparant =false ) {
90
90
global $ renderer_color ;
91
91
global $ renderer_color_transparancy ;
92
-
92
+
93
93
$ countSeries = count ($ data );
94
94
$ jsonData = "" ;
95
-
95
+
96
96
if ($ countSeries >0 ) {
97
- for ($ i =0 ;$ i <$ countSeries ;$ i ++) {
98
-
97
+ for ($ i =0 ;$ i <$ countSeries ;$ i ++) {
98
+
99
99
$ colorSerie = $ renderer_color [$ color ][$ i ][0 ];
100
100
if ($ colorTransparant )
101
101
$ colorSerie = "rgba( " .hex2rgb ($ colorSerie ).", " .$ renderer_color_transparancy .") " ;
102
-
102
+
103
103
$ jsonData .= " { " ;
104
-
104
+
105
105
if ($ charttype == RENDERER_CHARTTYPE_COLUMN )
106
- $ jsonData .= " type: 'column', " ;
107
-
106
+ $ jsonData .= " type: 'column', " ;
107
+
108
108
if ($ charttype == RENDERER_CHARTTYPE_BAR )
109
- $ jsonData .= " type: 'bar', " ;
110
-
109
+ $ jsonData .= " type: 'bar', " ;
110
+
111
111
if ($ charttype == RENDERER_CHARTTYPE_LINESTEP )
112
- $ jsonData .= " step: 'left', " ;
113
-
112
+ $ jsonData .= " step: 'left', " ;
113
+
114
114
$ jsonData .= $ layoutSerie ;
115
-
115
+
116
116
$ jsonData .= "
117
117
color: ' " .$ colorSerie ."',
118
118
name: " .json_encode ($ labels [$ i ]).",
119
119
data: " .json_encode ($ data [$ i ])." } " ;
120
-
120
+
121
121
if ($ i <($ countSeries -1 ))
122
122
$ jsonData .= ", " ;
123
-
123
+
124
124
}
125
-
125
+
126
126
} else if (countSeries==1 ) {
127
127
$ jsonData .= "data: { " .$ layoutSerie .json_encode ($ data [0 ])." } " ;
128
- }
129
-
128
+ }
129
+
130
130
return $ jsonData ;
131
131
}
132
132
@@ -135,13 +135,13 @@ function jsonEncodeRadarSerie($data,$labels,$color,$charttype) {
135
135
}
136
136
137
137
138
- function jsonEncodeSeries ($ data ,$ labels ,$ color ,$ charttype ) {
139
-
138
+ function jsonEncodeSeries ($ data ,$ labels ,$ color ,$ charttype ) {
139
+
140
140
if ($ charttype == RENDERER_CHARTTYPE_LINECOLUMN || $ charttype == RENDERER_CHARTTYPE_LINESTEPCOLUMN )
141
- return jsonEncodeMultiSeries ($ data ,$ labels ,$ color ,$ charttype );
142
-
141
+ return jsonEncodeMultiSeries ($ data ,$ labels ,$ color ,$ charttype );
142
+
143
143
else
144
- return jsonEncodeSingleSerie ($ data ,$ labels ,$ color ,$ charttype );
144
+ return jsonEncodeSingleSerie ($ data ,$ labels ,$ color ,$ charttype );
145
145
}
146
146
147
147
@@ -192,4 +192,4 @@ function hex2rgb($hex) {
192
192
193
193
}
194
194
195
- ?>
195
+ ?>
0 commit comments