Legend Position

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Rajnesh
Posts: 19
Joined: Thu Jan 03, 2008 8:48 am

Legend Position

Post by Rajnesh » Tue Jan 08, 2008 10:46 am

Hi
Is it possible to display the Legend inside the plot area or chartArea (not ouside or bottom).

Awaiting for ur reply.....
Thanx
Raj

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 08, 2008 11:30 am

There is some code to do this, but it is still experimental - see:

org.jfree.experimental.chart.annotations.XYTitleAnnotation.java
org.jfree.experimental.chart.demo.XYTitleAnnotationDemo1.java
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

mickish
Posts: 29
Joined: Tue Jan 08, 2008 11:15 pm
Location: Venice, Florida

XYTitleAnnotation coordinate arguments

Post by mickish » Tue Jan 08, 2008 11:56 pm

Can you please elaborate on the (x,y) coordinate arguments in the XYTitleAnnotation constructor?

It seems they are in the range [0, 1]. On a standard date domain axis, the x coordinate behaves like a percentage (x=.05 puts the legend 5% away from the left of the chart).

But the interaction with LogAxis is confusing. My legend at y=.2 appears in the middle of my chart, rather than 20% away from the bottom as I expect.

The difference between log axis values is 70 at the bottom, and 1400 at the top. Changes in the XYTitleAnnotation value of y for low values of y affect the position more than the same changes for high values of y.

I guess the y value is used as a percentage of the absolute range values, but I expected it to be a percentage of pixels on the chart.

Would it be reasonable/practical to change the coordinate system to pixels instead of range values?

Thanks!

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Wed Jan 09, 2008 12:56 pm

I'm glad that you are trying out this experimental code. The coordinates can be expressed as RELATIVE values (0.0 to 1.0) which should correspond to a linear scale over the plot's data area, or DATA values (which will use the axes to translate the position to Java2D space). There's also an INDEX option, which is intended to allow the annotation to be attached to a data value at a certain index position in the dataset - I don't think that is implemented yet.

I don't recall testing this with the LogAxis, so I'll give that a try.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Filoche
Posts: 26
Joined: Sat Jul 28, 2007 7:23 pm

Re: XYTitleAnnotation coordinate arguments

Post by Filoche » Wed Jan 09, 2008 5:51 pm

mickish wrote:Can you please elaborate on the (x,y) coordinate arguments in the XYTitleAnnotation constructor?

It seems they are in the range [0, 1]. On a standard date domain axis, the x coordinate behaves like a percentage (x=.05 puts the legend 5% away from the left of the chart).

But the interaction with LogAxis is confusing. My legend at y=.2 appears in the middle of my chart, rather than 20% away from the bottom as I expect.

The difference between log axis values is 70 at the bottom, and 1400 at the top. Changes in the XYTitleAnnotation value of y for low values of y affect the position more than the same changes for high values of y.

I guess the y value is used as a percentage of the absolute range values, but I expected it to be a percentage of pixels on the chart.

Would it be reasonable/practical to change the coordinate system to pixels instead of range values?

Thanks!
Hi there.
I worked a bit with that and I didnt find a easy way to place the annotation in a relative way. What I found is the x,y coords must be x,y values where you want your annotation on your serie.

If there's a way to place the annotation at a specific x,y coords, let me know.

Best regards.,
Phil

bruceweertman
Posts: 1
Joined: Fri Jan 03, 2014 7:25 pm
antibot: No, of course not.

Re: Legend Position

Post by bruceweertman » Fri Jan 03, 2014 7:43 pm

Here's a hack that seems to work. If P is the desired fractional distance from the edge of the axis and min is the min of the range and max is the max of the range, then the value to use will be:

min * (10 ^ P log10(max) - 1 ) / (max - min)

So, for example, if P = 0.02, min = 0.01, max = 500 the value to use will be 0.000023 (a very small number!)

I figured this out my looking at
org.jfree.chart.annotations.XYTitleAnnotation

public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea,
ValueAxis domainAxis, ValueAxis rangeAxis,
int rendererIndex, PlotRenderingInfo info)

and

org.jfree.chart.axis.LogAxis.java

public double valueToJava2D(double value, Rectangle2D area,RectangleEdge edge)

It's rather horrible to figure out.

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

Re: Legend Position

Post by paradoxoff » Sat Jan 04, 2014 5:22 pm

This approach should also work:
In the draw-method, replace the calculations of j2DX and j2DY with

Code: Select all

float j2DX = dataArea.getMinX() + this.x*dataArea.getWidth();
float j2DY = dataArea.getMinY() + this.y*dataArea.getHeight();

Locked