JXGraph allows you to plot functions on a graph. JXGraph in Java takes a Plot (usually an anonymous function) to plot a function. Because anonymous classes are not possible in Groovy, a new class, GroovyPlot, has been created that can take a closure and generate a Plot.

The graph node in SwingXBuilder adds a property *plots* that takes a multi-dimensional List as a parameter. The internal list has a Color component and a closure representing the plot. Here is an example of code plotting sin x :

Code Block |
---|

def swing = new SwingXBuilder() def frame = swing.frame(size:[300,300]) { graph(plots:[[Color.GREEN,{value -> Math.sin(value)}]]) }.show() |

And here is the corresponding graph:

To graph, for instance, the cosine as well as the sine, the graph node signature would be:

Code Block |
---|

graph(plots:[ [Color.GREEN, {value -> Math.sin(value)}], [Color.BLUE, { value -> Math.cos(value)}] ]) |