Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duration scale is missing #4

Open
masterbuilder99 opened this issue Nov 6, 2021 · 5 comments
Open

duration scale is missing #4

masterbuilder99 opened this issue Nov 6, 2021 · 5 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@masterbuilder99
Copy link

Hi @generateme

I am using wadogo to generate svg images in trateg. I supply price/time domain,
and then via wadogo I scale them to pixels. This is the code:
https://github.com/clojure-quant/trateg/blob/master/profiles/demo/src/demo/lib/svg.clj

 (show!
   (svg-view {:min-px 500
              :max-px 1000
              :min-dt (parse-date "2021-01-01")
              :max-dt (parse-date "2021-12-31")
              :svg-width 500
              :svg-height 500}
             [[:point {:color "red"}   [(parse-date "2021-04-01") 600]]
              [:point {:color "blue"}  [(parse-date "2021-05-01") 700]]
              [:point {:color "green"} [(parse-date "2021-09-01") 900]]
              [:point {:color "blue"}  [(parse-date "2021-03-01") 600]]
              [:line {:color "green"}
               [(parse-date "2021-03-01") 500]
               [(parse-date "2021-12-01") 900]]
              [:series {:color "black"}
               [[(parse-date "2021-01-01") 600]
                [(parse-date "2021-02-01") 500]
                [(parse-date "2021-03-01") 400]
                [(parse-date "2021-04-01") 300]
                [(parse-date "2021-05-01") 600]
                [(parse-date "2021-06-01") 700]
                [(parse-date "2021-07-01") 800]
                [(parse-date "2021-08-01") 900]
                [(parse-date "2021-09-01") 700]
                [(parse-date "2021-10-01") 600]
                [(parse-date "2021-11-01") 500]
                [(parse-date "2021-12-01") 600]]]
              [:ellipse {:cx (parse-date "2021-03-01")
                         :rx (parse-date "2021-03-01")
                         :cy 700
                         :ry 400}]]))

Scaling works for :point, :line :series. But it fails for :ellipse. And the reason is that The radius of an ellipse
needs to be scaled via a DURATION, not via a date. I will for now specify a duration via two dates, and then
calculate the radius this way. But if I want to follow svg convention in the svg rendering, then it would be
very great to have durations.

I use this code to create durations from dates. at and bt are localdate. The duration I can easily add/remove
from a date, I can also multiply or divide it.

(defn make-root-box [{:keys [ap bp at bt] :as box}]
  (let [ap (Math/log10 ap)
        bp (Math/log10 bp)
        interval (t.i/new-interval at bt)]
    (assoc box
           :ap ap
           :bp bp
           :zoom 1
           :idx-p 0
           :idx-t 0
           :dp (- bp ap)
           :dt (tick/duration interval))))
@masterbuilder99
Copy link
Author

Here is my hack:

(defn scale-ellipse [{:keys [px-scale time-scale] :as plot-ctx}
                     [{:keys [rx ry cx cy] :as opts}]]
  (let [; price
        y* (px-scale (+ ry cy))
        cy (px-scale cy)
        ;ry 70
        ry (- cy y* ) ; y is an inverse scale.
        ; time
        x* (time-scale (tick/>> cx rx))
        cx (time-scale cx)
        ;rx 70 ; (time-scale rx)
        rx (- x* cx) ; x is NOT an inverse scale
        ]
    (println "cy (scaled): " cy "y* (scaled): " y*  "ry (scaled): " ry)
    (println "cx (scaled): " cx "x* (scaled): " x*  "rx (scaled): " rx)
    (ellipse
     (merge opts {:rx rx :ry ry :cx cx :cy cy}))))

@masterbuilder99
Copy link
Author

I hope this examples make it clear that a duration scale is missing :-) It somehow corresponds to the time scale, but
not exactly.

@genmeblog
Copy link
Member

Oh! I missed this issue. Thanks for examples I'll be back to this soon.

@genmeblog
Copy link
Member

I'm trying to figure out how to create duration scale and not really understand how it should work.

Is it something like that?

(require '[wadogo.scale :as s]
         '[java-time :as dt])

(def duration1 (dt/duration 234235345))
(def duration2 (dt/duration 665543))

duration1
;; => #object[java.time.Duration 0x33b3f47a "PT65H3M55.345S"]
duration2
;; => #object[java.time.Duration 0x6594cb99 "PT11M5.543S"]

(def linear-scale (s/scale :linear {:domain [(dt/value (dt/property duration1 :seconds))
                                           (dt/value (dt/property duration2 :seconds))]
                                  :range [100 500]}))

(defn duration-scaler
  [duration]
  (let [seconds (dt/value (dt/property duration :seconds))]
    (linear-scale seconds)))

(duration-scaler (dt/duration 4433222))
;; => 493.5471164961254

@genmeblog
Copy link
Member

There are two temporal amounts: Period (years) and Duration (minutes). I see two additional scales. To solve is how ticks are represented and formatted. There rest is easily doable.

@genmeblog genmeblog added enhancement New feature or request good first issue Good for newcomers labels Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants