-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharterize.js
1365 lines (1198 loc) · 41.8 KB
/
charterize.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Chart module
*
* @package Ongage\Charterize
* @category **CATEGORY**
* @author Rafi Bodill <[email protected]>
* @copyright Copyright (C) 2010 - 2012 Ongage, LTD. All rights reserved.
* @license Ongage, 2010-2012
* @link http://www.ongage.com
*/
(function ($) {
// Default chart plugin configuration
var default_settings = {
provider: 'Highcharts',
height: 398,
extension: '',
bind: '',
query: {},
url: '',
type: 'column',
types: [ 'pie', 'column', 'bar', 'line', 'area' ],
/*margin: {
pie: [ 40, 100, 40, 100 ],
column: [ 40, 50, 150, 70 ],
bar: [ 40, 50, 80, 80 ],
line: [ 40, 50, 150, 70 ]
},*/
footer: { text: 'Powered by Charterize', href: false },
unit: '',
unitleft: '',
date_format: '',
aggregate_values: false,
// show_date_range: true/false, can also be function that receives response and return subtitle
show_date_range: true,
// Define decimals (How many digits to show after decimal point)
decimals: 0,
// Add average values to future dates
future_average: false,
// Formatters
xaxis: {
formatter: null // function (label, record)
},
// Series config
series: {
labels: {
decorator: 'autohide'
},
data: [],
line: { data: [] },
area: { data: [] },
column: { data: [] },
bar: { data: [] },
pie: { data: [], grouping: true },
funnel: { data: [] }
},
// Provider options
options: {},
// i18n
language: 'en',
lang: {
en: {
area_title: 'Area Chart',
line_title: 'Line Chart',
column_title: 'Column Chart',
bar_title: 'Bar Chart',
pie_title: 'Pie Chart',
funnel_title: 'Funnel Chart',
print_title: 'Print Chart',
export_title: 'Export Chart'
}
}
};
// ------------------------------------------------------- METHODS ---------------------------------------------------------
var methods = {
/**
* Init widget
*
* @param {Object} options User configuration
*/
init: function (options) {
return this.each(function () {
var $this = $(this),
data = $this.data('chart'),
usr_config = $.extend(true, {}, options),
settings;
// Merge extension settings with user settings
if (options.extension) {
$.extend(true, usr_config, $.fn.chart.extensions[options.extension]);
}
// Merge default settings
settings = $.extend(false, {}, default_settings, usr_config);
settings.series = $.extend(true, {}, default_settings.series, settings.series);
settings.xaxis = $.extend(true, {}, default_settings.xaxis, settings.xaxis);
settings.lang = $.extend(true, {}, default_settings.lang, settings.lang);
// If the plugin hasn't been initialized yet
if ( ! data) {
$this.addClass('charterize');
// Store target and settings within element data collection
$this.data('chart', {
target: $this,
settings: settings,
chart: null,
controls: null,
response: null
});
methods.setup.apply(this, [ settings ]);
if ( ! settings.model) {
// Call refresh
methods.refresh.call(this);
}
} else {
data.settings = settings;
$this.data('chart', data);
}
});
},
/**
* Setup extra elements and bindings
*
* @param {Object} settings
*/
setup: function (settings) {
var $this = $(this),
data = $this.data('chart'),
lang = settings.lang[settings.language],
title = '',
$header = $('<div />').addClass('chart-controls').append($('<div />').addClass('date-range')),
$export_print_group = $('<div />').addClass('btn-group');
// Binding custom events
$.each(['create', 'beforerefresh', 'refresh', 'beforeload', 'load' ], function (i, event_type) {
$this.bind(event_type+'.chart', function (event) {
var func = $(this).data('chart').settings[event.type];
if ($.isFunction(func)) {
func.apply(this, arguments);
}
});
});
if (settings.model) {
settings.model.onDataChanged.subscribe(function () {
log('dataView.onDataChanged: chart');
// Process response
var response = { payload: settings.model.getItems() },
query = settings.model.getRequestQuery(),
schema = methods.post_process.apply($this[0], [ response, query ]);
methods.update.apply($this[0], [ schema ]);
});
settings.model.onEmptyResult.subscribe(function () {
// log('dataView.onEmptyResult: chart');
$this.empty().html($('<div />').addClass('chart-empty').text('No results found for your criteria'));
});
// settings.model.onRowsChanged.subscribe(function (e, args) {
// log('dataView.onRowsChanged: chart');
// });
}
// Create export button
$export_print_group.append(
$('<span />').append($('<a />', { href: '#', text: '', title: lang['print_title']})
.addClass('info icon print')
.bind('click', function() {
methods._print.apply($this[0]);
return false;
})
));
// Create export button
$export_print_group.append(
$('<span />').append($('<a />', { href: '#', text: '', title: lang['export_title']})
.addClass('info icon export')
.bind('click', function() {
methods._export.apply($this[0]);
return false;
})
));
// Append export & print buttons
$header.append($export_print_group);
// Create 'type controls' element
if (settings.types.length > 1) {
var $type_group = $('<div />').addClass('btn-group');
// Append buttons
for (var i = 0, type_count = settings.types.length; i < type_count; i++) {
title = lang[settings.types[i]+'_title'];
$type_group.append(
$('<span />').append($('<a />', { href: '#', text: '', title: title, rel: settings.types[i] })
.addClass('info icon chart_'+settings.types[i]+(settings.types[i] == settings.type ? ' selected' : ''))
.bind('click.chart', function () {
var $icon = $(this);
// Add "selected" class only to this icon
$header.find('.icon').removeClass('selected');
$icon.addClass('selected');
methods._switch_type.apply($this[0], [ $icon.attr('rel') ]);
return false;
})
));
}
// Add vertical separator
$header.append($('<div />').addClass('vertical-separator'));
// Append chart type buttons
$header.append($type_group);
}
// Add header to screen
$this.before($header);
// Bind filter form
if (settings.bind) {
$('#'+settings.bind).on('submit', function () {
methods.refresh.call($this[0]);
});
}
$this.trigger('create');
},
/**
* Destroy chart widget
*/
destroy: function () {
return this.each(function () {
var $this = $(this),
data = $this.data('chart');
// Un-binding events by namespace
$this.unbind('.chart');
if (data) {
// Destroying chart
// TODO.rafi: I'm taking as granted: Highcharts.destroy()
data.chart && data.chart.destroy();
data.chart = null;
// Removing controls
data.controls && data.controls.remove();
data.controls = null;
// Emptying target element
$this.empty();
// Emptying data collections
data.response = null;
data.target = null;
}
// Removing data collections
$this.removeData('chart');
});
},
/**
* Export chart to JPEG
*/
_export: function () {
var $this = $(this),
data = $this.data('chart'),
$header = $this.siblings('h2:first');
data.chart.exportChart({
type: 'image/jpeg',
filename: $header.text() ? $header.text() : 'chart'
});
},
/**
* Print Chart
*/
_print: function () {
$(this).data('chart').chart.print();
},
/**
* Switch chart type
*
* @param {String} type Chart type
*/
_switch_type: function (type) {
var $this = $(this),
data = $this.data('chart'),
settings = data.settings,
height = $this.height(),
schema;
// Flash a red background if type is invalid
if ($.inArray(type, settings.types) < 0) {
$this.parent().trigger('focus').effect('highlight', { color: '#F55252' }, 1000);
return false;
}
data.settings.type = type;
$this.data('chart', data);
if(data.response) {
$this.empty().addClass('loading').css('height', height+'px');
// Rebuild schema (Skip AJAX call)
schema = methods.post_process.apply(this, [ data.response ]);
methods.update.apply(this, [ schema ]);
}
},
/**
* Parses bind form inputs to query
*
* @param {Object} settings Object with { bind: '', query: {} }
*/
_parse_query_binds: function (settings) {
var bind_regex = /^bind\((.*)\)$/,
query = $.extend(true, {}, settings.query),
$form, filter, group, matches, selector, $inp, val, date, utc_day_timestamp;
// Bind filter form
if ( ! settings.bind) return query;
$form = $('#'+settings.bind);
// Empty filters
query.filter = {};
for (var i = 0, filter_count = settings.query.filter.length; i < filter_count; i++) {
filter = settings.query.filter[i];
if (bind_regex.test(filter[2])) {
matches = bind_regex.exec(filter[2]);
if (matches.length == 2) {
selector = '[name="'+matches[1]+'"]';
$inp = $form.find('input'+selector+', select'+selector).eq(0);
val = $inp.val();
// TODO.rafi: generify
// If field has been on dates, change value to unix timestamp
/*if (val && (matches[1] == 'to_date' || matches[1] == 'from_date')) {
// Convert datepicker date to Date object
val = $.datepicker.parseDate('dd-mm-yy', val);
// Convert to unix timestamp, adjusting to UTC timezone
//utc_day_timestamp = (date.getTimezoneOffset() * -60) + (date.getTime() / 1000);
/**
* Reducing user's timezone offset to get the final utc timestamp
* Example:
* - User timezone: GMT + 3
* - User filtered by day: 10/05/2013
* - Final timestamp sent as filter should be for 09/05/2013 21:00 in UTC (GMT+0)
*
* Note: when using from-to dates, the timestamp sent is the same, in server side the "to-date" should
* be for 1 day from the from-date - second (adding to the value 1 day in seconds -1 second)
* Then the filter will be: 09/05/2013 21:00 - 10/05/2013 21:00 in UTC and will fetch the correct records.
*//*
/*val = utc_day_timestamp - App.user_timezone_offset;
// For date_to filters - Set timestamp to the end of the day.
if ($inp.hasClass('end-of-day')) {
val += 86399; // 1 Day - 1 Second
}*/
//}
if (val) {
query.filter[filter[0]] = val;
}
}
} else {
query.filter.push(filter);
}
}
// Empty group
query.group = [];
if (settings.query.group) {
for (var g = 0, group_count = settings.query.group.length; g < group_count; g++) {
group = settings.query.group[g];
if (bind_regex.test(group[0])) {
matches = bind_regex.exec(group[0]);
if (matches.length == 2) {
val = $form.find('input[name="'+matches[1]+'"]:checked').val();
if (val) {
query.group.push([val, group[1]]);
}
}
} else if (bind_regex.test(group[1])) {
matches = bind_regex.exec(group[1]);
if (matches.length == 2) {
val = $form.find('select[name="'+matches[1]+'"]').val();
if (val) {
query.group.push([group[0], val]);
}
}
} else {
query.group.push(group);
}
}
}
return query;
},
/**
* Refresh data
*
* @param {Object} settings Override element's stored settings
*/
refresh: function (settings) {
var $this = $(this),
data = $this.data('chart'),
height = $this.height(),
query;
// Allow override of settings for refresh
settings = settings || $.extend(true, {}, data.settings);
if ( ! settings.query) throw('Missing `query` setting');
$this.empty().addClass('loading').css('height', height+'px');
// log('---------------------------------------------');
// log('this:', $this);
// log('data:', data);
// log('refresh query select:', settings.query.select);
// return;
// Parse query filter bindings (to form)
query = methods._parse_query_binds(settings);
//log('query', query);
$this.trigger('beforerefresh', [ query ]);
if (settings.query) {
var quertData = query.filter;
if (query.sort) {
quertData.sort = query.sort;
}
if (query.order) {
quertData.order = query.order;
}
$.ajax({
type: 'GET',
url: window.pass.site.domain+'/api/v1/addon_ga/?report_id='+settings.report_id,
data: quertData,
success: function (response) {
// Process response
var schema = methods.post_process.apply($this[0], [ response, query ]);
// Prepare small DataView model
var model = {
data: response,
set_data: function(data) {
this.data = data;
},
get_data: function() {
return this.data;
},
sum: function (field_name) {
var total = 0;
$.each(this.data.payload, function (i, row) {
total += parseInt(row[field_name]);
});
return total;
},
get_item: function (i) {
return this.data.payload[i];
},
get_total: function () {
return this.data.metadata['total'];
}
};
model.set_data(response);
$this.trigger('refresh', [ model ]);
// Update view
methods.update.apply($this[0], [ schema ]);
}
});
} else if ( ! settings.model) {
throw('Must supply query or model for chart to work');
}
},
/**
* Process AJAX response, and returns the final schema
*
* @param {Object} response Object with { payload: [] }
* @param {Object} query Optional: Query (with bind values) that was used to request data
*/
post_process: function (response, query) {
// Check if the element really exist
if ($(this).data('chart') === undefined) {
return false;
}
var $this = $(this),
data = $this.data('chart'),
payload = response.payload || [],
settings = data.settings,
provider = $.fn.chart.providers[settings.provider],
xaxis = { categories: [] },
series = [],
content,
fields;
// console.log('post_process', $.extend({}, response), $.extend({}, query));
// Fill in gaps - Only if query is provided, and xaxis.type is 'date'
if (query && settings.xaxis.type == 'date' && payload.length) {
settings.parse_format = settings.parse_format || 'yyyymmdd';
// Pad missing month/day into label, if in yy-mm-dd mysql date format
/*$.each(payload, function (index, record) {
var label = record[settings.xaxis.field];
if (settings.parse_format == 'yy-mm-dd' && label.length < 10) {
// Grouping by week, set the date as the first day of the week
if (settings.xaxis.field == 'week') {
var week = parseInt(label.split('-')[1], 10),
year = parseInt(label.split('-')[0], 10),
year_first_day = new Date(year, 0, 1),
year_first_day_timestamp = year_first_day.getTime() / 1000,
week_days_in_seconds = (week-1) * 7 * 24 * 60 * 60,
week_first_day = new Date((year_first_day_timestamp + week_days_in_seconds + year_first_day.getTimezoneOffset()*60) * 1000);
label = week_first_day.getFullYear()+'-'+(('0' + (week_first_day.getMonth() + 1)).slice(-2))+'-'+(('0' + week_first_day.getDate()).slice(-2));
} else {
label += '-01' + (label.length == 4 ? '-01' : '');
}
record[settings.xaxis.field] = label;
}
});*/
var date = new Date,
from = new Date(),
to = new Date(),//$.datepicker.parseDate(settings.parse_format, payload[payload.length - 1][settings.xaxis.field]),
ascending,
new_payload = [],
count = 0,
counter = 0,
total = 0;
// Searching for from/to date from query filter
$.each(query.group, function (i, field) {
var name = $.isArray(field) ? field[0] : field,
alias = $.isArray(field) ? field[1] : field;
if (alias == settings.xaxis.field) {
$.each(query.filter, function (j, condition) {
if (condition[0] == name) {
// Convert filter timestamp from user's timezone to user local time
var filter_time = (condition[2] + (date.getTimezoneOffset() * 60) + App.user_timezone_offset) * 1000;
if ( ! from || filter_time < from.getTime()) {
from = new Date(filter_time);
}
if ( ! to || filter_time > to.getTime()) {
to = new Date(filter_time);
}
}
});
}
});
ascending = from.getTime() < to.getTime();
// console.log('debug', { payload: payload, from: from, to: to, query: query, xaxis: settings.xaxis });
var _MS_PER_DAY = 1000 * 60 * 60 * 24;
/**
* Helper - Compare two dates by: day, week, month, year
* @param {String} date_part Possible values: day, week, month, year
* @param {Date} local First date object
* @param {Date} remote Second date object
*/
function date_part_is_diff(date_part, local, remote) {
if (date_part == 'day' || date_part == 'week') {
// Calculate the days diff between local & remote dates
var day_diff = Math.floor((remote.getTime() - local.getTime()) / _MS_PER_DAY);
if (date_part == 'day') {
return (day_diff > 0);
} else if (date_part == 'week') {
return (day_diff > 7);
}
}
else if (date_part == 'month') {
return (local.getMonth() != remote.getMonth() || local.getFullYear() != remote.getFullYear());
} else if (date_part == 'year') {
return (local.getFullYear() != remote.getFullYear());
}
}
/**
* Helper - Increment a date's part: day, week, month, year
* @param {String} date_part Possible values: day, week, month, year
* @param {Date} date Date to increment
* @param {Boolean} ascend Ascend?
*/
function date_part_increment(date_part, date, ascend) {
if (date_part == 'day') {
date.setDate(date.getDate() + (ascend ? 1 : -1));
} else if (date_part == 'week') {
date.setDate(date.getDate() + (ascend ? 7 : -7));
} else if (date_part == 'month') {
date.setMonth(date.getMonth() + (ascend ? 1 : -1));
} else if (date_part == 'year') {
date.setFullYear(date.getFullYear() + (ascend ? 1 : -1));
}
}
// Iterate through all payload records
$.each(payload, function (index, record) {
/*var actual = $.datepicker.parseDate(settings.parse_format, record[settings.xaxis.field]),
new_record,
ascend;
// Prepare a new record array
new_record = $.extend({}, record);
$.each(new_record, function (key, val) {
if (key !== settings.xaxis.field) {
total+=parseFloat(val);
new_record[key] = count;
}
});
// console.log('--- debug ---', { xaxis_field: settings.xaxis.field, from: from, actual: actual });
// Detect date gaps and fill them
if (date_part_is_diff(settings.xaxis.field, from, actual)) {
// console.log('1st iteration', from, '!=', actual);
while (date_part_is_diff(settings.xaxis.field, from, actual)) {
// Insert empty new record
new_record[settings.xaxis.field] = $.datepicker.formatDate(settings.parse_format, from);
new_payload.push($.extend({}, new_record));
counter++;
ascend = from.getTime() < actual.getTime();
// Increment 'from' indicator
date_part_increment(settings.xaxis.field, from, ascend);
}
}*/
// show aggregated value in each record
if (settings.aggregate_values) {
$.each(record, function (key, val) {
if (key !== settings.xaxis.field) {
count+=parseFloat(val);
record[key] = count;
}
});
}
new_payload.push(record);
counter++;
// Increment 'from' indicator
date_part_increment(settings.xaxis.field, from, ascending);
});
// Previous iteration always increment `from` at the end. We need to cancel the last increment...
date_part_increment(settings.xaxis.field, from, ! ascending);
// console.log('#1 pass: from = ', from.toDateString(), ' to = ', to.toDateString());
// Fill missing dates at the end
if (date_part_is_diff(settings.xaxis.field, from, to)) {
var ascend = from.getTime() < to.getTime(),
new_record = $.extend({}, payload[0]),
new_date, current_date, avg;
// log('debug 2nd', { xaxis_field: settings.xaxis.field, from: from.toDateString(), to: to.toDateString() });
$.each(new_record, function (key, val) {
if (key !== settings.xaxis.field) {
new_record[key] = count;
}
});
while (date_part_is_diff(settings.xaxis.field, from, to)) {
// Increment 'from' indicator
date_part_increment(settings.xaxis.field, from, ascend);
new_date = $.datepicker.formatDate(settings.parse_format, from);
current_date = $.datepicker.formatDate(settings.parse_format, new Date);
// console.log('# new_date = ', new_date, ' current_date = ', current_date);
// Insert new record
new_record[settings.xaxis.field] = new_date;
if (new_date > current_date) {
if (settings.future_average) {
// Use Average value for future dates
avg = total/counter;
count+=parseFloat(avg);
new_record[settings.series.data] = counter ? count : 0;
}
} else {
counter++;
}
new_payload.push($.extend({}, new_record));
}
}
// Switch new payload
payload = new_payload;
}
// console.log('store response', $.extend({}, response, payload));
// Store response
data.response = response || { payload: [] };
data.response.payload = payload;
$this.data('chart', data);
// Merge provider template with settings.options provided by user
content = $.extend(true, {}, provider.template, settings.options);
// Render series
// Use extension/user series fields. Either by type of chart, or in series.data
fields = settings.series[settings.type]
&& $.isArray(settings.series[settings.type].data) && settings.series[settings.type].data.length
? settings.series[settings.type].data
: settings.series.data;
// Data fields can be an object
if ($.isPlainObject(fields)) {
if (fields.all && payload[0]) {
fields = [];
$.each(payload[0], function (key, value) {
if (key != settings.xaxis.field) {
fields.push(key);
}
});
}
}
// Process array of strings
if ($.isArray(fields)) {
$.each(fields, function (i, field_name) {
var name = field_name.replace('ga:', ''),
set = { name: (name.charAt(0).toUpperCase() + name.slice(1)), data: [] };
set.name = set.name.replace(/_/g, ' ').replace(/^([a-z])|\s+([a-z])/g, function ($1) { return $1.toUpperCase() });
$.each(payload, function (i, record) {
set.data.push(+(record[field_name]));
});
series.push(set);
});
} else if (payload && payload.length) {
throw('Fields data for series is invalid')
}
// Render X-axis categories
$.each(payload, function (index, record) {
var field = settings.xaxis.field,
type = settings.xaxis.type,
label = record[field];
if (label) {
label = label.charAt(0).toUpperCase() + label.slice(1);
}
// Call extension/user x-axis formatter function
if ($.isFunction(settings.xaxis.formatter)) {
label = settings.xaxis.formatter.apply($this[0], [ label, record ]);
// Call internal formatters
} else if ($.isFunction(methods.xaxis.formatters[type])) {
label = methods.xaxis.formatters[type].apply($this[0], [ label, record, settings.xaxis ]);
}
// Prepend new label into categories collection
xaxis.categories.push(label);
});
// Call custom provider post_process() function
if ($.isFunction(provider.post_process)) {
provider.post_process.apply($this[0], [ content, xaxis, series ])
} else {
throw('Provider is missing post_process()');
}
// Call custom decorator
if (settings.series.labels.decorator && $.isFunction(provider.series.labels.decorators[settings.series.labels.decorator])) {
provider.series.labels.decorators[settings.series.labels.decorator].apply($this[0], [ content, settings ])
}
return content;
},
/**
* Update chart element with final schema
*
* @param {Object} schema The complete object to create chart with
*/
update: function (schema) {
// Check if the element really exist
if ($(this).data('chart') === undefined) {
return false;
}
var $this = $(this),
data = $this.data('chart'),
settings = data.settings,
provider = $.fn.chart.providers[settings.provider],
$date_from = $('#date_from'),
$date_to = $('#date_to'),
subtitle;
$this.trigger('beforeload', [ schema ]);
// TODO: Assuming date range is in page like this, is wrong for a self-contained module
// Display from-to dates
if (settings.show_date_range) {
if (typeof(settings.show_date_range) == 'function') {
subtitle = settings.show_date_range(data.response);
} else if ($date_from.length && $date_to.length) {
subtitle = $date_from.val() + ' - ' + $date_to.val();
}
// Update highcharts subtitle
if (subtitle) {
$this.parents('.chart:first').find('.date-range')
.empty()
.append($('<span />').text('Date'))
.append(
$('<span />')
.addClass('blue')
.text(subtitle)
);
}
}
// Call custom provider update() function
if ($.isFunction(provider.update)) {
data.chart = provider.update.apply(this, [ schema ]);
$this.data('chart', data);
} else {
throw('Provider is missing update()');
}
$this.trigger('load', [ data.chart ]);
$this.removeClass('loading').css('height', '');
},
// Internal helpers
xaxis: {
formatters: {
/**
* Formats date fields by a given format
* Formats: http://docs.jquery.com/UI/Datepicker/parseDate
*
* @param {String} label
* @param {Object} record
* @param {Object} args
*/
date: function (label, record, args) {
return label;
}
}
}
};
/**
* Chart plugin
*
* @param method
*/
$.fn.chart = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method '+method+' does not exist on jQuery.chart');
}
}
};
/**
* Set default settings
*
* @param {Object} options
*/
$.fn.chart.set_defaults = function (options) {
$.extend(true, default_settings, options || {});
};
// ------------------------------------------------------- PROVIDERS -------------------------------------------------------
$.fn.chart.providers = {
/**
* Provider: Highcharts
*/
Highcharts: {
/**
* Highcharts - Post process
*
* @param {Object} content
* @param {Object} xaxis
* @param {Array} series
*/
post_process: function (content, xaxis, series) {
var $this = $(this),
data = $this.data('chart'),
settings = data.settings;
//console.log('highcharts post_process', content, xaxis, series);
// Pie: Custom formatting
if (settings.type == 'pie') {
content.series = [ { data: [] }];
if (settings.series.pie && settings.series.pie.grouping) {
// Summarize each series data values into one
$.each(series, function (index, serie) {
var name = serie.name,
sum = 0;
// Summarize each data value
$.each(serie.data, function(i, count) {
sum += count;
});
content.series[0].data.push([ name, sum ]);
});
} else {
$.each(series, function (index, serie) {
$.each(serie.data, function(i, value) {
content.series[0].data.push([ xaxis.categories[i], value ]);
});
});
}
} else {
// Limit column/bar charts by width/height ratio
if (settings.type == 'column' || settings.type == 'bar') {
// Set the sweet spot! how much does does a column or bar weight in pixels?
var stacks = (settings.options && settings.options.plotOptions && settings.options.plotOptions[settings.type]
&& settings.options.plotOptions[settings.type].stacking == 'normal')
|| (settings.options && settings.options.plotOptions && settings.options.plotOptions.series
&& settings.options.plotOptions.series.stacking == 'normal')
? 1 : series.length,
weight = 10 * stacks,
el_size = settings.type == 'column' ? $this.width() : $this.height();
// See if there's too much categories for our element to contain
if (xaxis.categories.length * weight > el_size) {
// We have a bar overflow!
// See how many columns/bars we can hold in our element..
var limit = Math.round(el_size / weight),
// How many shall we remove?
remove = xaxis.categories.length - limit;
// Splice them categories yo!
xaxis.categories.splice(0, remove);
// Splice each series data set also
$.each(series, function (index, serie) {
serie.data.splice(0, remove);
});
// If type is not date, it won't get an automatic title. Warn the user we limited his result!
if (( ! settings.xaxis.type || settings.xaxis.type != 'date' ) && xaxis.categories.length > 1) {
content.title.text = 'Showing top '+limit+' items';
}
}
}
// Update main content with processed series and x-axis categories
content.series = series;
content.xAxis.categories = xaxis.categories;
}
// Custom Funnel
if (settings.type == 'funnel') {
// Change x by window width