Permalink
Browse files

started playing with charting stuff. major tech debt incurred.

  • Loading branch information...
1 parent b2d59e4 commit 4e164cc5bdf5da64a91e22b96efbfd8c76a1f75b @daveray committed Mar 22, 2012
View
@@ -311,6 +311,75 @@
(defobject html-editor javafx.scene.web.HTMLEditor [control-options] [])
;*******************************************************************************
+(def axis-options
+ (merge
+ region-options
+ (options-for-class javafx.scene.chart.Axis)))
+
+(defobject category-axis javafx.scene.chart.CategoryAxis [axis-options]
+ [(option-map
+ (default-option :categories
+ ; TODO this sucks
+ (fn [this categories]
+ (.setCategories this
+ (javafx.collections.FXCollections/observableArrayList
+ (map str categories))))
+ (fn [this]
+ (.getCategories this))))])
+
+(defobject value-axis javafx.scene.chart.ValueAxis [axis-options] [])
+(defobject number-axis javafx.scene.chart.NumberAxis [value-axis-options] [])
+
+(def chart-options
+ (merge
+ region-options
+ (options-for-class javafx.scene.chart.Chart)))
+
+(defobject pie-chart javafx.scene.chart.PieChart [chart-options]
+ [(option-map
+ (default-option :data
+ ; TODO this sucks
+ (fn [this data]
+ (.setData this
+ (javafx.collections.FXCollections/observableArrayList
+ (for [[n v] data]
+ (javafx.scene.chart.PieChart$Data. n v)))))
+ (fn [this]
+ (.getData this))))])
+
+(def xy-chart-options
+ (merge
+ chart-options
+ (options-for-class javafx.scene.chart.XYChart)
+ (option-map
+ (default-option :data
+ ; TODO this sucks, omg
+ (fn [this data]
+ (.setData
+ this
+ (javafx.collections.FXCollections/observableArrayList
+ (for [[n series] data]
+ (javafx.scene.chart.XYChart$Series.
+ (name n)
+ (javafx.collections.FXCollections/observableArrayList
+ (for [[x y extra] series]
+ (javafx.scene.chart.XYChart$Data. x y extra))))))))
+ (fn [this] (.getData this))))))
+
+(def line-chart-options
+ (merge
+ xy-chart-options
+ (options-for-class javafx.scene.chart.LineChart)))
+
+(option-provider javafx.scene.chart.LineChart line-chart-options)
+
+(defn line-chart [& {:keys [x-axis y-axis] :as args}]
+ (apply-options
+ (javafx.scene.chart.LineChart. (or x-axis (number-axis))
+ (or y-axis (number-axis)))
+ (dissoc args :x-axis :y-axis)))
+
+;*******************************************************************************
(extend-protocol seesaw.selection/Selection
javafx.scene.control.SingleSelectionModel
(get-selection [this] [(.getSelectedItem this)])
@@ -2,7 +2,7 @@
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
-; which can be found in the file epl-v10.html at the root of this
+; which can be found in the file epl-v10.html at the root of this
; distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
@@ -22,17 +22,18 @@
(gsub #"([A-Z]+)([A-Z][a-z])" "$1-$2")
(gsub #"([a-z]+)([A-Z])" "$1-$2")
(.replace "_" "-")
- (clojure.string/lower-case))))
+ (clojure.string/lower-case))))
(defn- get-option-info [m]
(if (and (= 1 (count (.getParameterTypes m)))
(.matches (.getName m) "^set[A-Z].*"))
(let [base-name (.substring (.getName m) 3)
type (first (.getParameterTypes m))
- dash-name (dash-case base-name)]
+ dash-name (dash-case base-name)
+ boolean? (= Boolean/TYPE type)]
{ :setter (symbol (.getName m))
- :getter (symbol (str "get" base-name))
- :name (keyword (if (= Boolean/TYPE type)
+ :getter (symbol (str (if boolean? "is" "get") base-name))
+ :name (keyword (if boolean?
(str dash-name "?")
dash-name))
:event (if (= javafx.event.EventHandler type)
@@ -46,14 +47,14 @@
.getDeclaredMethods
(remove #(.isSynthetic %))
(filter #(let [ms (.getModifiers %)]
- (= java.lang.reflect.Modifier/PUBLIC
+ (= java.lang.reflect.Modifier/PUBLIC
(bit-and ms
(bit-or java.lang.reflect.Modifier/PUBLIC
java.lang.reflect.Modifier/STATIC)))))))
(defmacro options-for-class [class]
`(option-map
- ~@(for [{:keys [setter getter name event type enum paint]}
+ ~@(for [{:keys [setter getter name event type enum paint]}
(->> (resolve class)
get-public-instance-methods
(map get-option-info)
@@ -72,10 +73,10 @@
enum `(let [set-conv# ~(into {} (for [e enum]
[(keyword (dash-case (.name e)))
(symbol (.getName type) (.name e)) ]))
- get-conv# (clojure.set/map-invert set-conv#)]
+ get-conv# (clojure.set/map-invert set-conv#)]
(default-option
~name
- (fn [c# v#]
+ (fn [c# v#]
(.. c# (~setter (set-conv# v# v#))))
(fn [c#] (get-conv# (.. c# ~getter)))
(keys set-conv#)))
@@ -88,18 +89,25 @@
;*******************************************************************************
; A macro to handle most of the boilerplate for each kind of object
-(defmacro defobject [func-name class base-options extra-options]
- (let [opts-name (symbol (str (name func-name) "-options"))]
- `(do
- (def ~opts-name
+(defmacro defobject [func-name class-or-construct
+ base-options extra-options]
+ (let [opts-name (symbol (str (name func-name) "-options"))
+ class (if (symbol? class-or-construct)
+ class-or-construct
+ (first class-or-construct))
+ args (if (symbol? class-or-construct)
+ []
+ (rest class-or-construct))]
+ `(do
+ (def ~opts-name
(merge
~@base-options
(options-for-class ~class)
~@extra-options))
(option-provider ~class ~opts-name)
- (defn ~func-name
- [& opts#]
- (apply-options (new ~class) opts#)))))
+ (defn ~func-name
+ [& opts#]
+ (apply-options (new ~class ~@args) opts#)))))
@@ -192,6 +192,27 @@
;*******************************************************************************
+(facts "about category-axis"
+ (class (category-axis :categories [1 2 "three" 4])) => javafx.scene.chart.CategoryAxis)
+
+(facts "about number-axis"
+ (class (number-axis :lower-bound 3.0 :upper-bound 25.9)) => javafx.scene.chart.NumberAxis)
+
+(facts "about pie-chart"
+ (class (pie-chart)) => javafx.scene.chart.PieChart
+ (let [pc (pie-chart :legend-side :top
+ :legend-visible? true
+ :data [["Apples" 99] ["Bananas" 100] ["Kiwi" 22]])]
+ [(config pc :legend-side)
+ (config pc :legend-visible?)]) => [:top true])
+
+(facts "about line-chart"
+ (let [xa (number-axis)
+ ya (number-axis)
+ lc (line-chart :x-axis xa :y-axis ya :data {:a [[1 1] [2 2]]})]
+ (class lc) => javafx.scene.chart.LineChart))
+;*******************************************************************************
+
(facts "common node options"
(config (button :id :this-is-the-id) :id) => :this-is-the-id
(config (button :class :one-class) :class) => #{:one-class}
@@ -0,0 +1,39 @@
+; Copyright (c) Dave Ray, 2012. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns upshot.test.examples.line-chart
+ (:use [upshot.core]))
+
+(def data
+ {:series-a [[0 0] [1 1] [2 4] [3 9]]
+ :series-b [[0 9] [1 4] [2 1] [3 0]]})
+
+(defn make-scene []
+ (scene
+ :root (border-pane
+ :center (line-chart
+ :id :chart
+ :data data))
+ :width 800.0
+ :height 600.0
+ :fill :black))
+
+(defn add-behaviors [root]
+ root)
+
+(defn run []
+ (run-now
+ (-> (stage :scene (make-scene))
+ add-behaviors
+ .show)))
+(defn -main [& args]
+ (run))
+;(run)
+
@@ -0,0 +1,32 @@
+; Copyright (c) Dave Ray, 2012. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns upshot.test.examples.pie-chart
+ (:use [upshot.core]))
+
+(defn make-scene []
+ (scene
+ :root (border-pane
+ :center (pie-chart :id :chart
+ :data [["Apples" 99]
+ ["Bananas" 100]
+ ["Kiwi" 22]]))
+ :width 800.0
+ :height 600.0
+ :fill :black))
+
+(defn run []
+ (run-now
+ (-> (stage :scene (make-scene))
+ .show)))
+(defn -main [& args]
+ (run))
+;(run)
+

0 comments on commit 4e164cc

Please sign in to comment.