addDomainMarker label not visible in dynamic plot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
balt
Posts: 5
Joined: Mon Jun 22, 2015 2:49 pm
antibot: No, of course not.

addDomainMarker label not visible in dynamic plot

Post by balt » Mon Jun 22, 2015 3:00 pm

Hi all,

I'm drawing a realtime chart. When certain events occur, the chart gets a vertical line with a short label text highlighting what it signifies. Unfortunately, the label is never drawn, but the line is drawn where and how it should.
Can anyone tell me how I can get the code below to draw the label "FLAPS 20" vertically at the top left of the vertical marker line? Thanks!

Code: Select all

        ValueMarker vm = new ValueMarker(new java.util.Date().getTime());
        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);
        XYPlot plot = (XYPlot) AnalysisWindow.chartEnergy.getPlot();
        plot.addDomainMarker(0, vm, Layer.FOREGROUND);

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: addDomainMarker label not visible in dynamic plot

Post by Naxter » Mon Jun 22, 2015 3:07 pm

This might help you. I use an IntervalMarker, but it should be the same i guess.

Code: Select all

final Marker bst = new IntervalMarker(points.get(0).getX(),
                        points.get(1).getX(), shape.getFillColor(),
                        new BasicStroke(2.0f), null, null, 0.3f);
                    String label = shape.getLabel();
                    bst.setLabel(label);
                    bst.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
                    bst.setLabelFont(new Font("SansSerif", Font.ITALIC + Font.BOLD, 10));
                    bst.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT);
                    getDrawingPanel().getChart().getXYPlot().addDomainMarker(bst);

balt
Posts: 5
Joined: Mon Jun 22, 2015 2:49 pm
antibot: No, of course not.

Re: addDomainMarker label not visible in dynamic plot

Post by balt » Mon Jun 22, 2015 3:20 pm

Thanks, that works for the horizontal label drawing. Any idea how to get the label to draw vertically?

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: addDomainMarker label not visible in dynamic plot

Post by Naxter » Mon Jun 22, 2015 3:22 pm

You do the same just with "addRangeMarker"

balt
Posts: 5
Joined: Mon Jun 22, 2015 2:49 pm
antibot: No, of course not.

Re: addDomainMarker label not visible in dynamic plot

Post by balt » Mon Jun 22, 2015 3:34 pm

No, addRangeMarker draws a horizontal line. I need to rotate the label only (not the vertical line) by 90 degrees. This would seem like a very standard problem yet I can only find posts of many others who have failed... I can rotate the axis labels using setLabelAngle on the axis object, but that method doesn't apply to the ValueMarker object.

Anyone?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: addDomainMarker label not visible in dynamic plot

Post by paradoxoff » Mon Jun 22, 2015 8:59 pm

None of the marker classes supports rotated text. Since markers do not have their own draw-method but are drawn by the renderer, it is not easy to change that behaviour (wou yould have to change several renderer classes).
A while ago, I have started to work on a set of annotations that aim to at replacing the existing marker classes. Maybe you can try the patch. Patch link.

balt
Posts: 5
Joined: Mon Jun 22, 2015 2:49 pm
antibot: No, of course not.

Re: addDomainMarker label not visible in dynamic plot

Post by balt » Tue Jun 23, 2015 12:32 am

thanks for this, but before I apply that patch, do you have some documentation on how it works, i.e. how would I draw the same type of vertical marker as above with a 90 degree rotated text using your patch?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: addDomainMarker label not visible in dynamic plot

Post by paradoxoff » Tue Jun 23, 2015 3:15 pm

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);
    }
}

balt
Posts: 5
Joined: Mon Jun 22, 2015 2:49 pm
antibot: No, of course not.

Re: addDomainMarker label not visible in dynamic plot

Post by balt » Sun Jun 28, 2015 3:07 pm

Hi Peter,

I've had a chance to implement this now, works like a charm, fantastic! Thanks very much!

Let me know if you're ever in Sydney, I'll shout you a pint!

Cheers

- Balt

ekta1994
Posts: 26
Joined: Fri May 12, 2017 8:06 am
antibot: No, of course not.

Re: addDomainMarker label not visible in dynamic plot

Post by ekta1994 » Wed Dec 06, 2017 8:14 am

Hi,

I tried to add the class file XYDomainValueAnnotation in my project. It gives the error that there is no class named ValueAnnotation. What can be done for that?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: addDomainMarker label not visible in dynamic plot

Post by paradoxoff » Wed Dec 06, 2017 7:39 pm

You need to include all files from the ZIP file that you get from the patch on sourceforge.

ekta1994
Posts: 26
Joined: Fri May 12, 2017 8:06 am
antibot: No, of course not.

Re: addDomainMarker label not visible in dynamic plot

Post by ekta1994 » Thu Dec 07, 2017 6:40 am

Yeah. I included all the six files but certain classes it shows cannot be resolved to a type, namely, IntervalAnnotation, ValueAnnotation. These classes are acting as a superclass of the files created, but there aren't any such classes in the jfreechart library. Some help on this issue will be great.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: addDomainMarker label not visible in dynamic plot

Post by paradoxoff » Thu Dec 07, 2017 9:52 am

I just checked the sourceforge-patch page. It contains three zip files, one incomplete with 6 files, and the other ones complete with 10 files. The most recent one (from january 28th, 2017) is complete, and also contains the multiline-label-feature.

Locked