Macca Blog

My life experiences with code and design

JFXGraph code sample

Posted on by Mark

G’Day.

In this post, I will demonstrate with code samples, just how a developer would use the JFXGraph application.

Note:
I have made a small update to the application that I posted about in the previous post, which will allow you to add your own bars to the graph, and, by double-clicking on a bar, will remove the bar from the graph.

Here is a screen shot of the current version (0.02) of the JFXGraph app:

As you can see, there is a small input control at the top left, and it has some small input validation, i.e. value must be a non-negative integer, etc…

The Code:
The code sample to get a basic graph with no columns is as shown below:

import javafx.scene.Scene;
import javafx.stage.Stage;
import jfxgraph.graphs.bar.BarGraph;

var graph = BarGraph {
xPosition: 250,
yPosition: 500,
width: 400
};

Stage {
title: "JavaFX Graph"
width: 800
height: 600
scene: Scene {
content: [graph];
}
}

This would render the following:

To add some columns, just do the following:

import javafx.scene.Scene;
import javafx.stage.Stage;
import jfxgraph.graphs.bar.BarGraph;
import jfxgraph.graphs.bar.BarGraphColumn;

var graph = BarGraph {
xPosition: 250,
yPosition: 500,
width: 400
columns: [
BarGraphColumn{ tag: "Example1" value: 500 },
BarGraphColumn{ tag: "Example2" value: 300 },
BarGraphColumn{ tag: "Example3" value: 900 }
]

};

Stage {
title: "JavaFX Graph"
width: 800
height: 600
scene: Scene {
content: [graph];
}
}

Which renders:

Pretty simple huh?

I love feedback, good and bad, so please if you have any comments or suggestions, please let me know.

Here is the JNLP link as usual:

This entry was posted in Java, JavaFX, JFXGraph. Bookmark the permalink.

3 Responses to JFXGraph code sample

  1. MedSabri

    in order to run this example the 2 library :
    - jfxgraph.graphs.bar.BarGraph;
    - jfxgraph.graphs.bar.BarGraphColumn;
    does not exist please Post them.
    it’s a good example to illustrate the power of JavaFX.

    Thank a lot.

  2. Mark

    Hello MedSabri, clicking the Launch button does not work for you? Can you please tell me what errors you get etc..? thanks.

  3. Mark

    Simply running the code shown by itself will not work. This is because it is just an example of the JFXGraph project.

    Currently I am working with a few other developers on a JavaFX project called JFXtras that is designed to allow people to work with the graphing side of things (such as this JFXGraph application).

    Hopefully it will be ready for use very soon, I will be posting the details of this project when its ready… but for now, here is a link to the google groups page:

    JFXtras Google Group

Leave a Comment

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>