I want to add a Cross Hair on top of my Jfreechart's Gantt Chart that moves along with mouse similar to what was mentioned here.
Following that example I created my own:
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";
}
});
}
Unfortunately this error is shown, which does not allow me to overlay on top of category plot.
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)
Any ideas on how to work around this?