gpt4 book ai didi

java - jfreechart:向甘特图添加十字线覆盖

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:41 27 4
gpt4 key购买 nike

我想在 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()));

关于java - jfreechart:向甘特图添加十字线覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com