Hi Balt,
here is a short demo program that I wrote. The XYDomainValueAnnotation dva1 is an example for a vertical line that has a vertical label.
Note that the vertical label is not placed on the chart by user defined absolute coordinates, but by using the correct combinations of anchors.
The anchors tat I have chosen are the following:
Code: Select all
dva1.setLabelTextAnchor(TextAnchor.TOP_LEFT);//point with which the label is attached to the annotation area
dva1.setLabelAnchor(RectangleAnchor.RIGHT);//point of the annotation area to which the label is attached
dva1.setRotationAnchor(TextAnchor.TOP_LEFT);//point around which the label is rotated
That means: starting from a normal, horizontally oriented label, attach that label with its upper left point to the right horizontal center point of the annotation and then rotate it around its upper left point. In most cases, the labeltextAnchor and the rotationAnchor should be the same, as this is giving the most predictable results, but you can chosse them freely.
You do not need to apply the patch, since existing JFreeChart files are not affected. Just copy the new files into the appropriate folder of the JFreeChart source tree, and recompile the package. You could even copy the files to your own source folder and leave your existing JFreeChart installation completely unchanged.
The demo program also shows that the new annotations support tool tips.
Best regards
Peter
Code: Select all
package jfree;
import java.awt.BasicStroke;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.awt.Shape;
import javax.swing.*;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.ui.RectangleInsets;
import java.awt.Color;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;
public class AxisAnnotationDemo {
public static void main(String[] args) {
int count = 50;
int series = 5;
DefaultXYDataset dataset = new DefaultXYDataset();
long start = System.currentTimeMillis();
long increment = 1000*60*200; //200 min
for(int s = 0; s < series; s++){
double[][] data = new double[2][count];
for(int i = 0; i < count; i++){
data[0][i] = start + increment * i;
data[1][i] = (s + 1)*i;
}
dataset.addSeries("Series " + s, data);
}
DateAxis xAxis = new DateAxis("x axis");
NumberAxis yAxis = new NumberAxis("y axis");
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(false, true);
renderer.setUseOutlinePaint(true);
Shape shape = new Ellipse2D.Double(-4, -4, 8, 8);
for(int i = 0; i < series; i++){
renderer.setSeriesShape(i, shape);
renderer.setSeriesOutlinePaint(i, Color.black);
}
XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
plot.setInsets(new RectangleInsets(50, 50, 50, 50));
XYRangeIntervalAnnotation ria = new XYRangeIntervalAnnotation(50, 100);
ria.setLabel("RangeInterval");
ria.setToolTipText("Range interval from 50 to 100");
ria.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
ria.setLabelAnchor(RectangleAnchor.LEFT);
plot.addAnnotation(ria);
XYDomainIntervalAnnotation dia = new XYDomainIntervalAnnotation(System.currentTimeMillis()+172800000L,System.currentTimeMillis()+172800000L +86400000L );
dia.setLabel("Interval\n\rMutiline\n\rZeile 3\n\rZeile 4");
dia.setLabelTextAnchor(TextAnchor.TOP_CENTER);
dia.setLabelAnchor(RectangleAnchor.CENTER);
dia.setRotationAnchor(TextAnchor.TOP_CENTER);
plot.addAnnotation(dia);
XYDomainValueAnnotation dva = new XYDomainValueAnnotation();
dva.setLabel("Test Value Annotation");
dva.setStroke(new BasicStroke(5.0f));
dva.setValue(System.currentTimeMillis()+86400000L);
dva.setLabelTextAnchor(TextAnchor.TOP_LEFT);
dva.setLabelAnchor(RectangleAnchor.TOP_LEFT);
dva.setRotationAnchor(TextAnchor.TOP_LEFT);
plot.addAnnotation(dva);
XYDomainValueAnnotation dva1 = new XYDomainValueAnnotation();
dva1.setLabel("Second Test");
dva1.setLabelAngle(-Math.PI/2.0);
dva1.setStroke(new BasicStroke(5.0f));
dva1.setValue(System.currentTimeMillis()+3.5*86400000L);
dva1.setLabelTextAnchor(TextAnchor.TOP_LEFT);
dva1.setLabelAnchor(RectangleAnchor.RIGHT);
dva1.setRotationAnchor(TextAnchor.TOP_LEFT);
plot.addAnnotation(dva1);
XYRangeValueAnnotation rva = new XYRangeValueAnnotation();
rva.setValue(123);
rva.setLabel("Anno Test");
Font f = new Font("Tahoma",1,12);
rva.setLabelFont(f);
rva.setStroke(new BasicStroke(3.0f));
rva.setPaint(Color.BLUE);
rva.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
rva.setLabelAnchor(RectangleAnchor.LEFT);
plot.addAnnotation(rva);
ValueMarker vm = new ValueMarker(133);
vm.setLabel("Marker Test");
vm.setStroke(new BasicStroke(3.0f));
vm.setPaint(Color.BLUE);
vm.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
vm.setLabelAnchor(RectangleAnchor.LEFT);
vm.setLabelFont(f);
plot.addRangeMarker(vm);
BasicStroke lineStroke = new BasicStroke(1.0f);
plot.setDomainMinorGridlinesVisible(true);
plot.setDomainMinorGridlineStroke(lineStroke);
plot.setDomainGridlineStroke(lineStroke);
plot.setDomainMinorGridlinePaint(Color.gray);
plot.setDomainGridlinePaint(Color.BLACK);
JFreeChart chart = new JFreeChart(plot);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ChartPanel(chart));
frame.pack();
frame.setVisible(true);
}
}