Last active
February 3, 2026 20:24
-
-
Save dacr/3eea49c3da321a96d76ad388cbabffc9 to your computer and use it in GitHub Desktop.
smile visualization line charts / published by https://github.com/dacr/code-examples-manager #2ac85e0c-419d-4165-8491-9e5fc5367eba/a7fc93ce0b61155bc55cc76de09c52d263827ef1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : smile visualization line charts | |
| // keywords : smile, chart, visualization | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 2ac85e0c-419d-4165-8491-9e5fc5367eba | |
| // created-on : 2021-03-05T09:23:01Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "com.github.haifengl::smile-scala:3.1.1" | |
| // --------------------- | |
| import smile.plot.swing.* | |
| import smile.plot.show | |
| import java.awt.Color | |
| import scala.math.* | |
| // examples coming from http://haifengl.github.io/visualization.html | |
| implicit val renderer:Canvas=>Unit = JWindow.apply | |
| // -------------------------------------------------------------------- | |
| def interval(from:Double, to:Double, count:Int):LazyList[Double] = { | |
| val step = (to-from)/count | |
| LazyList.iterate(from)(step.+).takeWhile(_ <= to) | |
| } | |
| // -------------------------------------------------------------------- | |
| { | |
| val heart = -314 to 314 map { i => | |
| val t = i / 100.0 | |
| val x = 16 * pow(sin(t), 3) | |
| val y = 13 * cos(t) - 5 * cos(2*t) - 2 * cos(3*t) - cos(4*t) | |
| Array(x, y) | |
| } | |
| show(line(heart.toArray, color = Color.RED)) | |
| } | |
| // -------------------------------------------------------------------- | |
| { | |
| val gauss = interval(-5, 5, 1000).map { x => | |
| val y = exp(-pow(x, 2)) | |
| Array(x, y) | |
| } | |
| show(line(gauss.toArray, color = Color.BLUE)) | |
| } | |
| // -------------------------------------------------------------------- | |
| println("enter to exit"); scala.io.StdIn.readLine() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment