我想在我的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)
关于如何解决此问题的任何想法?
最佳答案
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()));