You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

直方图鼠标悬停显示对应输入数值的技术求助

Fixing Histogram Hover Value Display

Hey Kunal, let's get that hover tooltip showing the corresponding value for your histogram bars sorted out! You don't actually need to call your draw() function in the mouseover event—instead, we'll focus on storing the value with each bar and displaying it when the user hovers. Here's a straightforward approach:

Step 1: Attach the Value to Each Bar Element

First, make sure every bar in your histogram has its corresponding value stored somewhere accessible. The easiest way is using HTML5 data-* attributes when you generate the bars (you can integrate this into your existing draw() function):

function draw(inputValues) {
  const histogramContainer = $('#histogram');
  histogramContainer.empty(); // Clear existing bars

  inputValues.forEach(value => {
    // Calculate bar dimensions (adjust this to match your existing scaling logic)
    const barHeight = value * 12; // Example scaling factor
    const bar = $(`<div class="histogram-bar" style="height: ${barHeight}px;"></div>`);
    
    // Store the original input value in a data attribute
    bar.data('bar-value', value);
    
    histogramContainer.append(bar);
  });

  // Bind hover events AFTER generating bars to ensure all new bars are covered
  bindBarHoverEvents();
}

Step 2: Implement the Mouseover/Mouseout Logic

Create a separate function to handle hover interactions. This keeps your code clean and ensures events are properly bound every time you regenerate the histogram:

function bindBarHoverEvents() {
  // Unbind existing events first to avoid duplicate handlers
  $('.histogram-bar').off('mouseover mouseout mousemove');

  $('.histogram-bar')
    .on('mouseover', function(e) {
      const currentValue = $(this).data('bar-value');
      
      // Create a tooltip element (you can also reuse a pre-existing hidden one)
      const tooltip = $('<div class="bar-tooltip"></div>')
        .text(`数值: ${currentValue}`)
        .appendTo('body');

      // Position tooltip near the mouse cursor
      positionTooltip(tooltip, e);

      // Update position as mouse moves over the bar
      $(this).on('mousemove', function(e) {
        positionTooltip(tooltip, e);
      });
    })
    .on('mouseout', function() {
      // Remove tooltip when mouse leaves the bar
      $('.bar-tooltip').remove();
      $(this).off('mousemove');
    });
}

// Helper function to handle tooltip positioning
function positionTooltip(tooltip, event) {
  tooltip.css({
    top: `${event.pageY + 10}px`,
    left: `${event.pageX + 10}px`,
    position: 'absolute',
    zIndex: 1000
  });
}

Step 3: Add Basic Tooltip Styling

Add some CSS to make the tooltip look polished and readable:

.bar-tooltip {
  background-color: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 14px;
  pointer-events: none; /* Prevent tooltip from blocking mouse events */
}

Why You Don't Need to Call draw() in Mouseover

Your draw() function is responsible for generating the histogram itself. Calling it during mouseover would redraw the entire chart unnecessarily—this is inefficient and could cause visual flicker. The approach above focuses on retrieving the pre-stored value and displaying it directly, no redraw required.

内容的提问来源于stack exchange,提问作者Kunal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:25:49