Repository navigation
Expand file tree
/
Copy pathcontroller.bubble.js
More file actions
179 lines (153 loc) · 4.67 KB
/
Copy pathcontroller.bubble.js
File metadata and controls
179 lines (153 loc) · 4.67 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
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
import DatasetController from '../core/core.datasetController.js';
import {isFinite, valueOrDefault} from '../helpers/helpers.core.js';
/**
* The label of the point's own category: from the index axis when it is a category axis,
* else from the value axis when that one is.
*/
function categoryLabel({iScale, vScale}, parsed) {
const scale = iScale.type === 'category' ? iScale : vScale.type === 'category' ? vScale : null;
const value = scale ? parsed[scale.axis] : null;
return isFinite(value) ? scale.getLabelForValue(value) : '';
}
export default class BubbleController extends DatasetController {
static id = 'bubble';
/**
* @type {any}
*/
static defaults = {
datasetElementType: false,
dataElementType: 'point',
animations: {
numbers: {
type: 'number',
properties: ['x', 'y', 'borderWidth', 'radius']
}
}
};
/**
* @type {any}
*/
static overrides = {
scales: {
x: {
type: 'linear'
},
y: {
type: 'linear'
}
}
};
initialize() {
this.enableOptionSharing = true;
super.initialize();
}
/**
* Parse array of primitive values
* @protected
*/
parsePrimitiveData(meta, data, start, count) {
const parsed = super.parsePrimitiveData(meta, data, start, count);
for (let i = 0; i < parsed.length; i++) {
parsed[i]._custom = this.resolveDataElementOptions(i + start).radius;
}
return parsed;
}
/**
* Parse array of arrays
* @protected
*/
parseArrayData(meta, data, start, count) {
const parsed = super.parseArrayData(meta, data, start, count);
for (let i = 0; i < parsed.length; i++) {
const item = data[start + i];
parsed[i]._custom = valueOrDefault(item[2], this.resolveDataElementOptions(i + start).radius);
}
return parsed;
}
/**
* Parse array of objects
* @protected
*/
parseObjectData(meta, data, start, count) {
const parsed = super.parseObjectData(meta, data, start, count);
for (let i = 0; i < parsed.length; i++) {
const item = data[start + i];
parsed[i]._custom = valueOrDefault(item && item.r && +item.r, this.resolveDataElementOptions(i + start).radius);
}
return parsed;
}
/**
* @protected
*/
getMaxOverflow() {
const data = this._cachedMeta.data;
let max = 0;
for (let i = data.length - 1; i >= 0; --i) {
max = Math.max(max, data[i].size(this.resolveDataElementOptions(i)) / 2);
}
return max > 0 && max;
}
/**
* @protected
*/
getLabelAndValue(index) {
const meta = this._cachedMeta;
const labels = this.chart.data.labels || [];
const {xScale, yScale} = meta;
const parsed = this.getParsed(index);
const x = xScale.getLabelForValue(parsed.x);
const y = yScale.getLabelForValue(parsed.y);
const r = parsed._custom;
return {
label: labels[index] || categoryLabel(meta, parsed),
value: '(' + x + ', ' + y + (r ? ', ' + r : '') + ')'
};
}
update(mode) {
const points = this._cachedMeta.data;
// Update Points
this.updateElements(points, 0, points.length, mode);
}
updateElements(points, start, count, mode) {
const reset = mode === 'reset';
const {iScale, vScale} = this._cachedMeta;
const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode);
const iAxis = iScale.axis;
const vAxis = vScale.axis;
for (let i = start; i < start + count; i++) {
const point = points[i];
const parsed = !reset && this.getParsed(i);
const properties = {};
const iPixel = properties[iAxis] = reset ? iScale.getPixelForDecimal(0.5) : iScale.getPixelForValue(parsed[iAxis]);
const vPixel = properties[vAxis] = reset ? vScale.getBasePixel() : vScale.getPixelForValue(parsed[vAxis]);
properties.skip = isNaN(iPixel) || isNaN(vPixel);
if (includeOptions) {
properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode);
if (reset) {
properties.options.radius = 0;
}
}
this.updateElement(point, i, properties, mode);
}
}
/**
* @param {number} index
* @param {string} [mode]
* @protected
*/
resolveDataElementOptions(index, mode) {
const parsed = this.getParsed(index);
let values = super.resolveDataElementOptions(index, mode);
// In case values were cached (and thus frozen), we need to clone the values
if (values.$shared) {
values = Object.assign({}, values, {$shared: false});
}
// Custom radius resolution
const radius = values.radius;
if (mode !== 'active') {
values.radius = 0;
}
values.radius += valueOrDefault(parsed && parsed._custom, radius);
return values;
}
}