-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcharts.js
596 lines (525 loc) · 17.2 KB
/
gcharts.js
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
Copyright (c) <2008> <Nate Agrin: [email protected]>
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.
*/
/*
gchartjs is a Javascript dynamo for the Google Charts API.
It's inheritance model is based on Douglas Crockford's notion of functional
inheritance. I've modified that model a bit to add some convience functions
such as 'inherits' which allows for module-like mixin behavior. To create
new objects in gchartjs, one should never have to call 'new'. For example:
// THIS WON'T WORK!!!!
var lc = new gchart.line_chart({height: 20, width: 100});
is unessecary. Instead the function 'gchart.line_chart()' returns an object
which can be used to create a line chart or to extend further. Hence you
should be writing the above line like so:
var lc = gchart.line_chart({height: 20, width: 100});
@author: Nate Agrin
@date: 5-24-08
@version: 0.5
@license: MIT
@usage:
var lc = gchart.line_chart({height: 20, width: 20});
lc.add_data({
data: [1,2,3,4,5],
color: '000000',
label: 'foo'
});
lc.to_url();
var img = lc.to_img();
GCHARTJS CURRENTLY SUPPORTS A LIMITED SUBSET OF THE GOOGLE CHART API.
AVAILABLE CHART TYPES ARE:
Bar charts
Line charts
Accessories:
Fills
Line / Bar colors
Legends
*/
(function(){
this.gchart = {};
gchart.base = function (specs) {
specs = specs || {};
// pseudo constants
var VERSION = '0.5';
var BASE_URL = 'http://chart.apis.google.com/chart?';
var CHART_TYPES = ['lc', // line chart
'lxy', // line chart xy
'ls', // sparkline
'bhs', // bar chart horizontal (single)
'bvs', // bar chart vertical (single)
'bhg', // bar chart horizontal (grouped)
'bvg', // bar chart vertical (grouped)
'p', // pie chart (2d)
'p3', // pie chart (3d)
'v', // Venn diagram
's', // scatter plots
'r', // radar chart (straight lines)
'rs', // radar chart (splines)
't', // map
'gom']; // Google-o-meter!
// private attributes
var data_sets = [];
// 'that' is the object that gchart.base eventually returns
var that = {
height: 0,
width: 0,
type: '',
title: '',
specs: specs
};
// handle the options passed in
that.type = specs['type'] || 'base';
that.width = specs['width'] || 200;
that.height = specs['height'] || 100;
var get_version = function () {
return VERSION;
}
that.get_version = get_version;
// This is a simplified way for including properties and methods from
// modules which also allows for the calling of a super-class' overwritten
// method. It was largely adapted from the method developed by
// John Resig here: http://ejohn.org/blog/simple-javascript-inheritance/
var includes = function (provider) {
for (var attribute in provider) {
if (this.hasOwnProperty(attribute) &&
typeof provider[attribute] === 'function' &&
typeof this[attribute] === 'function') {
var _super = this[attribute];
this[attribute] = function () {
this._super = _super;
var ret = provider[attribute].apply(this, arguments);
delete this._super;
return ret;
};
}
else {
if (typeof provider[attribute] === 'object') {
if (provider[attribute].constructor === Array) {
this[attribute] = provider[attribute].concat();
}
else if (provider[attribute].constructor === Object){
var tmp = {};
for (var prop in provider[attribute]) {
tmp[prop] = provider[attribute][prop];
}
this[attribute] = tmp;
}
}
else {
this[attribute] = provider[attribute];
}
}
}
return this;
};
that.includes = includes;
// data handling methods
var add_data_set = function (data) {
data_sets.push(data);
return this;
};
that.add_data_set = add_data_set;
var get_data_sets = function () {
return data_sets;
};
that.get_data_sets = get_data_sets;
var encode_data = function (encoding) {
if (this.get_data_sets().length == 0) {
throw "You must provide data for the chart.";
}
switch(encoding) {
case('simple'):
case('s'):
return simple_encoding();
break;
case('enhanced'):
case('e'):
return enhanced_encoding();
break;
default:
return no_encoding();
break;
}
};
that.encode_data = encode_data;
var no_encoding = function() {
var flattened_array = [];
var data_sets = get_data_sets();
for (var i=0; i < data_sets.length; i += 1) {
flattened_array.push(data_sets[i].data.join(','));
}
return 'chd=t:' + flattened_array.join('|');
};
// Straight from http://code.google.com/apis/chart/#simple with some
// modifications. This function currently scales the datasets to the largest
// value in the cumulative datasets, which may or may not be a desired
// funciton of the encoding.
var simple_encoding = function () {
var simple_encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var chart_data = [];
var cumulative_data = [];
var max_value = 0;
for (var i=0; i < data_sets.length; i += 1) {
cumulative_data = cumulative_data.concat(data_sets[i].data);
}
max_value = cumulative_data.sort(function(a,b) {return (a - b);}).pop();
for (var i=0; i < data_sets.length; i += 1) {
var data_set = [];
for (var j = 0; j < data_sets[i].data.length; j++) {
var current_value = data_sets[i].data[j];
if (!isNaN(current_value) && current_value >= 0) {
data_set.push(
simple_encoding.charAt(
Math.round(
(simple_encoding.length-1) * current_value / max_value)));
}
else {
data_set.push('_');
}
}
chart_data.push(data_set.join(''));
}
return 'chd=s:' + chart_data.join(',');
};
var enhanced_encoding = function () {
throw "Not yet implemented.";
};
var get_url_parts = function (encoding) {
encoding = encoding || null;
var uri = [];
uri.push(this.encode_data(encoding));
uri.push(this.get_type());
uri.push(this.get_size());
if (this.has_title()) uri.push('chtt='+this.title.replace(' ', '+'));
return uri;
};
that.get_url_parts = get_url_parts;
var to_url = function (encoding, safe) {
safe = safe || false;
var parts = this.get_url_parts(encoding);
return safe ? BASE_URL + parts.join('&') : BASE_URL + parts.join('&');
};
that.to_url = to_url;
var to_img = function () {
var img = document.createElement('img');
img.src = this.to_url();
if (this.has_title()) {
img.title = img.alt = this.title;
}
else {
img.title = img.alt = "Google chart";
}
return img;
};
that.to_img = to_img;
// Handle the size
var check_size = function () {
if (this.height * this.width > 300000 ||
this.height * this.width <= 0) {
throw "Chart cannot be larger than 300,000 pixels squared or less " +
"than or equal to 0 pixels squared.";
}
return true;
};
that.check_size = check_size;
var get_size = function () {
if (this.check_size()) return 'chs=' + this.width + 'x' + this.height;
};
that.get_size = get_size;
// Handle the type
var check_type = function () {
if (this.type === '' || typeof this.type === 'undefined') {
throw "You must specify a supported chart type.";
}
for (var i=0; i < CHART_TYPES.length; i += 1) {
if (CHART_TYPES[i] === this.type) return true;
}
throw "Chart type '" + this.type + "' is not currently supported.";
};
that.check_type = check_type;
var get_type = function() {
if (this.check_type()) return 'cht=' + this.type;
};
that.get_type = get_type;
// title handling
var has_title = function () {
return (typeof this.title !== 'undefined' && this.title !== '');
};
that.has_title = has_title;
return that;
};
// Mixins are object literals whose properties get attached to a chart object
// through the use of the that.includes() method. When a mixin defines a
// method already available in the parent object, includes allows access to
// the parent's method via a call to this._super() within the overwritten
// method's functional body.
gchart.mixins = {};
gchart.mixins.color = {
// data can really only have line coloring and the labeling
get_colors: function() {
var chco = [];
var data_sets = this.get_data_sets();
var has_data = false;
var has_array = false;
// first go through and handle the base cases
for (var i=0; i < data_sets.length; i += 1) {
if (typeof data_sets[i]['color'] !== 'undefined') {
has_data = true;
if (data_sets[i]['color'].constructor === Array) has_array = true;
chco.push(this.get_data_sets()[i]['color']);
}
// add in a blank space in case other data sets have colors
else {
chco.push('');
}
}
// handle the colors as 2d arrays and single strings
if (has_data && has_array) {
return 'chco=' + chco.join('|');
}
else if (has_data){
return 'chco=' + chco.join(',');
}
return [];
},
get_url_parts: function(enc) {
return this._super(enc).concat(this.get_colors());
}
};
gchart.mixins.legend = {
get_legend: function () {
var chdl = [];
var data_sets = this.get_data_sets();
var has_data = false;
for (var i=0; i < data_sets.length; i += 1) {
if (typeof data_sets[i]['label'] !== 'undefined') {
has_data = true;
chdl.push(data_sets[i]['label']);
}
// add an empty string as a spacer incase a value has been omitted
else {
chdl.push('');
}
}
return has_data ? 'chdl=' + chdl.join('|') : [];
},
get_url_parts: function(enc) {
return this._super(enc).concat(this.get_legend());
}
};
gchart.mixins.fill = {
fills: [],
// Fills have types: solid, gradient or stripes; elements they can be
// applied to: chart, background, transparency and a color in hex form
// with an optional opacity set.
add_fill: function(fill) {
if (typeof fill.type === 'undefined' ||
typeof fill.element === 'undefined' ||
typeof fill.color === 'undefined') {
throw "You must define a fill type, element and color for a fill.";
}
this.fills.push(fill);
},
// handle the fill
// there are 4 types:
// fill area chm=[b|B,hex,start,end]
// solid fill chf=[bg|c|a,s,hex]
// linear fill chf=[bg|c,lg,angle,color1,offset1,colorN,offsetN]
// linear stripes chf=[bg|c,ls,angle,color1,width1...N]
get_fills: function() {
var fills = [];
if (this.fills.length == 0) return fills;
for (var i=0; i < this.fills.length; i++) {
var fill = [];
fill.push(this._get_fill_element(this.fills[i]));
switch(this.fills[i].type) {
case('solid'):
case('s'):
fill.push('s');
fill.push(this.fills[i].color);
if (this.fills[i].opacity !== undefined) {
fill[2] += this.fills[i].opacity;
}
break;
case('gradient'):
case('lg'):
case('linear gradient'):
fill.push('lg');
fill.push(this.fills[i].angle);
// is it an array or a single color?
if (this.fills[i].color && this.fills[i].color.constructor == Array) {
for(var j=0; j < this.fills[i].color.length; j++) {
fill.push(this.fills[i].color[j]);
fill.push(this.fills[i].offset[j]);
}
}
else {
fill.push(this.fills[i].color);
fill.push(this.fills[i].offset);
}
break;
case('stripes'):
case('ls'):
case('linear stripes'):
fill.push('ls');
fill.push(this.fills[i].angle);
// is it an array or a single color?
if (this.fills[i].color && this.fills[i].color.constructor == Array) {
for(var j=0; j < this.fills[i].color.length; j++) {
fill.push(this.fills[i].color[j]);
if (this.fills[i].width && this.fills[i].width.constructor == Array) {
fill.push(this.fills[i].width[j]);
}
else {
fill.push(this.fills[i].width);
}
}
}
else {
fill.push(this.fills[i].color);
fill.push(this.fills[i].width);
}
break;
}
fills.push(fill.join(','));
}
return 'chf=' + fills.join('|');
},
_get_fill_element: function(fill) {
switch(fill.element) {
case('bg'): case('background'):
return 'bg';
break;
case('c'): case('chart'):
return 'c';
break;
case('a'): case('transparent'): case('transparency'):
if (fill.type != 'solid' && fill.type != 's') {
throw "Transparent fills cannot be used with gradient or stripe fills.";
}
return 'a';
break;
default:
throw "The solid fill type '" + fill.type +
"' was not recognized; try 'bg', 'c' or 'a'.";
break;
}
},
get_url_parts: function(enc) {
return this._super(enc).concat(this.get_fills());
}
};
gchart.mixins.axis = {
axes: [],
add_axis: function(axis) {
axis['labels'] = axis['labels'] || [];
axis['positions'] = axis['positions'] || [];
axis['range'] = axis['range'] || [];
axis['style'] = axis['style'] || [];
this.axes.push(axis);
},
get_axes: function() {
var chxt = []; // type
var chxl = []; // label
var chxp = []; // position
var chxr = []; // range
var chxs = []; // style
for(var i=0; i < this.axes.length; i++) {
chxt.push(this.axes[i].type);
if (this.axes[i].labels.length > 0) {
chxl.push(i + ':|' + this.axes[i].labels.join('|'));
}
if (this.axes[i].positions.length > 0) {
chxp.push(i + ',' + this.axes[i].positions.join(','));
}
if (this.axes[i].range.length > 0) {
chxr.push(i + ',' + this.axes[i].range.join(','));
}
if (this.axes[i].style.length > 0) {
chxs.push(i + ',' + this.axes[i].style.join(','));
}
}
var url_parts = [];
if (chxt.length > 0) {
url_parts.push('chxt=' + chxt.join(','));
}
if (chxl.length > 0) {
url_parts.push('chxl=' + chxl.join('|'));
}
if (chxp.length > 0) {
url_parts.push('chxp=' + chxp.join('|'));
}
if (chxr.length > 0) {
url_parts.push('chxr=' + chxr.join('|'));
}
if (chxs.length > 0) {
url_parts.push('chxs=' + chxs.join('|'));
}
return url_parts;
},
get_url_parts: function(enc) {
return this._super(enc).concat(this.get_axes());
}
};
// Bar charts
gchart.bar_chart = function (specs) {
var that = gchart.base(specs);
that.includes(gchart.mixins.color);
that.includes(gchart.mixins.legend);
that.includes(gchart.mixins.fill);
that.includes(gchart.mixins.axis);
that.type = 'bhs';
return that;
};
gchart.vertical_bar_chart = function(specs) {
var that = gchart.bar_chart(specs);
that.type = 'bvs'; // default is vertical stacked bar chart
if (typeof that.specs['grouped'] !== 'undefined' && that.specs['grouped']) {
that.type = 'bvg';
}
return that;
};
gchart.horizontal_bar_chart = function(specs) {
var that = gchart.bar_chart(specs);
that.type = 'bhs'; // default is horizontal stacked bar chart
if (typeof that.specs['grouped'] !== 'undefined' && that.specs['grouped']) {
that.type = 'bhg';
}
return that;
};
// Line Charts
gchart.line_chart = function (specs) {
var that = gchart.base(specs);
that.includes(gchart.mixins.color);
that.includes(gchart.mixins.legend);
that.includes(gchart.mixins.fill);
that.includes(gchart.mixins.axis);
that.type = 'lc';
return that;
};
gchart.line_chart_xy = function (specs) {
var that = gchart.line_chart(specs);
that.type = 'lxy';
return that;
};
gchart.sparkline = function (specs) {
var that = gchart.line_chart(specs);
that.type = 'ls';
return that;
};
})();