-
Notifications
You must be signed in to change notification settings - Fork 0
/
select-control.js
67 lines (49 loc) · 1.77 KB
/
select-control.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
var dc = require('dc');
module.exports = function (parent, chartGroup) {
var _chart = dc.baseMixin({});
var _defaultValue;
var _currentSelection;
/**
#### .defaultValue(primitive)
Explicitly set default selection. If not set, defaults to the first item in the select options.
**/
_chart.defaultValue = function(_) {
if (!arguments.length) return _defaultValue;
_defaultValue = _;
return _chart;
};
_chart.currentSelection = function(_) {
if(!arguments.length) return _currentSelection || _distinctDimensionValues()[0];
//TODO: add ability to make a selection
};
function _distinctDimensionValues() {
return _chart.group().all().map(function(o){return o.key;});
}
_chart._doRender = function () {
_chart.root().classed('select-control', true);
_chart.root().html('');
var selectionList = _chart.root().append('select');
if(_currentSelection === undefined) {
if(_defaultValue === undefined) {
_defaultValue = _distinctDimensionValues()[0];
}
_currentSelection = _defaultValue;
}
selectionList.selectAll('option')
.data(_distinctDimensionValues())
.enter().append('option')
.attr('selected', function(d){ return d === _currentSelection ? '' : null})
.text(function(d){return d})
.attr('value', function(d){return d;});
selectionList.on('change', function(){
_currentSelection = this.value;
// Avoid double-firing _invokeFilteredListener that comes with _chart.filterAll();
_chart.replaceFilter(this.value);
dc.redrawAll();
});
};
_chart._doRedraw = function(){
return _chart._doRender();
};
return _chart.anchor(parent, chartGroup);
};