【问题标题】:show label when user mouseOver ValueMarker in jFreeChart当用户鼠标悬停在 jFreeChart 中的 ValueMarker 时显示标签
【发布时间】:2015-10-02 16:24:20
【问题描述】:

现在我是一个 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。这与我将鼠标悬停在图表的其他未填充部分上时相同。

【问题讨论】:

  • 您可以使用ChartMouseListener(例如:stackoverflow.com/questions/21172794/…)捕获鼠标悬停,但也许您会对工具提示感到满意? (例如:stackoverflow.com/questions/6766223/…
  • @Eric ChartMouseListener 不会在 ValueMarker 上捕获鼠标事件。已经试过了。标记已添加到绘图中,并且 chartmouselistener 位于图表对象上
  • ChartMouseListener 应该捕获图表中发生的所有事件。您可以发布您尝试过的内容吗?当检测到鼠标悬停在特定实体上时,如何动态添加/删除标记?
  • @Eric 我已经用 chartMouseListner 代码更新了我的问题。基本上我已经用它测试过了,它没有提供任何方法来识别鼠标在标记上。

标签: jfreechart


【解决方案1】:

这实际上是不可能的。我在jfree 论坛上的this thread 中获得了@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. 当您将鼠标悬停在注释行上时,工具提示将出现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多