by lkorasik
A fluent interface for working with gnuplot from Kotlin.
- Install gnuplot on your system
- Add a dependency
Currently, Kotlin 1.9 + JVM 21 is supported. (The artifact is not yet posted in maven central)
<dependency>
<groupId>com.lkorasik.gnukt</groupId>
<artifactId>core</artifactId>
<version>0.1</version>
</dependency>
Скрипт:
val configuration = GnuktConfiguration("gnuplot")
val script = gnuktScript {
title("sin \\& cos") // Plot title
val sin = function { // Declare function
name("f")
arguments("x")
expression("sin(x)")
}
val cos = function {
name("g")
arguments("x")
expression("cos(x)")
}
xLabel("x") // Set x-axis label
yLabel("y") // Set y-axis label
xRange(-5.0, 5.0) // Set x-axis range
plotFunction(sin, cos) // Plot functions
pause() // Wait for close
}
Gnukt(configuration).run(script)
Equivalent script:
gnuktScript {
title("sin \\& cos")
val sin = function {
name("f")
arguments("x")
expression("sin(x)")
}
val cos = function {
name("g")
arguments("x")
expression("cos(x)")
}
xLabel("x")
yLabel("y")
xRange(-5.0, 5.0)
plotFunction(sin, cos)
pause()
}.execute(gnuplotPath = "gnuplot")
You can also replace svg with any other terminal that is supported by gnuplot.
gnuktScript {
// ...
val terminal = terminal {
terminal("svg")
size {
width(400)
height(400)
}
}
terminal(terminal)
output("out.svg")
// ...
}
gnuktScript {
// ...
val file1 = file {
file("example.dat")
xColumn(1)
yColumn(3)
}
plotFile(file1)
// ...
}
gnuktScript {
// ...
}.save("script.gnuplot")
gnuktScript {
// ...
rawCommand("set key left box")
// ...
}
not appear?
Perhaps you forgot to add pause()
at the end.
gnuktScript {
// ...
pause()
}
We welcome contributions. Please check the issue tracker. If you see something you wish to work on, please either comment on the issue, or just send a pull request. Want to work on something else, then just open a issue, and we can discuss! We appreciate documentation improvements, code cleanup, or new features. Please be mindful that all work is done on a volunteer basis, thus we can be slow to reply.