在 jFreeChart 中当用户鼠标悬停在 ValueMarker 上时显示标签

show label when user mouseOver ValueMarker in jFreeChart

现在我是一个 JFreeChart 折线图。我想添加标记来标记一些事件。我已经添加了标记。问题是标记标签已经开始重叠。如果用户将鼠标悬停在标记线上,我想通过仅显示标记标签来解决此问题。如何捕获鼠标悬停事件?

Marker tempMarker = new ValueMarker(hour.getFirstMillisecond());        
tempMarker.setPaint(Color.BLACK); 
tempMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
tempMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);

//instead of this i want a mouseoverlistner and then show the label
tempMarker.setLabel("Event Name");

鼠标悬停代码

chartPanel.addChartMouseListener(new ChartMouseListener() {
    @Override
    public void chartMouseClicked(ChartMouseEvent event) {
        //do something on mouse click
    }

    @Override
    public void chartMouseMoved(ChartMouseEvent event) {
        System.out.println("Entity Type: " + event.getEntity());
    }
});

当我将鼠标悬停在 Marker 对象上时,ChartEntity 被发送到 chartMouseMoved。这与我将鼠标悬停在图表的其他未填充部分上时相同。

这真的不可能。我在 this thread on the jfree 论坛中得到了@paradoxoff 的帮助。这是我如何实现的。

  1. Click here 下载注释文件
  2. 您只需要 org.jfree.chart.annotations 文件夹。将它添加到您的项目中。
  3. 用下面的代码Annotations替换标记

    XYDomainValueAnnotation tempMarker = new XYDomainValueAnnotation();
    tempMarker.setValue(hour.getFirstMillisecond());
    
    //set the color of the lines
    switch (priority) {
        case Constants.HIGH_IMPORTANCE:
            tempMarker.setPaint(Color.RED);
            break;
        case Constants.LOW_IMPORTANCE:
            tempMarker.setPaint(Color.GREEN);
            break;
        default:
            tempMarker.setPaint(Color.BLACK);
    }
    
    // dont set the label
    //tempMarker.setLabel("Event Name");
    
    //set the Tool Tip which will display when you mouse over.
    tempMarker.setToolTipText("Annotation");
    
    //format everything
    tempMarker.setStroke(new BasicStroke(5.0f));        
    tempMarker.setLabelTextAnchor(TextAnchor.TOP_LEFT);
    tempMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    tempMarker.setRotationAnchor(TextAnchor.TOP_LEFT);
    
    //add the annotation lines
    plot.addAnnotation(tempMarker);
    
  4. 当您将鼠标悬停在注释线上时,将出现工具提示。