Contact Us

Contact Details

JavaFX Charting API - Business Functionality at Last

Submitted by Mark Clarke on Mon, 08/10/2009 - 14:30

This weekend I dived back into JavaFX again. JavaFX 1.2 has been out for a while now, and I have wanted to try out JavaFX again, ever since I took 1.0 for a spin about a year ago. At that time there was no support for JavaFX on Linux. This, combined with the fact that it JavaFX had no UI controls for data input, meant that I had no practical use for it since I am not really a visual designer but more of an application developer. Luckily now ,JavaFX runs officially on Linux, and whats more 1.2 introduced the first version of the charting API, somethings that worth looking at!

Flex Charting only option before

Charting is one of those things, like Internationalization that gets left to the end. Unlike internationalization,optimization and security :), it usually pays big time to do charting when it comes to convincing the CFO of the customer to pay you :) Its the tip of the iceberg stuff which they see and usually, sadly, rate the whole system on. Until now, Flex has been the only real option available, for quickly generating some eye-candy and maybe adding a zero on to the invoice :)

The big minus for me with Flex, besides not being completely open source, was the lack of available tools to develop Flex apps on Linux. Coding mxml and action script by hand, without code completion and syntax highlighting, made it a difficult task. With JavaFX both Netbeans and Eclipse have great, and improving, tool support. This makes learning the language so much easier and its much quicker to produce usable code. I just hope that soon its easy to integrate JavaFX with our Maven build scripts and processes.

JavaFX Charting API Demo

In the demo below I make use of a 3D pie chart, a line chart and a bar chart. The basic demo is to demonstrates some "drill down" capabilities. I.E one can drill down from the pie chart to the line chart and then the bar chart.

To see the different charts:

click on a slice in the pie chart,

a data point in the line chart and

a bar in the bar chart.

Sorry for the lack of visual clues, like the cursor changing when hovering over the relevant graphic but I couldn't figure out how to change the cursor to a hand just for the data points, all my effort resulted in the cursor changing to a hand anywhere on the graph. It is possible however to have different chart generated for different data points as the Data objects have an action function associated for each.

Charting Demo

JavaFX Charting API still requires more work

The JavaFX charting api still requires some work. The line chart does not have a convenient DateAxisValue which is used quiet often. Also you need to stipulate the min and max values for the charts yourself for the value axis. If you don't the graphs don't show. It would be better if the framework worked out some reasonable defaults for you. This is one of the short-comings of JavaFX the language. You cannot stipulate mandaorty fields that need to be initialised before the object can be used. A user needs to consult the documentation, or just have to quess, as I didn't find this in the API docs.

Animations and transitions are real easy to do in JavaFX, the transitions available at present aren't as impressive as with Flex but I am sure these will come. In the demo I make use of a simple fade transition to transission between graphs.

JavaFX not Ready for Use

While creating this demo application I initially got carried away and took a first stab at writing a model,view updater framework to allow the new UI controls to read and write their textual representation to and from their backing objects. This for the first time made me have to deal with JavaFX as a language rather than its easy to use declarative format with which one can do so much nifty graphical stuff. I soon found out that JavaFX is far from ready for use in any kind of production environment. I hit 5 bugs, 2 show-stopper ones, at least for me, in 16 hours. The bugs were:

the chart api throws an error whenever it repaints itself,this seemed to have no negative effect (minor),

using the lookup function on the Scene object to retrieve node references, kind of like getNodeById from javascript, returned null for any object that extends CustomNode, (critical for me, can work around it but the code is ugly - major.)

the init and postinit blocks in a class that extends CustomNode appear to be ignored. Any variable initialised with them are null after the code blocks execute (major),

there appears to be a problem using "bind with inverse" in some cases (major)

binding the height and width attributes of a chart meant that the gradient fill stopped working (minor).

JavaFX the Language - Please Don't Let Simplicty Kill It

Beside the bugs above, I found that the reflection API is hard to understand. There is no documentation on how to use it. It seems almost impossible to get the class object of an instantiated object, and have it be past around like any normal object. You can create an object using the API , if you know the string name for the class,but if you get given an object at runtime and want to determine its class it seems impossible. I hope this is because JavaFX is still new and not an attempt to simplify the language for visual designers.

I hope that in trying to make JavaFX a "simple" scripting language we don't loose out on some powerful language features that we need and expect as Java developers. I eventually stopped building the more complex JavaFX example and settled for the more simple one presented.

Comments

You should look at yous browser.jnlp file. I have that error in FF
java.io.FileNotFoundException: JNLP file error: http://www.jumpingbean.co.za/files/JavaFXApplication2_browser.jnlp. Please make sure the file exists and check if "codebase" and "href" in the JNLP file are correct.
at sun.plugin2.applet.JNLP2Manager.loadJarFiles(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

To me that means that href, or codebase properties are not set propely, maybe You post them to standard NetBeans Localhost serlet?

thanks for the feedback. I have uploaded the files. Let me know if it works because I had a look at the config files and it has some file references in them. The applet works my side without these so its difficult to test with them.thanks

Cheched at the office and it works on the Ubuntu machines. Tried on someone's windows machine at a client and it seemed to have problems, wasn't sure if it was the firewall. Get as far as the spinning Java logo. Oh well. Guess I got to learn a bit more about deploying JavaFX applets. I hope that most get a chance to see the demo. You will need the latest version of Java installed though as it uses the cutting edge release of JavaFX.

Its actually a feature, but your feedback is valuable insight that the semantics might be a touch too obscure. Enjoyed this blog post, now if only we could get our applets to not "flicker" on Safari...