forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
point.js
242 lines (197 loc) · 6.63 KB
/
point.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
(function() {
'use strict';
function mg_filter_out_plot_bounds (data, args) {
// max_x, min_x, max_y, min_y;
var x = args.x_accessor;
var y = args.y_accessor;
var new_data = data.filter(function(d){
return (args.min_x === null || d[x] >= args.min_x) &&
(args.max_x === null || d[x] <= args.max_x) &&
(args.min_y === null || d[y] >= args.min_y) &&
(args.max_y === null || d[y] <= args.max_y);
})
return new_data;
}
function pointChart(args) {
this.init = function(args) {
this.args = args;
raw_data_transformation(args);
process_point(args);
init(args);
x_axis(args);
y_axis(args);
this.mainPlot();
this.markers();
this.rollover();
this.windowListeners();
return this;
};
this.markers = function() {
markers(args);
if (args.least_squares) {
add_ls(args);
}
return this;
};
this.mainPlot = function() {
var svg = mg_get_svg_child_of(args.target);
var g;
var data = mg_filter_out_plot_bounds(args.data[0], args);
// console.log(data);
//remove the old points, add new one
svg.selectAll('.mg-points').remove();
// plot the points, pretty straight-forward
g = svg.append('g')
.classed('mg-points', true);
var pts = g.selectAll('circle')
.data(data)
.enter().append('svg:circle')
.attr('class', function(d, i) { return 'path-' + i; })
//.attr('clip-path', 'url(#mg-plot-window-' + mg_target_ref(args.target) + ')')
.attr('cx', args.scalefns.xf)
.attr('cy', args.scalefns.yf);
//are we coloring our points, or just using the default color?
if (args.color_accessor !== null) {
pts.attr('fill', args.scalefns.color);
pts.attr('stroke', args.scalefns.color);
} else {
pts.classed('mg-points-mono', true);
}
if (args.size_accessor !== null) {
pts.attr('r', args.scalefns.size);
} else {
pts.attr('r', args.point_size);
}
return this;
};
this.rollover = function() {
var svg = mg_get_svg_child_of(args.target);
//remove the old rollovers if they already exist
svg.selectAll('.mg-voronoi').remove();
//remove the old rollover text and circle if they already exist
svg.selectAll('.mg-active-datapoint').remove();
//add rollover text
svg.append('text')
.attr('class', 'mg-active-datapoint')
.attr('xml:space', 'preserve')
.attr('x', args.width - args.right)
.attr('y', args.top * 0.75)
.attr('text-anchor', 'end');
//add rollover paths
var voronoi = d3.geom.voronoi()
.x(args.scalefns.xf)
.y(args.scalefns.yf)
.clipExtent([[args.buffer, args.buffer + args.title_y_position], [args.width - args.buffer, args.height - args.buffer]]);
var paths = svg.append('g')
.attr('class', 'mg-voronoi');
paths.selectAll('path')
.data(voronoi(mg_filter_out_plot_bounds(args.data[0], args)))
.enter().append('path')
.attr('d', function(d) {
if (d === undefined) {
return;
}
return 'M' + d.join(',') + 'Z';
})
.attr('class', function(d,i) {
return 'path-' + i;
})
.style('fill-opacity', 0)
.on('mouseover', this.rolloverOn(args))
.on('mouseout', this.rolloverOff(args))
.on('mousemove', this.rolloverMove(args));
return this;
};
this.rolloverOn = function(args) {
var svg = mg_get_svg_child_of(args.target);
return function(d, i) {
svg.selectAll('.mg-points circle')
.classed('selected', false);
//highlight active point
var pts = svg.selectAll('.mg-points circle.path-' + i)
.classed('selected', true);
if (args.size_accessor) {
pts.attr('r', function(di) {
return args.scalefns.size(di) + args.active_point_size_increase;
});
} else {
pts.attr('r', args.point_size + args.active_point_size_increase);
}
//trigger mouseover on all points for this class name in .linked charts
if (args.linked && !MG.globals.link) {
MG.globals.link = true;
//trigger mouseover on matching point in .linked charts
d3.selectAll('.mg-voronoi .path-' + i)
.each(function() {
d3.select(this).on('mouseover')(d,i);
});
}
if (args.show_rollover_text) {
var fmt = MG.time_format(args.utc_time, '%b %e, %Y');
mg_update_rollover_text(args,svg,fmt, '\u2022', d.point, i);
}
if (args.mouseover) {
args.mouseover(d, i);
}
};
};
this.rolloverOff = function(args) {
var svg = mg_get_svg_child_of(args.target);
return function(d,i) {
if (args.linked && MG.globals.link) {
MG.globals.link = false;
d3.selectAll('.mg-voronoi .path-' + i)
.each(function() {
d3.select(this).on('mouseout')(d,i);
});
}
//reset active point
var pts = svg.selectAll('.mg-points circle')
.classed('unselected', false)
.classed('selected', false);
if (args.size_accessor) {
pts.attr('r', args.scalefns.size);
} else {
pts.attr('r', args.point_size);
}
//reset active data point text
svg.select('.mg-active-datapoint')
.text('');
if (args.mouseout) {
args.mouseout(d, i);
}
};
};
this.rolloverMove = function(args) {
return function(d, i) {
if (args.mousemove) {
args.mousemove(d, i);
}
};
};
this.update = function(args) {
return this;
};
this.windowListeners = function() {
mg_window_listeners(this.args);
return this;
};
this.init(args);
}
var defaults = {
buffer: 16,
ls: false,
lowess: false,
point_size: 2.5,
label_accessor: null,
size_accessor: null,
color_accessor: null,
size_range: null, // when we set a size_accessor option, this array determines the size range, e.g. [1,5]
color_range: null, // e.g. ['blue', 'red'] to color different groups of points
size_domain: null,
color_domain: null,
active_point_size_increase: 1,
color_type: 'number' // can be either 'number' - the color scale is quantitative - or 'category' - the color scale is qualitative.
};
MG.register('point', pointChart, defaults);
}).call(this);