Cannot Label Vertical Marker

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Cannot Label Vertical Marker

Post by gw1500se » Tue Jun 05, 2018 4:16 pm

I created a method to add a marker on a time series plot with a label. The marker works fine but I cannot get the label to show up.

Code: Select all

public void addMarker(String id_,String name_) {
		Functions.logger("Adding open door marker for "+id_+" "+name_,false);
		Long tod=new Date().getTime();
		ValueMarker marker=new ValueMarker(tod);
		double maxy=probeTimeSeries.get(id_).getMaxY();
		maxy-=(double)name_.length()+12;
		XYTextAnnotation label=new XYTextAnnotation("Door Open: "+name_,tod-1,maxy);
		label.setRotationAnchor(TextAnchor.BASELINE_CENTER);
		label.setTextAnchor(TextAnchor.BASELINE_CENTER);
		label.setRotationAngle(-Math.PI/2);
		label.setPaint(Configuration.getInstance().getMonitor(id_).getColor());
		marker.setStroke(new BasicStroke(2));
		marker.setPaint(Configuration.getInstance().getMonitor(id_).getColor());
		chart.addDomainMarker(marker);
		chart.addAnnotation(label);
	}
I am guessing that my calculations for the label location (XYTextAnnotation) is somewhere off the chart so it does not show up. I want the label to be parallel to the vertical marker starting at the top of the plot. Can someone spot where I am going wrong? TIA.

daress
Posts: 23
Joined: Tue Mar 13, 2007 4:52 pm
Contact:

Re: Cannot Label Vertical Marker

Post by daress » Tue Jun 05, 2018 6:35 pm

I support your guess that you are drawing the XYTextAnnotation off the chart.

The constructor for [url=http://www.jfree.org/jfreechart/api/jav ... ation.html]XYTextAnnotation[url] has the second and thrird arguments as the X and Y location of the text. Not knowing the time bounds of the chart you are drawing, I suspect that the Y coordinate is ok, but your X coordinate, being based on the milliseconds that your code is executing (minus 1), is your problem.

Compare the chart.getPlot().getDomianAxis().getUpperBound() and chart.getPlot().getDomainAxis().getLowerBound() values against the X value you using for the XYTextAnnotation. If X is not between the upper and lower bounds of the domain axis, then you are indeed drawing off the chart.

gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Re: Cannot Label Vertical Marker

Post by gw1500se » Tue Jun 05, 2018 7:09 pm

Thanks for the reply. The chart is updated in real time so the bounds for the time is dynamic and starts with minutes and could go on for hours. Thus the scale will shrink over time. The y-axis is also dynamic and may start at <100 but over time will not exceed 300. Is it not the y-axis that is the problem rather than the x? I did not realize the time was milliseconds but even at -1 millisecond it just means the location will appear to be on the marker rather than just off the marker, right?

daress
Posts: 23
Joined: Tue Mar 13, 2007 4:52 pm
Contact:

Re: Cannot Label Vertical Marker

Post by daress » Tue Jun 05, 2018 7:39 pm

I have never done a dynamic plot before, so again I would suggest adding some debugging output with System.out.println() statements within your addMarker() routine to check the X and Y coordinates against the plot's domain and range axes upper and lower bounds. Verify that those values are reasonable/correct for the plot.

gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Re: Cannot Label Vertical Marker

Post by gw1500se » Wed Jun 06, 2018 1:46 pm

I printed out the values and indeed there is apparently a problem with the getMaxY method. It seems to be returning the min value not the max. At the time of the marker execution the y-axis range is ~70-93 and getMaxY returned 71 when it should have returned 93. Interestingly, getMinY returns the same value as getMaxY. How do I get the maxy of the chart?

gw1500se
Posts: 23
Joined: Tue Nov 21, 2017 3:36 pm
antibot: No, of course not.

Re: Cannot Label Vertical Marker

Post by gw1500se » Wed Jun 06, 2018 2:03 pm

I think I have it. I'm using getRange() instead.

Locked