我想完成什么:
我试图在图例中表示气泡大小(z 值)。当该图例元素悬停在上方或关闭时,气泡应缩小到相同大小,就像正常的散点图一样。
到目前为止我尝试过的:
通过添加没有数据的新系列,我能够获得一个图例项来表示尺寸。我将有数据的 2 个系列链接到这个新系列。然后,我覆盖了 Highcharts 图例悬停功能,以便当“大小”图例元素悬停时,链接的系列保持完全可见。
像这样:
series: [
{
type: "bubble",
name: HotSpotResources.Positive,
color: "#2699FB",
data: points[0], //example data like -> {x: 10, y: 12, z: 150, id: "some_id"}
linkedTo: "nsize",
showInLegend: true
},
{
type: "bubble",
name: HotSpotResources.Negative,
color: "#F8A6A6",
data: points[1],
linkedTo: "nsize",
showInLegend: true
},
{
id: "nsize",
type: "bubble",
name: HotSpotsResources.nSize,
color: "#4A4A4A",
marker: {
symbol: `url(${LayoutResources.AppRoot}/assets/images/nsize-icon.svg)`
},
}
],
和图例悬停覆盖:
(function (H) {
H.Legend.prototype.setItemEvents = function (item, legendItem, useHTML) {
const legend = this,
boxWrapper = legend.chart.renderer.boxWrapper,
activeClass = 'highcharts-legend-' + (item.series ? 'point' : 'series') + '-active',
hasLinkedSeries = item.linkedSeries && item.linkedSeries.length ? true : false,
setLinkedSeriesState = (item, state) => {
item.linkedSeries.forEach((elem) => (elem.setState(state)));
};
// Set the events on the item group, or in case of useHTML, the item itself (#1249)
(useHTML ? legendItem : item.legendGroup).on('mouseover', () => {
if (item.visible) {
item.setState('hover');
// Add hover state to linked series
if (hasLinkedSeries) {
setLinkedSeriesState(item, 'hover');
}
// A CSS class to dim or hide other than the hovered series
boxWrapper.addClass(activeClass);
legendItem.css(legend.options.itemHoverStyle);
}
}).on('mouseout', () => {
legendItem.css(H.merge(item.visible ? legend.itemStyle : legend.itemHiddenStyle));
// A CSS class to dim or hide other than the hovered series
boxWrapper.removeClass(activeClass);
item.setState();
}).on('click',(event) => {
const strLegendItemClick = 'legendItemClick',
fnLegendItemClick = () => {
item.setVisible ? item.setVisible() : "";
};
// Pass over the click/touch event. #4.
event = {
browserEvent: event
};
// click the name or symbol
if (item.firePointEvent) { // point
item.firePointEvent(strLegendItemClick, event, fnLegendItemClick);
}
else {
H.fireEvent(item, strLegendItemClick, event, fnLegendItemClick);
}
});
};
})(Highcharts)
到目前为止一切顺利。
我尝试仅使用 css 缩小大小,但作为 svg 这是不可能的/它产生了不良结果。缩放它们也改变了它们的位置,所以这是不可能的。
.highcharts-series-hover {
transform: scale(1.5);
transition: transform 250ms;
}
通过更改标记状态,我能够缩小悬停时的气泡(气泡本身而不是图例项):
plotOptions: {
bubble: {
marker: {
states: {
hover: {
radius: 4
}
}
}
}
}
但我希望在将鼠标悬停在尺寸图例元素上时得到相同的结果,而不是在将鼠标悬停在气泡上时得到相同的结果。
最后,这是我创建的图表的屏幕截图以供引用:
任何人都可以找到一种方法来操纵 Highcharts 来完成这项任务或帮助我指明正确的方向吗?
最佳答案
您可以在 renderItem
方法的包装中添加 onmouseover
和 onmouseout
事件,并将未悬停的系列类型更改为 分散
。
(function(H) {
H.wrap(H.Legend.prototype, 'renderItem', function(proceed, item) {
proceed.call(this, item);
var chart = this.chart,
series = chart.series,
element = item.legendGroup.element;
element.onmouseover = function() {
series.forEach(function(s) {
if (s !== item) {
s.update({
type: 'scatter'
}, false);
}
});
chart.redraw();
}
element.onmouseout = function() {
series.forEach(function(s) {
if (s !== item) {
s.update({
type: 'bubble'
}, false);
}
});
chart.redraw();
}
});
}(Highcharts));
<小时/>
现场演示: https://jsfiddle.net/BlackLabel/r64f3w5n/
API引用: https://api.highcharts.com/class-reference/Highcharts.Series#update
文档: https://www.highcharts.com/docs/extending-highcharts/extending-highcharts
关于javascript - Highcharts:如何缩小图例元素悬停时的气泡半径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61331944/