Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Last active July 31, 2022 09:51
Show Gist options
  • Save mikeananev/af6b6dcbe886a44de9b5b4736f5060b6 to your computer and use it in GitHub Desktop.
Save mikeananev/af6b6dcbe886a44de9b5b4736f5060b6 to your computer and use it in GitHub Desktop.
clj-xchart update example
(ns org.rssys.xchart
(:gen-class)
(:require [clojure.math :as math]
[com.brunobonacci.mulog :as mulog]
[com.hypirion.clj-xchart :as c])
(:import (org.knowm.xchart XYChart)))
(def chart
^XYChart (c/xy-chart {"Sin" {:x (range 0 63)
:y (range 0 63)
:style {:marker-type :none}}
"Cos" [(range 0 63) (range 0 63)]}))
;; 63 is the number of dots to plot
(def frame (c/view chart))
(dotimes [n 100]
(let [start-x (* 2.0 math/PI (+ (* 0.01 n) n))
x-data (range start-x (* 2.0 math/PI (+ (* 0.01 n) (inc n))) 0.1)
sin-y-data (map math/sin x-data)
cos-y-data (map math/cos x-data)]
(Thread/sleep 28)
(.updateXYSeries chart "Sin" x-data sin-y-data nil)
(.updateXYSeries chart "Cos" x-data cos-y-data nil)
(.revalidate (.getContentPane frame))
(.repaint (.getContentPane frame))))
;;;;;;;;;;;;;;;;;;;;;;
(def chart
^XYChart (c/xy-chart {"Expected rate" [(range 10 20) (range 20 30)]}))
(def frame (c/view chart))
(.updateXYSeries chart "Expected rate" (repeatedly 10 #(rand-int 10)) (range 30 40) nil)
(.revalidate (:contentPane (bean frame)))
(.repaint (:contentPane (bean frame)))
(.revalidate (.getContentPane frame)) ;; the same
(.repaint (.getContentPane frame))
(dotimes [n 100]
(Thread/sleep 100)
(.updateXYSeries chart "Expected rate" (repeatedly 10 #(rand-int 30)) (range 30 40) nil)
(.revalidate (:contentPane (bean frame)))
(.repaint (:contentPane (bean frame))))
(dotimes [n 100]
(Thread/sleep 100)
(.updateXYSeries chart "Expected rate" (repeatedly 10 #(rand-int 30)) (repeatedly 10 #(rand-int 90)) nil)
(.revalidate (.getContentPane frame))
(.repaint (.getContentPane frame)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment