问题描述
我通过 Oz 在 Clojure 中使用 Vega-Lite,所以下面的语法不是严格的 JSON,但希望您能看到语法如何映射到 JSON。
我正在尝试创建一个非常简单的时间序列图,其中 x 轴是以小时为单位的时间,例如“18:00”
有人可以帮忙吗?
(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 可以理解本地时间并使用它进行绘图吗?
解决方法
只需提供以下 :format {:parse {:time "date:'%H:%M'"}}}
,您就会得到以您当地时间绘制的图表。您可以参考以下配置或editor:
{
"$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"
}