I would like to set Y axis as log scale, but it seems JavaFX does not have such possibility: searching on the web I have found suggestions about how to do to set as log scale but the cose is written in a completely different way, I never saw before.

So I am asking if someone can help me to set Y scale as log, below are my simple class to plot data in linear mode

JavaFX provides an impressive chart API that allow to draw a wide range of charts. Unfortunately the only scale that is available for XY charts is a linear implementation. Since the javadoc of the API is sometimes a bit rough it took me some times to get figure out how to get a logarithmic scale. After a discussion on mix.oracle.com it turned out that there where to approaches:

the first one is to transform your data with the logarithmic function before injecting it into the chart and to adjust the formatting of the labels to "simulate" a logarithmic scale. Since I don't really like the idea of manipulating the data to get the expected representation I prefer the second approach:
the second one is to provide your own implementation of a logarithmic Axis. Let's see how to do this:
Extend the ValueAxis class. There are two methods to implement:
public getDisplayPosition(value: java.lang.Object) : Number
will do the mapping between a data value and its visual position. For a log axis, the implementation will look like this:

will define which tick marks should be displayed. The javadoc does not say much about this function. It looks like the main idea is to update the tickMarks sequence of TickMark. The fields of TickMark are weirdly flagged as public-read package, so in oder to instantiate TickMark for our implementation we first need to define a class extending TickMark, let's say CustomTickMark, and to put it in the package javafx.scene.chart.part in order to be able to set the required fields. After that you just need to update the sequence of tick marks with the marks you want to see on your chart, which can look like this:

Well I have never saw methods written in such way updateTickMarks(): Void , it seems a for each statment! And var, where does this come from? I know of int, double, float, String, etc. but never saw var as variable declaration.

I do not know how to set my Y scale using such methods, can someone drop me some lines of code to add log scale to my code?

Thanks!

Susie

05-29-2012, 03:31 PM

DarrylBurke

Re: Log scale for Y axis in XYChart

Quote:

Originally Posted by susieferrari

Well I have never saw methods written in such way updateTickMarks(): Void , it seems a for each statment! And var, where does this come from? I know of int, double, float, String, etc. but never saw var as variable declaration.

That's JavaFX Script, aka JavaFX 1.x (the last version was I think 1.3.1).

Take a bash at converting that to Java code. If I'm reading the API correctly, you will want to override Axis#calculateTickValues(double length, Object range) to return an appropriately populated java.util.List<Axis.TickMark<T>>.

db

05-29-2012, 03:48 PM

susieferrari

Re: Log scale for Y axis in XYChart

Thanks Darryl,

I do not know anything about JavaFX Script: are the method posted of any help in FX 2.0?

I have no idea how to use or converting that methods, can you help me?

Thanks

Susie

05-29-2012, 07:04 PM

DarrylBurke

Re: Log scale for Y axis in XYChart

Quote:

Originally Posted by susieferrari

I do not know anything about JavaFX Script

Search the net; there are tutorials and guides. It's not all that difficult to learn to read the script. You don't need to go in to how to write it.
I don't know FX Script either, but the program statements are more or less self-explanatory.

Quote:

Originally Posted by susieferrari

I have no idea how to use or converting that methods, can you help me?

Here's an approximation, typed in Notepad -- may have typos or be otherwise uncompilable when plugged into a custom Axis subclass, and will need a translation of the getDisplayPosition method.

Code:

// instance field: first check whether Axis already has a protected field you can use.
private List<Axis.TickMark> tickMarks = new ArrayList<Axis.TickMark>();