-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGoogleCharts.class.asp
426 lines (338 loc) · 9.43 KB
/
GoogleCharts.class.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<%
' GoogleChart utility class 1.0 - May, 14th - 2015
'
' Licence:
' The MIT License (MIT)
' Copyright (c) 2012 RCDMK - rcdmk[at]hotmail[dot]com
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
' associated documentation files (the "Software"), to deal in the Software without restriction,
' including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
' and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
' subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial
' portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
' NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
' IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
' WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
' Constants
const CHART_BAR = "BarChart"
const CHART_COLUMN = "ColumnChart"
const CHART_LINE = "LineChart"
const CHART_PIE = "PieChart"
const CTYPE_BOOL = "boolean"
const CTYPE_DATE = "date"
const CTYPE_DATETIME = "datetime"
const CTYPE_NUMBER = "number"
const CTYPE_STRING = "string"
const CTYPE_TIME = "timeofday"
class GoogleCharts
dim i_type, i_title, i_subtitle, i_id, i_width, i_height
dim i_columns, i_data, i_scriptPath, i_loadingText
public property get [Type]()
[Type] = i_type
end property
public property let [Type](byval value)
select case value
case CHART_BAR, CHART_COLUMN, CHART_LINE, CHART_PIE
i_type = value
case else:
err.raise 1, typeName(me), "Invalid chart type. Valid types are: CHART_BAR, CHART_COLUMN, CHART_LINE, CHART_PIE"
end select
end property
public property get ID()
ID = i_id
end property
public property let ID(byval value)
i_id = value
end property
public property get Title()
Title = i_title
end property
public property let Title(byval value)
i_title = value
end property
public property get Subtitle()
Subtitle = i_subtitle
end property
public property let Subtitle(byval value)
i_subtitle = value
end property
public property get Width()
Width = i_width
end property
public property let Width(byval value)
i_width = value
end property
public property get Height()
Height = i_height
end property
public property let Height(byval value)
i_height = value
end property
public property get ScriptPath()
ScriptPath = i_scriptPath
end property
public property let ScriptPath(byval value)
i_scriptPath = value
end property
public property get LoadingText()
LoadingText = i_loadingText
end property
public property let LoadingText(byval value)
i_loadingText = value
end property
sub Class_Initialize()
i_type = CT_BAR
Randomize
i_id = int(rnd() * timer)
i_title = "Google Chart " & i_id
i_subtitle = ""
i_loadingText = "Loading..."
i_width = 400
i_height = 300
i_scriptPath = "https://www.google.com/jsapi"
redim i_columns(-1)
redim i_data(-1)
end sub
sub Class_Terminate()
redim i_data(-1,-1)
end sub
public function AddColumn(byval dataType, byval label)
dim col
set col = new GoogleChartsColumn
col.Type = dataType
col.Label = label
arrayPush i_columns, col
set AddColumn = col
end function
public sub AddRow(byval values)
dim row
if isArray(values) then
row = values
else
row = Array(values)
end if
arrayPush i_data, row
end sub
public sub ClearColumns()
dim col
for each col in i_columns
set col = nothing
next
redim i_columns(-1)
end sub
public sub ClearData()
redim i_data(-1)
end sub
public sub LoadRecordset(byref rs)
if TypeName(rs) <> "Recordset" then err.raise 3, TypeName(me), "Invalid object type. Accepted type is ADODB.Recordset"
ClearColumns()
ClearData()
dim field, row
for each field in rs.fields
AddColumn getFieldType(field.type), field.Name
next
if not rs.bof then rs.moveFirst
while not rs.eof
redim row(-1)
for each field in rs.fields
ArrayPush row, field.value
next
AddRow row
rs.movenext
wend
end sub
function getFieldType(byval fieldType)
select case fieldType
case 2, 3, 4, 5, 6, 7, 14, 16, 17, 18, 19, 20, 21, 131, 139
getFieldType = CTYPE_NUMBER
case 11
getFieldType = CTYPE_BOOL
case 64, 134, 135
getFieldType = CTYPE_TIME
case 133
getFieldType = CTYPE_DATE
case else
getFieldType = CTYPE_STRING
end select
end function
public sub LoadArray(byref arr)
if TypeName(arr) <> "Variant()" then err.raise 4, TypeName(me), "Invalid object type. Accepted type is Array() - Variant"
if arrayDimensions(arr) <> 2 then err.raise 5, TypeName(me), "Invalid Array Size. Array must be bidimensional"
ClearData()
dim r, c, row
for r = 0 to ubound(arr, 2)
redim row(-1)
for c = 0 to ubound(arr, 1)
arrayPush row, arr(c, r)
next
AddRow row
next
end sub
' Pushes (adds) a value to an array, expanding it
private function arrayPush(byref arr, byref value)
redim preserve arr(ubound(arr) + 1)
if isobject(value) then
set arr(ubound(arr)) = value
else
arr(ubound(arr)) = value
end if
ArrayPush = arr
end function
private function arrayDimensions(byref arr)
dim dimensions
dimensions = 0
on error resume next
while err.number = 0
dimensions = dimensions + 1
ubound arr, dimensions
wend
on error goto 0
arrayDimensions = dimensions - 1
end function
public sub Draw()
dim curLCID
curLCID = Session.LCID
Session.LCID = 1033
%>
<script type="text/javascript" id="GC_script_tag_<%= i_id %>">
if (typeof(google) == "undefined") {
var s = document.createElement("script");
s.src = '<%= i_scriptPath %>?callback=GC<%= i_id %>';
var body = document.getElementsByTagName('body')[0];
body.insertBefore(s, body.childNodes[0]);
} else {
GC<%= i_id %>();
}
function GC<%= i_id %>() {
if (typeof(google) == "undefined") return;
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart', 'bar'], 'callback':drawChart});
// Callback that creates and populates a data table,
// instantiates the chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
<%
dim col
for each col in i_columns
%>
data.addColumn({
type: '<%= col.Type %>',
label: '<%= escapeText(col.Label) %>',
id: '<%= col.ID %>',
role: '<%= col.Role %>',
pattern: '<%= col.Pattern %>'
});
<%
next
%>
data.addRows([
<%
dim r, row, c
for r = 0 to ubound(i_data)
row = i_data(r)
%>
[
<%
for c = 0 to ubound(row)
if c <= ubound(i_columns) then
if i_columns(c).Type = CTYPE_STRING then
response.write "'"
response.write escapeText(row(c))
response.write "'"
else
response.write row(c)
end if
end if
if c < ubound(row) then response.write ","
next
%>
]<%
if r < ubound(i_data) then response.write ","
next
%>
]);
// Set chart options
var options = {
'title':'<%= escapeText(i_title) %>',
'subtitle':'<%= escapeText(i_subtitle) %>',
'width':<%= i_width %>,
'height':<%= i_height %>
};
// Instantiate and draw our chart, passing in some options.
var element = document.getElementById('chart_div_<%= i_id %>');
var chart = new google.visualization.<%= i_type %>(element);
chart.draw(data, options);
}
};
</script>
<!--Div that will hold the chart-->
<div id="chart_div_<%= i_id %>" class="GoogleChart"><%= i_loadingText %></div>
<%
Session.LCID = curLCID
end sub
function escapeText(byval text)
dim result
result = text
result = replace(result, "'", "\'")
result = replace(result, """", "\""")
result = replace(result, vbCr, "\r")
result = replace(result, vbLf, "\n")
escapeText = result
end function
end class
class GoogleChartsColumn
dim i_type, i_label, i_role, i_id, i_pattern
public property get [Type]()
[Type] = i_type
end property
public property let [Type](byval value)
select case value
case CTYPE_BOOL, CTYPE_DATE, CTYPE_DATETIME, CTYPE_NUMBER, CTYPE_STRING, CTYPE_TIME
i_type = value
case else
err.raise 2, typeName(me), "Invalid column type. Valid types are: CTYPE_BOOL, CTYPE_DATE, CTYPE_DATETIME, CTYPE_NUMBER, CTYPE_STRING, CTYPE_TIME"
end select
end property
public property get Label
Label = i_label
end property
public property let Label(byval value)
i_label = value
end property
public property get Role()
Role = i_role
end property
public property let Role(byval value)
i_role = value
end property
public property get ID()
ID = i_id
end property
public property let ID(byval value)
i_id = value
end property
public property get Pattern()
Pattern = i_pattern
end property
public property let Pattern(byval value)
i_pattern = value
end property
sub Class_Initialize()
i_type = CTYPE_STRING
i_label = "column"
i_role = ""
i_id = ""
i_pattern = ""
end sub
sub Class_Terminate()
end sub
end class
%>