Skip to content

Commit

Permalink
Renamed transpose? to transpose-data? and label? to label-points?
Browse files Browse the repository at this point in the history
  • Loading branch information
liebke committed Jan 10, 2011
1 parent c8717d2 commit 53b835d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
y1 (map #(Math/cos %) x)
y2 (map #(Math/sin %) x)]
(-> (xy-plot :minx -5 :xmax 5 :ymin -1.5 :maxy 1.5)
(add-points [x y1] :transpose? true)
(add-points [x y2] :transpose? true
(add-points [x y1] :transpose-data?? true)
(add-points [x y2] :transpose-data?? true
:color (rgb 255 0 0))))))
</code></pre>

Expand Down
2 changes: 1 addition & 1 deletion images/rand-plot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ <h3>Plotting Sine and Cosine</h3>
(-> (xy-plot :width 450 :height 200
:xmin -5 :xmax 5
:ymin -1.5 :ymax 1.5)
(add-points [x y1] :transpose? true
(add-points [x y1]
:transpose-data?? true
:size 1)
(add-points [x y2] :transpose? true
(add-points [x y2]
:transpose-data?? true
:size 1
:fill (rgb 255 0 0))))))
</code></pre>
Expand All @@ -103,8 +105,9 @@ <h3>Plotting Random Data</h3>
y (repeatedly 25 #(rand-int 100))]
(spit (str directory "/rand-plot.svg")
(emit-svg
(-> (xy-plot :width 500 :height 500 :label? true)
(add-points [x y] :transpose? true)))))
(-> (xy-plot :width 500 :height 500
:label-points? true)
(add-points [x y] :transpose-data?? true)))))
</code></pre>
</div>

Expand Down
19 changes: 7 additions & 12 deletions src/analemma/charts.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(ns analemma.charts
(:use [analemma.svg :only [rect style line text group
translate circle rgb svg]]
translate circle rgb svg
translate-value]]
[analemma.xml :only [emit]]))

(def default-chart-props {:x 50, :y 50,
:height 500, :width 750,
:xmin 0, :xmax 100,
:ymin 0, :ymax 100,
:grid-lines 10,
:label? false,
:label-points?? false,
:points []
:major-grid-color (rgb 255 255 255)
:minor-grid-color (rgb 245 245 245)
Expand All @@ -22,12 +23,6 @@
:label-font-size "10px"
:label-number-format "%.1f"})

(defn translate-value [v from-min from-max to-min to-max]
(let [scale (/ (- to-max to-min)
(- from-max from-min))
trans (- to-min (* from-min scale))]
(float (+ (* v scale) trans))))

(defn chart-background [{:keys [height width background-color
major-grid-color major-grid-width]}]
(-> (rect 0 0 height width)
Expand Down Expand Up @@ -117,14 +112,14 @@
{:keys [height width
xmin xmax
ymin ymax
label?]} props
label-points??]} props
x* (translate-value x xmin xmax 0 width)
y* (- height (translate-value y ymin ymax 0 height))
point (apply circle x* y* r options)
label (point-label props x* y* x y r)]
(-> chart
(update-in [:points] (fn [old] (conj old (apply assoc {:x x, :y y, :r r} options))))
(assoc :svg (concat (:svg chart) (if label? [point label] [point]))))))
(assoc :svg (concat (:svg chart) (if label-points?? [point label] [point]))))))

(defn points->xy [points]
(reduce (fn [[x y] [p1 p2]] [(conj x p1) (conj y p2)])
Expand All @@ -133,8 +128,8 @@
(defn xy->points [[x y]]
(map (fn [p1 p2] [p1 p2]) x y))

(defn add-points [chart data & {:keys [size sizes colors transpose? fill]}]
(let [[x y] (if transpose? data (points->xy data))
(defn add-points [chart data & {:keys [size sizes colors transpose-data?? fill]}]
(let [[x y] (if transpose-data?? data (points->xy data))
sizes (or sizes (repeat (count x) (or size 3)))
colors (or colors (repeat (count x) (or fill (rgb 0 0 255))))
data (map (fn [x y r color] [x y r color]) x y sizes colors)]
Expand Down
13 changes: 13 additions & 0 deletions src/analemma/svg.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns analemma.svg
(:require [analemma.xml :as xml]))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SVG FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -122,3 +123,15 @@
(defn image [href & options]
(let [attrs (apply hash-map options)]
[:image (merge {"xlink:href" href} attrs)]))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UTILITY FUNCTIONS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn translate-value [v from-min from-max to-min to-max]
(let [scale (/ (- to-max to-min)
(- from-max from-min))
trans (- to-min (* from-min scale))]
(float (+ (* v scale) trans))))

8 changes: 4 additions & 4 deletions src/examples/charts.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
y (repeatedly 25 #(rand-int 100))]
(spit (str directory "/rand-plot.svg")
(emit-svg
(-> (xy-plot :width 500 :height 500 :label? true)
(add-points [x y] :transpose? true))))))
(-> (xy-plot :width 500 :height 500 :label-points? true)
(add-points [x y] :transpose-data?? true))))))

(defn sin-cos-plot [directory]
(let [x (range -5 5 0.05)
Expand All @@ -23,9 +23,9 @@
(-> (xy-plot :width 450 :height 200
:xmin -5 :xmax 5
:ymin -1.5 :ymax 1.5)
(add-points [x y1] :transpose? true
(add-points [x y1] :transpose-data?? true
:size 1)
(add-points [x y2] :transpose? true
(add-points [x y2] :transpose-data?? true
:size 1
:fill (rgb 255 0 0)))))))

Expand Down

0 comments on commit 53b835d

Please sign in to comment.