-
Notifications
You must be signed in to change notification settings - Fork 4
/
highcharts-legend-highlighter.src.js
189 lines (170 loc) · 6.58 KB
/
highcharts-legend-highlighter.src.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*!
* Legend Highlighter for Highcharts.js
*
* https://github.com/Sidnet/highcharts-legend-highlighter
*
* Authors:
* Michał Dziekoński (https://github.com/mdziekon)
* Krzysztof Pisera (https://github.com/Krzypis)
*
* Released under the MIT license:
* https://raw.github.com/Sidnet/highcharts-legend-highlighter/master/LICENSE
*/
/* global Highcharts */
(function (Highcharts) {
"use strict";
var addEvent = Highcharts.addEvent;
var isTypeSupported = function (chartType) {
switch (chartType) {
case "line":
case "spline":
case "area":
case "areaspline":
case "arearange":
case "areasplinerange":
case "column":
case "columnrange":
case "bar":
case "scatter":
case "bubble":
case "boxplot":
case "errorbar":
case "waterfall":
case "gauge":
case "pie":
case "funnel":
return true;
default:
return false;
}
};
var getItemType = function (item, series) {
if (item.series) {
return item.series.type;
} else {
return series[item.index].type;
}
};
var getItemIndex = function (item, type) {
if (item.index !== undefined) {
return item.index;
} else {
return item.series.index;
}
};
// Inject "onmouseenter" & "onmouseleave" event listeners into "renderItem"
// function (used in Highcharts.Legend.prototype to render single legend label)
// proceed - default "renderItem" function
// item - series or point assigned to currently rendered legend label
Highcharts.wrap(Highcharts.Legend.prototype, "renderItem", function (proceed, item) {
// Call the default function
proceed.apply(this, Array.prototype.slice.call(arguments, 1));
if ((!this.chart.options.legend.highlightSeries) ||
(this.chart.options.legend.highlightSeries.enabled !== true) ||
(item._legendHighlightReady !== undefined))
{
return;
}
var series = this.chart.series;
var element = item.legendGroup.element;
var itemType = getItemType(item, series);
var itemIndex = getItemIndex(item, itemType);
var options = this.chart.options.legend.highlightSeries;
var settings = {
defaultOpacity: options.defaultOpacity ? options.defaultOpacity : 1,
dimmedOpacity: options.dimmedOpacity ? options.dimmedOpacity : 0.25,
dimPiePieces: options.dimPiePieces
};
if (!isTypeSupported(itemType)) {
console.error("[highcharts-legend-highlight.js]: Chart type \"" +
itemType + "\" is not supported");
return;
}
// (Default events) Dim all data series except the currently hovered over one
addEvent(element, "mouseenter", function () {
var i;
var linkedSeries = {};
if (item.linkedSeries !== undefined) {
for (i = 0; i < item.linkedSeries.length; ++i) {
linkedSeries[item.linkedSeries[i].index] = true;
}
}
for (i = 0; i < series.length; ++i) {
if (i === itemIndex || linkedSeries[i] !== undefined) {
continue;
}
if (series[i].visible) {
series[i].group.attr("opacity", settings.dimmedOpacity);
series[i].markerGroup.attr("opacity", settings.dimmedOpacity);
}
}
});
addEvent(element, "mouseleave", function () {
var i;
var linkedSeries = {};
if (item.linkedSeries !== undefined) {
for (i = 0; i < item.linkedSeries.length; ++i) {
linkedSeries[item.linkedSeries[i].index] = true;
}
}
for (i = 0; i < series.length; ++i) {
if (i === itemIndex || linkedSeries[i] !== undefined) {
continue;
}
if (series[i].visible) {
series[i].group.attr("opacity", settings.defaultOpacity);
series[i].markerGroup.attr("opacity", settings.defaultOpacity);
}
}
});
if (itemType === "pie") {
// Slice out currently hovered over pie piece
// Dim the other ones if "settings.dimmedOpacity" is true
addEvent(element, "mouseenter", function () {
for (var i = 0; i < item.series.data.length; ++i) {
if (item.series.data[i].sliced) {
item.series.data[i].slice(false, true);
item.series.data[i]._restoreSlice = true;
}
if (settings.dimPiePieces) {
item.series.data[i].graphic.attr("opacity", settings.dimmedOpacity);
}
}
item.slice(true, true);
if (settings.dimPiePieces) {
item.graphic.attr("opacity", settings.defaultOpacity);
}
});
addEvent(element, "mouseleave", function () {
item.slice(false, true);
for (var i = 0; i < item.series.data.length; ++i) {
if (item.series.data[i]._restoreSlice) {
item.series.data[i].slice(true, true);
item.series.data[i]._restoreSlice = undefined;
}
if (settings.dimPiePieces) {
item.series.data[i].graphic.attr("opacity", settings.defaultOpacity);
}
}
});
}
if (itemType === "funnel") {
// Dim all data points in funnel chart except the currently hovered over one
addEvent(element, "mouseenter", function () {
for (var i = 0; i < item.series.data.length; ++i) {
if (item.series.data[i] !== item) {
item.series.data[i].graphic.attr("opacity", settings.dimmedOpacity);
}
}
});
addEvent(element, "mouseleave", function () {
for (var i = 0; i < item.series.data.length; ++i) {
if (item.series.data[i] !== item) {
item.series.data[i].graphic.attr("opacity", settings.defaultOpacity);
}
}
});
}
item._legendHighlightReady = true;
});
})(Highcharts);