-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcomponent.js
165 lines (131 loc) · 4.71 KB
/
component.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
COMPONENT('barchart', 'pl:20;pt:10;pb:25;prselected:0;axisX:true;axisY:true;paddingbars:5;limit:0;paddinggroup:10;radius:2;offsetX:10;offsetY:10;templateY:{{ value | format(0) }};templateX:{{ value }};height:0', function(self, config, cls) {
var svg, g, axis, selected;
var templateX, templateY;
self.readonly();
self.nocompile();
self.make = function() {
self.aclass(cls);
self.empty().append('<svg></svg>');
svg = self.find('svg');
axis = svg.asvg('g').attr('class', 'axisy');
g = svg.asvg('g').attr('class', 'bars');
selected = svg.asvg('text').attr('class', 'selected').attr('text-anchor', 'end');
self.on('resize + resize2', self.resize);
self.event('click mouseenter', 'rect', function(e) {
var rect = $(this);
var index = rect.attrd('index');
if (index === self.$selectedindex && e.type === 'mouseenter')
return;
self.$selectedindex = index;
var arr = index.split(',');
var item = self.get()[+arr[0]];
var value = item.values[+arr[1]];
selected.text(templateY({ value: value.y }));
if (e.type === 'mouseenter') {
setTimeout2(self.id, function() {
selected.text('');
}, 2000);
} else
clearTimeout2(self.id);
});
};
self.resize = function() {
setTimeout2('resize.' + self.id, function() {
self.refresh();
}, 500);
};
self.configure = function(key, value, init) {
switch (key) {
case 'templateX':
templateX = Tangular.compile(value);
break;
case 'templateY':
templateY = Tangular.compile(value);
break;
default:
!init && self.resize();
break;
}
};
self.released = function(is) {
!is && setTimeout(self.refresh, 1000);
};
self.setter = function(value) {
if (!self.element[0].offsetParent) {
setTimeout(function() {
self.refresh();
}, 1000);
return;
}
if (!value) {
g.empty();
return;
}
var maxX = 0;
var maxY = 0;
var labels = [];
var paddingbars = config.paddingbars;
var paddinggroup = config.paddinggroup;
var len = value.length;
var size = value[0].values.length;
var width = typeof(config.width) === 'string' ? self.parent(config.width).width() : config.width || self.element.width();
var height = typeof(config.height) === 'string' ? self.parent(config.height).height() : config.height || (width / 100) * 60;
if (config.marginW)
width -= config.marginW;
if (config.marginH)
height -= config.marginH;
var barwidth = ((width - paddingbars - paddinggroup - config.pl) / (size * len));
var lines = {};
barwidth -= paddingbars + (paddinggroup / len);
for (var i = 0; i < len; i++) {
var item = value[i];
labels.push(item.name);
for (var j = 0, length = item.values.length; j < length; j++) {
var val = item.values[j];
maxX = Math.max(maxX, val.x);
maxY = Math.max(maxY, val.y);
}
}
if (config.limit)
maxY = config.limit;
svg.attr('width', width);
svg.attr('height', height);
selected.attr('transform', 'translate({0},30)'.format(width - config.prselected));
g.empty();
axis.empty();
lines.height = height - config.pt - config.pb;
var T = { value: null };
for (var i = 5; i > 0; i--) {
var val = i * 20;
var y = (((lines.height / 100) * val) + config.pt);
config.axisY && axis.asvg('line').attr('x1', 0).attr('x2', width).attr('y1', y).attr('y2', y).attr('class', 'axis');
T.value = (maxY / 100) * (100 - val);
axis.asvg('text').aclass('ylabel').attr('transform', 'translate({0},{1})'.format(config.offsetX, y - config.offsetY)).text(templateY(T));
}
var offsetX = config.pl + paddingbars + paddinggroup;
var posX = 0;
var offsetL = (len - 1) === 0 ? 0.5 : len - 1;
var offsetY = config.pb;
for (var i = 0, length = size; i < length; i++) {
for (var j = 0; j < len; j++) {
var item = value[j];
var val = item.values[i];
var rect = g.asvg('rect');
var y = ((val.y / maxY) * 100) >> 0;
var x = posX + (barwidth * j);
var h = lines.height.inc('{0}%'.format(y));
x += offsetX + (paddingbars * j);
T.value = val.y;
rect.attr('x', x).attr('y', ((lines.height - h) + (offsetY / 2)) - 3).attr('width', barwidth).attr('height', h).attr('class', 'bar bar' + (j + 1)).attr('data-index', j + ',' + i);
config.radius && rect.attr('rx', config.radius).attr('ry', config.radius);
}
T.value = val.x;
var text = templateX(T);
var ax = posX + offsetX + (barwidth * len) + (paddingbars * len) + 2;
config.axisX && axis.asvg('line').attr('x1', ax).attr('x2', ax).attr('y1', 0).attr('y2', height - 25).attr('class', 'axis');
g.asvg('text').aclass('xlabel').text(text).attr('text-anchor', 'middle').attr('transform', 'translate({0},{1})'.format(posX + offsetX + (barwidth * offsetL), height - 6));
posX += (len * barwidth) + paddinggroup;
offsetX += len * paddingbars;
}
};
});