jfreechart:将十字准线覆盖添加到ganttchart

时间:2015-02-06 09:30:53

标签: java user-interface drawing jfreechart

我想在我的Jfreechart的甘特图上添加一个十字线,它与鼠标一样移动here

按照那个例子,我创建了自己的:

private Crosshair xCrosshair;
private Crosshair yCrosshair; 

public void createChartPanel(JFreeChart chart){

    chartPanel = new ChartPanel(chart); 
    chartPanel.setInitialDelay(0);  // make the tooltip appear quicker

    // Mouse wheel listener for the zoom in out feature
    chartPanel.addMouseWheelListener(this);
    // Mouse movement listener for the cross hair feature
    chartPanel.addChartMouseListener(this);

    CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
    this.xCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
    this.xCrosshair.setLabelVisible(true);
    this.yCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
    this.yCrosshair.setLabelVisible(true);
    crosshairOverlay.addDomainCrosshair(xCrosshair);
    crosshairOverlay.addRangeCrosshair(yCrosshair);
    chartPanel.addOverlay(crosshairOverlay);
}

 // add the custom renderer and modify x axis
 public void createCategoryPlot(JFreeChart chart){

    plot = chart.getCategoryPlot();
    ganttRenderer = new Renderer();
    plot.setRenderer(ganttRenderer);

            /* Modify the x axis */
    DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH);
    format.applyPattern("#");

    xAxis = new NumberAxis();
    xAxis.setNumberFormatOverride(format);
    xAxis.setLabel("Cycles");

    CategoryAxis axis = plot.getDomainAxis();
    axis.setLowerMargin(0.05);
    axis.setCategoryMargin(0.55);
    axis.setUpperMargin(0.05);  // modify the position of each task axis
    plot.setRangeAxis(xAxis);

    // add change listener to for change in the axis range
    plot.getRangeAxis().addChangeListener(this); 
    initRange = xAxis.getRange();
    chartPanel.getChart().removeLegend();
}

public void chartMouseMoved(ChartMouseEvent event) {    

    int mouseX = event.getTrigger().getX();
    int mouseY = event.getTrigger().getY();


    Rectangle2D dataArea = this.chartPanel.getScreenDataArea();
    JFreeChart chart = event.getChart();

    double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
    //        double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x);
    this.xCrosshair.setValue(x);

    this.yCrosshair.setLabelGenerator(new CrosshairLabelGenerator(){

        @Override
        public String generateLabel(Crosshair arg0) {
            return "y value";
        }

    });        
}

不幸的是显示了这个错误,这不允许我叠加在类别图上。

 Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jfree.chart.plot.CategoryPlot cannot be cast to org.jfree.chart.plot.XYPlot
at org.jfree.chart.panel.CrosshairOverlay.paintOverlay(CrosshairOverlay.java:233)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1658)

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

ChartFactory.createGanttChart()实例化CategoryPlot,如here所示,但CrosshairOverlay需要XYPlot。您可以使用绘图的现有十字准线实施和基于DEFAULT_TOOL_TIP_FORMAT_STRING中显示的IntervalCategoryToolTipGenerator的自定义工具提示格式获得类似效果。

CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
GanttRenderer r = (GanttRenderer) plot.getRenderer();
r.setBaseToolTipGenerator(new IntervalCategoryToolTipGenerator(
    "{0}, {1}: {3} - {4}", DateFormat.getDateInstance()));