-
Notifications
You must be signed in to change notification settings - Fork 11.9k
/
controller.bubble.js
167 lines (146 loc) · 3.88 KB
/
controller.bubble.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
import DatasetController from '../core/core.datasetController';
import {resolveObjectKey, valueOrDefault} from '../helpers/helpers.core';
export default class BubbleController extends DatasetController {
initialize() {
this.enableOptionSharing = true;
super.initialize();
}
/**
* Parse array of objects
* @protected
*/
parseObjectData(meta, data, start, count) {
const {xScale, yScale} = meta;
const {xAxisKey = 'x', yAxisKey = 'y'} = this._parsing;
const parsed = [];
let i, ilen, item;
for (i = start, ilen = start + count; i < ilen; ++i) {
item = data[i];
parsed.push({
x: xScale.parse(resolveObjectKey(item, xAxisKey), i),
y: yScale.parse(resolveObjectKey(item, yAxisKey), i),
_custom: item && item.r && +item.r
});
}
return parsed;
}
/**
* @protected
*/
getMaxOverflow() {
const me = this;
const meta = me._cachedMeta;
const data = meta.data;
let max = 0;
for (let i = data.length - 1; i >= 0; --i) {
max = Math.max(max, data[i].size());
}
return max > 0 && max;
}
/**
* @protected
*/
getLabelAndValue(index) {
const me = this;
const meta = me._cachedMeta;
const {xScale, yScale} = meta;
const parsed = me.getParsed(index);
const x = xScale.getLabelForValue(parsed.x);
const y = yScale.getLabelForValue(parsed.y);
const r = parsed._custom;
return {
label: meta.label,
value: '(' + x + ', ' + y + (r ? ', ' + r : '') + ')'
};
}
update(mode) {
const me = this;
const points = me._cachedMeta.data;
// Update Points
me.updateElements(points, 0, points.length, mode);
}
updateElements(points, start, count, mode) {
const me = this;
const reset = mode === 'reset';
const {xScale, yScale} = me._cachedMeta;
const firstOpts = me.resolveDataElementOptions(start, mode);
const sharedOptions = me.getSharedOptions(firstOpts);
const includeOptions = me.includeOptions(mode, sharedOptions);
for (let i = start; i < start + count; i++) {
const point = points[i];
const parsed = !reset && me.getParsed(i);
const x = reset ? xScale.getPixelForDecimal(0.5) : xScale.getPixelForValue(parsed.x);
const y = reset ? yScale.getBasePixel() : yScale.getPixelForValue(parsed.y);
const properties = {
x,
y,
skip: isNaN(x) || isNaN(y)
};
if (includeOptions) {
properties.options = me.resolveDataElementOptions(i, mode);
if (reset) {
properties.options.radius = 0;
}
}
me.updateElement(point, i, properties, mode);
}
me.updateSharedOptions(sharedOptions, mode, firstOpts);
}
/**
* @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;
}
}
BubbleController.id = 'bubble';
/**
* @type {any}
*/
BubbleController.defaults = {
datasetElementType: false,
dataElementType: 'point',
animations: {
numbers: {
type: 'number',
properties: ['x', 'y', 'borderWidth', 'radius']
}
}
};
/**
* @type {any}
*/
BubbleController.overrides = {
scales: {
x: {
type: 'linear'
},
y: {
type: 'linear'
}
},
plugins: {
tooltip: {
callbacks: {
title() {
// Title doesn't make sense for scatter since we format the data as a point
return '';
}
}
}
}
};