forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor-mixin.js
173 lines (162 loc) · 5.81 KB
/
color-mixin.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
/**
* The Color Mixin is an abstract chart functional class providing universal coloring support
* as a mix-in for any concrete chart implementation.
* @name colorMixin
* @memberof dc
* @mixin
* @param {Object} _chart
* @returns {dc.colorMixin}
*/
dc.colorMixin = function (_chart) {
var _colors = d3.scale.category20c();
var _defaultAccessor = true;
var _colorAccessor = function (d) { return _chart.keyAccessor()(d); };
/**
* Retrieve current color scale or set a new color scale. This methods accepts any function that
* operates like a d3 scale.
* @method colors
* @memberof dc.colorMixin
* @instance
* @see {@link https://github.com/d3/d3-3.x-api-reference/blob/master/Scales.md d3.scale}
* @example
* // alternate categorical scale
* chart.colors(d3.scale.category20b());
* // ordinal scale
* chart.colors(d3.scale.ordinal().range(['red','green','blue']));
* // convenience method, the same as above
* chart.ordinalColors(['red','green','blue']);
* // set a linear scale
* chart.linearColors(["#4575b4", "#ffffbf", "#a50026"]);
* @param {d3.scale} [colorScale=d3.scale.category20c()]
* @returns {d3.scale|dc.colorMixin}
*/
_chart.colors = function (colorScale) {
if (!arguments.length) {
return _colors;
}
if (colorScale instanceof Array) {
_colors = d3.scale.quantize().range(colorScale); // deprecated legacy support, note: this fails for ordinal domains
} else {
_colors = d3.functor(colorScale);
}
return _chart;
};
/**
* Convenience method to set the color scale to
* {@link https://github.com/d3/d3-3.x-api-reference/blob/master/Ordinal-Scales.md#ordinal d3.scale.ordinal} with
* range `r`.
* @method ordinalColors
* @memberof dc.colorMixin
* @instance
* @param {Array<String>} r
* @returns {dc.colorMixin}
*/
_chart.ordinalColors = function (r) {
return _chart.colors(d3.scale.ordinal().range(r));
};
/**
* Convenience method to set the color scale to an Hcl interpolated linear scale with range `r`.
* @method linearColors
* @memberof dc.colorMixin
* @instance
* @param {Array<Number>} r
* @returns {dc.colorMixin}
*/
_chart.linearColors = function (r) {
return _chart.colors(d3.scale.linear()
.range(r)
.interpolate(d3.interpolateHcl));
};
/**
* Set or the get color accessor function. This function will be used to map a data point in a
* crossfilter group to a color value on the color scale. The default function uses the key
* accessor.
* @method colorAccessor
* @memberof dc.colorMixin
* @instance
* @example
* // default index based color accessor
* .colorAccessor(function (d, i){return i;})
* // color accessor for a multi-value crossfilter reduction
* .colorAccessor(function (d){return d.value.absGain;})
* @param {Function} [colorAccessor]
* @returns {Function|dc.colorMixin}
*/
_chart.colorAccessor = function (colorAccessor) {
if (!arguments.length) {
return _colorAccessor;
}
_colorAccessor = colorAccessor;
_defaultAccessor = false;
return _chart;
};
// what is this?
_chart.defaultColorAccessor = function () {
return _defaultAccessor;
};
/**
* Set or get the current domain for the color mapping function. The domain must be supplied as an
* array.
*
* Note: previously this method accepted a callback function. Instead you may use a custom scale
* set by {@link dc.colorMixin#colors .colors}.
* @method colorDomain
* @memberof dc.colorMixin
* @instance
* @param {Array<String>} [domain]
* @returns {Array<String>|dc.colorMixin}
*/
_chart.colorDomain = function (domain) {
if (!arguments.length) {
return _colors.domain();
}
_colors.domain(domain);
return _chart;
};
/**
* Set the domain by determining the min and max values as retrieved by
* {@link dc.colorMixin#colorAccessor .colorAccessor} over the chart's dataset.
* @method calculateColorDomain
* @memberof dc.colorMixin
* @instance
* @returns {dc.colorMixin}
*/
_chart.calculateColorDomain = function () {
var newDomain = [d3.min(_chart.data(), _chart.colorAccessor()),
d3.max(_chart.data(), _chart.colorAccessor())];
_colors.domain(newDomain);
return _chart;
};
/**
* Get the color for the datum d and counter i. This is used internally by charts to retrieve a color.
* @method getColor
* @memberof dc.colorMixin
* @instance
* @param {*} d
* @param {Number} [i]
* @returns {String}
*/
_chart.getColor = function (d, i) {
return _colors(_colorAccessor.call(this, d, i));
};
/**
* **Deprecated.** Get/set the color calculator. This actually replaces the
* {@link dc.colorMixin#getColor getColor} method!
*
* This is not recommended, since using a {@link dc.colorMixin#colorAccessor colorAccessor} and
* color scale ({@link dc.colorMixin#colors .colors}) is more powerful and idiomatic d3.
* @method colorCalculator
* @memberof dc.colorMixin
* @instance
* @param {*} [colorCalculator]
* @returns {Function|dc.colorMixin}
*/
_chart.colorCalculator = dc.logger.deprecate(function (colorCalculator) {
if (!arguments.length) {
return _chart.getColor;
}
_chart.getColor = colorCalculator;
return _chart;
}, 'colorMixin.colorCalculator has been deprecated. Please colorMixin.colors and colorMixin.colorAccessor instead');
return _chart;
};