-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
226 lines (177 loc) · 5.97 KB
/
index.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
"use strict";
var getSet = require('get-set');
var extend = require('extend');
function isFunction(func) {
return Object.prototype.toString.call(func) === '[object Function]';
}
// helper data mapping functions
function defDataMap(prop, def) {
if (arguments.length < 2) def = null;
return function(d, v) {
if (isFunction(def)) def = def();
if (arguments.length === 1) return d && d[prop] || def;
d[prop] = v;
};
}
var segDesc = {
unitClass: { writable: true },
dname: { writable: true },
xBaseDomain: { writable: true },
yScale: { writable: true },
base: { writable: true },
g: { writable: true },
// handler width
hdWidth: { writable: true },
// "inherit" events,
on: { enumerable: true, writable: true},
trigger: {writable: true},
init: {
value: function() {
// getters(setters) to be added
getSet(this)([
'name', 'height', 'top', 'data', 'opacity',
'dataView', 'xDomain', 'xRange', 'yDomain', 'yRange',
'dStart', 'dDuration', 'dHeight', 'dY', 'dColor', 'dSortIndex',
'each'
]);
// defaults
this.minWidth = 1;
this.selectable = false;
this.hdWidth = 3;
this.height(0);
this.opacity(0.70);
// default data accessors defDataMap(property, defaultvalue)
this.dStart(defDataMap('start', 0));
this.dDuration(defDataMap('duration', 0));
this.dHeight(defDataMap('height', this.height));
this.dY(defDataMap('y', 0));
this.dColor(defDataMap('color', '#ccc'));
return this;
}
},
load: {
enumerable: true, configurable: true, value: function(base){
this.base = base; // bind the baseTimeLine
this.unitClass = this.name() + '-item';
}
},
bind: {
value: function(g) {
this.g = g;
this.update();
}
},
update: {
enumerable: true, value: function(data) {
data = data || this.data() || this.base.data();
this.data(data);
// this.untouchedXscale = this.base.xScale.copy();
// this.untouchedYscale = this.base.yScale.copy();
this.zoomFactor = this.base.zoomFactor;
var sel = this.g.selectAll('.' + this.unitClass)
.data(data, this.dSortIndex());
var g = sel.enter()
.append('g')
.attr("class", this.unitClass)
.attr('id', function(d) {
return d.id;
});
// .attr("transform", "translate(0, 0)");
g.append('rect')
.attr("class", 'seg')
.attr('fill-opacity', this.opacity());
g.append("line")
.attr("class", 'handle left')
.style("stroke-width", this.hdWidth)
.attr('stroke-opacity', 0);
g.append("line")
.attr("class", 'handle right')
.style("stroke-width", this.hdWidth)
.attr('stroke-opacity', 0);
sel.exit().remove();
this.draw();
}
},
xZoom: {
enumerable: true, value: function(val) {
// console.log(this.xBaseDomain);
// console.log('zooom');
var that = this;
var xScale = this.base.xScale;
var min = xScale.domain()[0],
max = xScale.domain()[1];
var nuData = [];
// var dv = extend(this.defaultDataView(), this.dataView());
this.data().forEach(function(d, i) {
var start = that.dStart()(d);
var duration = that.dDuration()(d);
var end = start + duration;
// if((start + dv.duration(d)) <= max && start >= min) nuData.push(d);
if((start > min && end < max) || (start < min && end < max && end > min) || (start > min && start < max && end > max) || (end > max && start < min)) nuData.push(d);
// if((end < min && start < min) || (end > max && start > max)) nuData.push(d);
});
// this.update(nuData);
this.update();
// var xAxis = this.graph[this.iName];
// xAxis.scale(xScale);
// this.g.call(xAxis);
}
},
draw: {
enumerable: true, configurable: true, value: function(el) {
el = el || this.g.selectAll('.' + this.unitClass);
var that = this;
var g = this.g;
var halfHandler = this.hdWidth * 0.5;
// var dv = extend(this.defaultDataView(), this.dataView());
var base = this.base;
var xScale = base.xScale;
var max = Math.max;
// data mappers
var dStart = this.dStart();
var dDuration = this.dDuration();
var dY = this.dY();
var dColor = this.dColor();
var dHeight = this.dHeight();
var x = function(d) { return xScale(dStart(d)); };
var w = function(d) { return max(that.minWidth, (xScale(dStart(d) + dDuration(d))) - xScale(dStart(d))); };
// var h = function(d) { return max(that.yScale(dv.height(d)), 1); };
var h = function(d) { return max(base.height() - that.yScale(dHeight(d)), 1); };
var y = function(d) { return that.yScale(dY(d)) - h(d); };
// handlers
var lx = function(d) { return xScale(dStart(d)) + halfHandler; };
var rx = function(d) {
var _w = (xScale(dStart(d) + dDuration(d))) - xScale(dStart(d));
var rpos = xScale(dStart(d) + dDuration(d)) - halfHandler;
return (_w < that.minWidth) ? dStart(d) + ((that.minWidth + that.hdWidth) * 2 ) : rpos;
};
var lrh = function(d) { return y(d) + h(d); };
var color = function(d) { return dColor(d); };
var segs = el.selectAll('.seg');
segs.attr('x', x)
.attr('y', y)
.attr('width', w)
.attr('height', h)
.attr('fill', color);
if(!!this.each()) el.each(this.each());
el.selectAll('.handle.left')
.attr("x1", lx)
.attr("x2", lx)
.attr("y1", y)
.attr("y2", lrh)
.attr("fill", color)
.style("stroke", color);
el.selectAll('.handle.right')
.attr("x1", rx)
.attr("x2", rx)
.attr("y1", y)
.attr("y2", lrh)
.attr("fill", color)
.style("stroke", color);
}
}
};
module.exports = function createBaseTimeline(options){
var segmenter = Object.create({}, segDesc);
return segmenter.init();
};