forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor-chart.js
More file actions
63 lines (49 loc) · 1.68 KB
/
Copy pathcolor-chart.js
File metadata and controls
63 lines (49 loc) · 1.68 KB
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
dc.colorChart = function(_chart) {
var _colors = d3.scale.category20c();
var _colorDomain = [0, _colors.range().length];
var _colorCalculator = function(value) {
var minValue = _colorDomain[0];
var maxValue = _colorDomain[1];
if (isNaN(value)) value = 0;
if(maxValue == null) return _colors(value);
var colorsLength = _chart.colors().range().length;
var denominator = (maxValue - minValue) / colorsLength;
var colorValue = Math.abs(Math.min(colorsLength - 1, Math.round((value - minValue) / denominator)));
return _chart.colors()(colorValue);
};
var _colorAccessor = function(d, i){return i;};
_chart.colors = function(_) {
if (!arguments.length) return _colors;
if (_ instanceof Array) {
_colors = d3.scale.ordinal().range(_);
var domain = [];
for(var i = 0; i < _.length; ++i){
domain.push(i);
}
_colors.domain(domain);
} else {
_colors = _;
}
_colorDomain = [0, _colors.range().length];
return _chart;
};
_chart.colorCalculator = function(_){
if(!arguments.length) return _colorCalculator;
_colorCalculator = _;
return _chart;
};
_chart.getColor = function(d, i){
return _colorCalculator(_colorAccessor(d, i));
};
_chart.colorAccessor = function(_){
if(!arguments.length) return _colorAccessor;
_colorAccessor = _;
return _chart;
};
_chart.colorDomain = function(_){
if(!arguments.length) return _colorDomain;
_colorDomain = _;
return _chart;
};
return _chart;
};