如何获取对象数组的分位数

时间:2015-07-24 14:06:45

标签: javascript d3.js

我有一个名为(def providers [{:name "Steam" :url "http://steamcommunity.com/openid"} {:name "Yahoo" :url "http://me.yahoo.com/"}]) (defroutes home-routes (GET "/" req (h/html5 pretty-head (pretty-body ; (github-link req) [:h2 "Authenticating with various services using OpenID"] [:h3 "Current Status " [:small "(this will change when you log in/out)"]] (if-let [auth (friend/current-authentication req)] [:p "Some information delivered by your OpenID provider:" [:ul (for [[k v] auth :let [[k v] (if (= :identity k) ["Your OpenID identity" (str (subs v 0 (* (count v) 2/3)) "…")] [k v])]] [:li [:strong (str (name k) ": ")] v])]] [:div [:h3 "Login with…"] (for [{:keys [name url]} providers :let [base-login-url (context-uri req (str "/login?identifier=" url)) dom-id (str (gensym))]] [:form {:method "POST" :action (context-uri req "login") :onsubmit (when (.contains ^String url "username") (format "var input = document.getElementById(%s); input.value = input.value.replace('username', prompt('What is your %s username?')); return true;" (str \' dom-id \') name))} [:input {:type "hidden" :name "identifier" :value url :id dom-id}] [:input {:type "submit" :class "button" :value name}]]) [:p "…or, with a user-provided OpenID URL:"] [:form {:method "POST" :action (context-uri req "login")} [:input {:type "text" :name "identifier" :style "width:250px;"}] [:input {:type "submit" :class "button" :value "Login"}]]]) [:h3 "Logging out"] [:p [:a {:href (context-uri req "logout")} "Click here to log out"] "."]))) (GET "/logout" req (friend/logout* (resp/redirect (str (:context req) "/"))))) (def page (friend/authenticate home-routes {:allow-anon? true :default-landing-uri "/" :workflows [(openid/workflow :openid-uri "/login" :credential-fn identity)]})) 的对象数组,其中一个对象是这样的:

dataArray

现在我希望使用d3获得此数组的较低分位数。我想到了这样的事情:

{
   id,
   x,
   y
}

但遗憾的是此方法没有访问者选项。如何从对象数组中获取属性的分位数?提前致谢

1 个答案:

答案 0 :(得分:3)

我找到了解决问题的方法。我需要得到一个只包含一组对象的某个属性的数组,所以我不需要在分位数方法中使用一个存取器。

// get an array of just the y property values
var keyArray = dataArray.map(function(item) { return item["y"]; });

// calculate a lower quantile of this array
var quantile_y = d3.quantile(keyArray, 0.25);

<强>更新

为了正常工作,必须对赋予d3.quantile()的数组进行排序。 e.g。

keyArray.sort(d3.ascending);