-
Notifications
You must be signed in to change notification settings - Fork 50
/
BarChartInfoView.js
87 lines (85 loc) · 3.56 KB
/
BarChartInfoView.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
/*
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
define([
'underscore',
'contrail-view'
],function(_,ContrailView) {
var BarChartInfoView = ContrailView.extend({
el: ".chart",
chart: null,
chartSelection: null,
initialize: function(options) {
var self = this;
self.options = options;
self.$el.append($('<div/>',{
class:'infobox-container'
}));
},
renderInfoboxes: function() {
var self = this;
var infoChartCfg = getValueByJsonPath(self,'attributes;viewConfig;config',[],false);
var totalCntModel = getValueByJsonPath(self,'attributes;viewConfig;totalCntModel',null,false);
var data = self.model.getItems();
var dataCF = crossfilter(data);
var chartInfoTmpl = contrail.getTemplate4Id(cowc.TMPL_CHARTINFO);
var totalCntMap = {};
//Sum-up each field across all records
$.each(data,function(idx,obj) {
for(var i=0;i<infoChartCfg.length;i++) {
var currField = infoChartCfg[i]['field'];
var excludeList = infoChartCfg[i]['excludeList'];
if(idx == 0) {
totalCntMap[currField] = 0;
}
if(excludeList != null) {
var doExclude = _.find(excludeList,function(currFilter) {
return obj[currFilter['key']] == currFilter['value'];
});
if(doExclude == null)
totalCntMap[currField] += obj[currField];
} else {
totalCntMap[currField] += obj[currField];
}
}
});
self.$el.find('.infobox-container').html('');
for(var i=0;i<infoChartCfg.length;i++) {
var currCfg = infoChartCfg[i];
var chartCfg = {
title : currCfg['title'],
totalCnt: totalCntModel.has(currCfg['field']) ? '' : totalCntMap[currCfg['field']]
};
self.$el.find('.infobox-container').append(chartInfoTmpl(chartCfg));
var currElem = self.$el.find('.infobox-container .infobox:last');
if(totalCntModel.has(currCfg['field'])) {
$(currElem).find('.infobox-data-number').text(totalCntModel.get(currCfg['field']));
totalCntModel.on('change',function(updatedModel) {
$(currElem).find('.infobox-data-number').text(updatedModel.get(currCfg['field']));
})
}
var sparkLineData = bucketizeCFData(dataCF,function(d) {
return d[currCfg['field']];
});
//Draw sparkline
chUtils.drawSparkLineBar(currElem.find('.sparkline')[0], {
data: sparkLineData['data'],
xLbl: currCfg['title'],
yLbl: currCfg['yLbl']
});
}
},
render: function() {
var self = this;
//Need to initialize crossfilter with model
//If model is already populated
if(self.model.isRequestInProgress() == false) {
self.renderInfoboxes();
}
self.model.onDataUpdate.subscribe(function() {
self.renderInfoboxes();
});
}
});
return BarChartInfoView;
});