首页 > 解决方案 > Vega-Lite (Clojure Oz) 解析本地时间

问题描述

我通过Oz在 Clojure 中使用 Vega-Lite,因此下面的语法不是严格的 JSON,但希望您能看到语法如何映射到 JSON。

我正在尝试创建一个非常简单的时间序列图,其中 x 轴是以小时为单位的时间,例如“18:00”<--当地时间下午 6:00。到目前为止,如果我使用解析器,我只能让 Vega Lite 识别时间。以下代码有效,但它输出到 UTC 时间而不是本地时间。我不知道如何让 Vega-Lite 将其解析为当地时间。

有人可以帮忙吗?

(def test-plot
  {:data {:values
          [{:time "18:00" :volume 10}
           {:time "18:02" :volume 41}
           {:time "18:07" :volume 192}
           {:time "18:30" :volume 257}
           {:time "19:00" :volume 300}]
          :format {:parse {:time "utc:'%H:%M'"}}}
   :encoding {:x {:field "time" :type "temporal" :timeUnit "hoursminutes"}
              :y {:field "volume" :type "quantitative"}}
   :mark "point"})

;;; to compile and view in Clojure - Oz:
(do
  (println "calling (oz/start-server!)")
  (oz/start-server!)

  (println "calling (oz/view!)")
  (oz/view! test-plot)

  (println "calling (Thread/sleep)")
  (Thread/sleep 5000))

如果我使用:format {:parse {:time "utc:'%H:%M'"}}}然后我得到下面的图像,其中 Vega-Lite 正确解析时间,然后转换为 UTC 时间。 在此处输入图像描述

但是,如果我尝试删除utc,例如:format {:parse {:time "'%H:%M'"}}},那么我会得到这个 Vega-Lite 不再正确解析时间的图像。 在此处输入图像描述

有人可以帮我弄清楚如何让 Vega-Lite 正确解析时间和/或如何表示时间,{:data :{values...}}以便 Vega-Lite 可以理解当地时间并用它绘图吗?

标签: plotclojurevega-litevega

解决方案


只需提供以下:format {:parse {:time "date:'%H:%M'"}}}信息,您将获得在您当地时间绘制的图表。您可以参考以下配置或编辑器

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"time": "18:00", "volume": 10},
      {"time": "18:02", "volume": 41},
      {"time": "18:07", "volume": 192},
      {"time": "18:30", "volume": 257},
      {"time": "19:00", "volume": 300}
    ],
    "format": {"parse": {"time": "date:'%H:%M'"}}
  },
  "encoding": {
    "x": {"field": "time", "type": "temporal", "timeUnit": "hoursminutes"},
    "y": {"field": "volume", "type": "quantitative"}
  },
  "mark": "point"
}

推荐阅读