I have a XYPlot with visible crosshair and configured it to be "lockedOnData". Using a XYShapeAndLineRenderer this works fine, but with a XYShapeRenderer instead, the crosshair is not displayed. Looks like it is not able to lock on the data points of the XYShapeRenderer.
Any idea why?
Here I got a minimal working example that demonstrates it:
Code: Select all
public class Demo extends JFrame {
public static void main(String[] args) {
Demo demo = new Demo("Demo", "Demo-Chart");
demo.pack();
demo.setVisible(true);
}
public Demo(String applicationTitle, String chartTitle) {
super(applicationTitle);
XYPlot plot = new XYPlot();
plot.setDomainAxis(new NumberAxis());
plot.setRangeAxis(new NumberAxis());
plot.setDomainCrosshairLockedOnData(true);
plot.setRangeCrosshairLockedOnData(true);
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
JFreeChart chart = new JFreeChart(chartTitle, plot);
XYShapeRenderer renderer = new XYShapeRenderer();
// With the following renderer, the crosshair works as expected:
//XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
DefaultXYDataset dataset = new DefaultXYDataset();
dataset.addSeries(0, createData());
plot.setRenderer(0,renderer);
plot.setDataset(0, dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
setContentPane(chartPanel);
}
private double[][] createData() {
double[][] data = new double[2][5];
data[0][0] = 1;
data[0][1] = 2;
data[0][2] = 3;
data[0][3] = 4;
data[0][4] = 5;
data[1][0] = 4;
data[1][1] = 7;
data[1][2] = 2;
data[1][3] = 6;
data[1][4] = 4;
return data;
}
}