-
Notifications
You must be signed in to change notification settings - Fork 87
/
interpolate.js
111 lines (82 loc) · 2.18 KB
/
interpolate.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
export default function(Chart) {
Chart.Interaction.modes.interpolate = function(chart, e, options) {
var items = [];
for (var datasetIndex = 0; datasetIndex < chart.data.datasets.length; datasetIndex++) {
// check for interpolate setting
if (!chart.data.datasets[datasetIndex].interpolate) {
continue;
}
var meta = chart.getDatasetMeta(datasetIndex);
// do not interpolate hidden charts
if (meta.hidden) {
continue;
}
var xScale = chart.scales[meta.xAxisID];
var yScale = chart.scales[meta.yAxisID];
var xValue = xScale.getValueForPixel(e.x);
var data = chart.data.datasets[datasetIndex].data;
var index = data.findIndex(function(o) {
return o.x >= xValue;
});
if (index === -1) {
continue;
}
// linear interpolate value
var prev = data[index - 1];
var next = data[index];
if (prev && next) {
var slope = (next.y - prev.y) / (next.x - prev.x);
var interpolatedValue = prev.y + (xValue - prev.x) * slope;
}
if (chart.data.datasets[datasetIndex].steppedLine && prev) {
interpolatedValue = prev.y;
}
if (isNaN(interpolatedValue)) {
continue;
}
var yPosition = yScale.getPixelForValue(interpolatedValue);
// do not interpolate values outside of the axis limits
if (isNaN(yPosition)) {
continue;
}
// create a 'fake' event point
var fakePoint = {
value: interpolatedValue,
xValue: xValue,
tooltipPosition: function() {
return this._model;
},
hasValue: function() {
return true;
},
_model: {
x: e.x,
y: yPosition
},
_datasetIndex: datasetIndex,
_index: items.length,
_xScale: {
getLabelForIndex: function(indx) {
return items[indx].xValue;
}
},
_yScale: {
getLabelForIndex: function(indx) {
return items[indx].value;
}
},
_chart: chart
};
items.push(fakePoint);
}
// add other, not interpolated, items
var xItems = Chart.Interaction.modes.x(chart, e, options);
for (index = 0; index < xItems.length; index++) {
var item = xItems[index];
if (!chart.data.datasets[item._datasetIndex].interpolate) {
items.push(item);
}
}
return items;
};
}