I am using interval marker to mark a region of [a,b] on the x axis. I need to do two things now:
1.) I need to add tool-tip for the region, i.e. if a user hovers on any area of plot between [a,b] values of x, I should be able to display the tool-tip. As, I understand, I can do this by detecting the coordinates of mouseover and checking if they lie between [a,b] and accordingly display. But isn't there any property of interval marker itself or some other way to do it?
2.) I need to add the label to the region. I did that using setLabel(). But it displays the label horizontally. I want the label vertically. I searched a bit and found the suggestion of adding XYTextAnnotation or over-riding the drawDomainMarker() function of the plot's renderer.
I tried with the former approach (Annotation), the annotations don't appear at correct x-values. But when I zoom in my chart, they are on correct place. Why is this happening when the interval marker is able to draw the intervals properly at correct places?
Edit 1:
An Example, I have this code snippet:
public class Test extends ApplicationFrame {
public Test(final String title) {
super(title);
IntervalXYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private IntervalXYDataset createDataset() {
final XYSeries series = new XYSeries("Random Data");
series.add(1.0, 400.2);
series.add(5.0, 294.1);
series.add(4.0, 100.0);
series.add(12.5, 734.4);
series.add(17.3, 453.2);
series.add(21.2, 500.2);
series.add(21.9, null);
series.add(25.6, 734.4);
series.add(30.0, 453.2);
final XYSeriesCollection dataset = new XYSeriesCollection(series);
return dataset;
}
private JFreeChart createChart(IntervalXYDataset dataset) {
final JFreeChart chart = ChartFactory.createXYBarChart("XY Series Demo", "X", false, "Y", dataset,
PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
ValueMarker vm = new ValueMarker(20);
vm.setLabelOffsetType(LengthAdjustmentType.EXPAND);
vm.setPaint(Color.black);
vm.setStroke(new BasicStroke(2.0F));
vm.setLabel("FLAPS 20");
vm.setLabelFont(new Font("SansSerif", 0, 11));
vm.setLabelPaint(Color.black);
vm.setLabelAnchor(RectangleAnchor.TOP_LEFT);
vm.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
plot.addDomainMarker(0, vm, Layer.FOREGROUND);
XYTextAnnotation textLabel = new XYTextAnnotation("Target Range", 5, 200);
textLabel.setFont(new Font("Sans Serif", Font.BOLD, 10));
textLabel.setRotationAnchor(TextAnchor.CENTER);
textLabel.setTextAnchor(TextAnchor.CENTER_LEFT);
textLabel.setRotationAngle(-3.14 / 2);
textLabel.setPaint(Color.red);
plot.addAnnotation(textLabel);
return chart;
}
public static void main(final String[] args) {
final Test demo = new Test("XY Series Demo 3");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
If we see above, an XYTextAnnotation has been added at the coordinates (x=5 and y=200), but if I see in the graph it doesn't appear to be at that position. Why such? And how it can be resolved?
Edit 2: Regarding the question raised in Edit 1, what I figured out is the rotation is happening around the label's center. That's why after the textLabel is rotated, the position changes. I need to change textLabel.setTextAnchor(TextAnchor.CENTER_LEFT); to textLabel.setTextAnchor(TextAnchor.TOP_LEFT); for it to work. Also the textAnchor should be changes to TOP_LEFT for the text label to be aligned to the required coordinate in my plot.
But still unable to achieve Doubt 1.
Please feel free to correct above.
Thanks