deno.land / x / billboardjs@3.6.0 / ChartInternal / internals / color.ts

نووسراو ببینە
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/** * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */import {select as d3Select} from "d3-selection";import {scaleOrdinal as d3ScaleOrdinal} from "d3-scale";import {document} from "../../module/browser";import {$ARC, $COLOR, $SHAPE} from "../../config/classes";import {KEY} from "../../module/Cache";import {notEmpty, isFunction, isObject, isString} from "../../module/util";import type {IArcData, IDataRow} from "../data/IData";import {d3Selection} from "../../../types";
/** * Set pattern's background color * (it adds a <rect> element to simulate bg-color) * @param {SVGPatternElement} pattern SVG pattern element * @param {string} color Color string * @param {string} id ID to be set * @returns {{id: string, node: SVGPatternElement}} * @private */const colorizePattern = (pattern, color, id: string) => { const node = d3Select(pattern.cloneNode(true));
node .attr("id", id) .insert("rect", ":first-child") .attr("width", node.attr("width")) .attr("height", node.attr("height")) .style("fill", color);
return { id, node: node.node() };};
/** * Get color pattern from CSS file * CSS should be defined as: background-image: url("#00c73c;#fa7171; ..."); * @param {d3Selection} element Chart element * @returns {Array} * @private */function getColorFromCss(element: d3Selection): string[] { const cacheKey = KEY.colorPattern; const {body} = document; let pattern = body[cacheKey];
if (!pattern) { const delimiter = ";"; const content = element .classed($COLOR.colorPattern, true) .style("background-image");
element.classed($COLOR.colorPattern, false);
if (content.indexOf(delimiter) > -1) { pattern = content .replace(/url[^#]*|["'()]|(\s|%20)/g, "") .split(delimiter) .map(v => v.trim().replace(/[\"'\s]/g, "")) .filter(Boolean);
body[cacheKey] = pattern; } }
return pattern;}
// Replacement of d3.schemeCategory10.// Contained differently depend on d3 version: v4(d3-scale), v5(d3-scale-chromatic)const schemeCategory10 = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"];
export default { generateColor(): Function { const $$ = this; const {$el, config} = $$; const colors = config.data_colors; const callback = config.data_color; const ids: string[] = [];
let pattern = notEmpty(config.color_pattern) ? config.color_pattern : d3ScaleOrdinal(getColorFromCss($el.chart) || schemeCategory10).range();
const originalColorPattern = pattern;
if (isFunction(config.color_tiles)) { const tiles = config.color_tiles.bind($$.api)();
// Add background color to patterns const colorizedPatterns = pattern.map((p, index) => { const color = p.replace(/[#\(\)\s,]/g, ""); const id = `${$$.state.datetimeId}-pattern-${color}-${index}`;
return colorizePattern(tiles[index % tiles.length], p, id); });
pattern = colorizedPatterns.map(p => `url(#${p.id})`); $$.patterns = colorizedPatterns; }
return function(d: IDataRow | IArcData | string): string { const id: string = (d as IDataRow).id || (d as IArcData).data?.id || d as string;
const isLine = $$.isTypeOf(id, ["line", "spline", "step"]) || !config.data_types[id]; let color;
// if callback function is provided if (isFunction(colors[id])) { color = colors[id].bind($$.api)(d);
// if specified, choose that color } else if (colors[id]) { color = colors[id];
// if not specified, choose from pattern } else { if (ids.indexOf(id) < 0) { ids.push(id); }
color = isLine ? originalColorPattern[ids.indexOf(id) % originalColorPattern.length] : pattern[ids.indexOf(id) % pattern.length];
colors[id] = color; }
return isFunction(callback) ? callback.bind($$.api)(color, d) : color; }; },
generateLevelColor(): Function | null { const $$ = this; const {config} = $$; const colors = config.color_pattern; const threshold = config.color_threshold; const asValue = threshold.unit === "value"; const max = threshold.max || 100; const values = threshold.values && threshold.values.length ? threshold.values : [];
return notEmpty(threshold) ? function(value) { const v = asValue ? value : (value * 100 / max); let color = colors[colors.length - 1];
for (let i = 0, l = values.length; i < l; i++) { if (v <= values[i]) { color = colors[i]; break; } }
return color; } : null; },
/** * Append data backgound color filter definition * @param {string} color Color string * @private */ generateDataLabelBackgroundColorFilter(color?: string): void { const $$ = this; const {$el, config, state} = $$; const backgroundColors = color || config.data_labels_backgroundColors;
if (backgroundColors) { let ids: string[] = [];
if (isString(backgroundColors)) { ids.push(""); } else if (isObject(backgroundColors)) { ids = Object.keys(backgroundColors); }
ids.forEach(v => { const id = `${state.datetimeId}-labels-bg${$$.getTargetSelectorSuffix(v)}${color ? $$.getTargetSelectorSuffix(color) : ""}`;
$el.defs.append("filter") .attr("x", "0") .attr("y", "0") .attr("width", "1") .attr("height", "1") .attr("id", id) .html(`<feFlood flood-color="${v === "" ? backgroundColors : backgroundColors[v]}" /><feComposite in="SourceGraphic"/>`); }); } },
/** * Get data gradient color url * @param {string} id Data id * @returns {string} * @private */ getGradienColortUrl(id: string): string { return `url(#${this.state.datetimeId}-gradient${this.getTargetSelectorSuffix(id)})`; },
/** * Update linear gradient definition (for area & bar only) * @private */ updateLinearGradient(): void { const $$ = this; const {config, data: {targets}, state: {datetimeId}, $el: {defs}} = $$;
targets.forEach(d => { const id = `${datetimeId}-gradient${$$.getTargetSelectorSuffix(d.id)}`; const supportedType = ($$.isAreaType(d) && "area") || ($$.isBarType(d) && "bar"); const isRotated = config.axis_rotated;
if (supportedType && defs.select(`#${id}`).empty()) { const color = $$.color(d); const { x = isRotated ? [1, 0] : [0, 0], y = isRotated ? [0, 0] : [0, 1], stops = [[0, color, 1], [1, color, 0]] } = config[`${supportedType}_linearGradient`];
const linearGradient = defs.append("linearGradient") .attr("id", `${id}`) .attr("x1", x[0]) .attr("x2", x[1]) .attr("y1", y[0]) .attr("y2", y[1]);
stops.forEach(v => { const stopColor = isFunction(v[1]) ? v[1].bind($$.api)(d.id) : v[1];
linearGradient.append("stop") .attr("offset", v[0]) .attr("stop-color", stopColor || color) .attr("stop-opacity", v[2]); }); } }); },
/** * Set the data over color. * When is out, will restate in its previous color value * @param {boolean} isOver true: set overed color, false: restore * @param {number|object} d target index or data object for Arc type * @private */ setOverColor(isOver: boolean, d): void { const $$ = this; const {config, $el: {main}} = $$; const onover = config.color_onover; let color = isOver ? onover : $$.color;
if (isObject(color)) { color = ({id}) => (id in onover ? onover[id] : $$.color(id)); } else if (isString(color)) { color = () => onover; } else if (isFunction(onover)) { color = color.bind($$.api); }
main.selectAll( isObject(d) ? // when is Arc type `.${$ARC.arc}${$$.getTargetSelectorSuffix(d.id)}` : `.${$SHAPE.shape}-${d}` ).style("fill", color); }};
billboardjs

Version Info

Tagged at
a year ago