当用户鼠标在jFreeChart的ValueMarker上时显示标签



现在是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。这与将鼠标悬停在图表的其他非填充部分时的操作相同。

这是不可能的。我在jfree论坛上得到了@paradoxoff的帮助。以下是我如何做到的。

  1. 点击此处下载注释文件
  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. 当鼠标移到注释行上时,工具提示将出现

最新更新