简体   繁体   English

在 Vega-lite 的工具提示中将时间戳格式化为日期

[英]Format timestamp as a date in the tooltip in Vega-lite

I have this chart that takes data from a CSV, pivots it by one of the column and displays several timeseries with lines.我有这张图表,它从 CSV 中获取数据,将其以其中一列为轴心,并用线条显示多个时间序列。 Also makes a tooltip taking the names from the pivot column automatically.还使工具提示自动从 pivot 列中获取名称。

带有工具提示和多条股票行的股票图表

The only problem is that the date/time appears as a timestamp.唯一的问题是日期/时间显示为时间戳。 Is there a way to format it to look nicer, like an actual date + time, the same way it appears in the X axis?有没有办法让它看起来更好看,就像实际的日期+时间一样,就像它在 X 轴上的显示方式一样?

I know I could specify all the values for the tooltip doing something like我知道我可以指定工具提示的所有值,比如

        "tooltip": [
    {
        "field": "date",
        "type": "temporal",
        "title": "date",
        "timeUnit": "utcyearmonthdatehoursminutes"
    },
    {...}
]

But I want to be able to keep using "mark": {"type": "rule", "tooltip": {"content": "data"}}, to be able to get the tooltip fields dynamically, and if I add the code above only the date appears in the tooltip.但我希望能够继续使用"mark": {"type": "rule", "tooltip": {"content": "data"}},以便能够动态获取工具提示字段,如果我添加上面的代码,只有日期出现在工具提示中。

Here's the full code:这是完整的代码:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/stocks.csv"},
  "width": 400,
  "height": 300,
  "encoding": {"x": {"field": "date", "type": "temporal"}},
  "layer": [
    {
      "encoding": {
        "color": {"field": "symbol", "type": "nominal"},
        "y": {"field": "price", "type": "quantitative"}
      },
      "layer": [
        {"mark": "line"},
        {"transform": [{"filter": {"param": "hover", "empty": false}}], "mark": "point"}
      ]
    },
    {
      "transform": [{"pivot": "symbol", "value": "price", "groupby": ["date"]}],
      "mark": {"type": "rule", "tooltip": {"content": "data"}},
      "encoding": {
        "opacity": {
          "condition": {"value": 0.3, "param": "hover", "empty": false},
          "value": 0
        }
      },
      "params": [{
        "name": "hover",
        "select": {
          "type": "point",
          "fields": ["date"],
          "nearest": true,
          "on": "mouseover",
          "clear": "mouseout"
        }
      }]
    }
  ]
}

You can format the date field in calculated transform and assign new field, which will then be displayed in your tooltip.您可以在计算转换中格式化日期字段并分配新字段,然后将显示在您的工具提示中。 Just add the below transform code or check the editor .只需添加以下转换代码或检查编辑器 For more format or date expressions refer, https://vega.github.io/vega/docs/expressions有关更多格式或日期表达式,请参阅https://vega.github.io/vega/docs/expressions

"transform": [
        {"pivot": "symbol", "value": "price", "groupby": ["date"]},
        {"calculate": "datetime(datum.date)", "as": "convertedDate"},
        {
          "calculate": "utcFormat(datum.date,'%B %d, %Y')",
          "as": "converted as string"
        }
      ],

If you want to update the existing date field, then just provide the date field in as config as done below:如果要更新现有的日期字段,则只需在配置中提供日期字段, as所示:

 {"calculate": "datetime(datum.date)", "as": "date"},

I had the same issue and I resolved it in the following way:我有同样的问题,我通过以下方式解决了它:

  • Deleted "tooltip" from "mark" and put it into "encoding":从“标记”中删除“工具提示”并将其放入“编码”:

    "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "width": "container", "data": { "values": {{dataViz2 | “$模式”:“https://vega.github.io/schema/vega-lite/v5.json”,“宽度”:“容器”,“数据”:{“值”:{{dataViz2 | safe }} },安全的 }} },
    "mark": { "type":"line", “标记”:{ “类型”:“行”,
    "point": {"filled": false,"fill": "white"} }, “点”:{“填充”:假,“填充”:“白色”}},
    "encoding": { "x": {"field": "date", "type": "temporal", "axis": {"format": "%b %d "}}, "y": {"field": "total", "type": "quantitative"}, "tooltip": [ {"field": "date", "type": "temporal", "format": "%b %d %Y"}, {"field": "total", "type": "quantitative", "format": ",d" } ] “编码”:{“x”:{“字段”:“日期”,“类型”:“时间”,“轴”:{“格式”:“%b %d”}},“y”:{“ field": "total", "type": "quantitative"}, "tooltip": [ {"field": "date", "type": "temporal", "format": "%b %d %Y" }, {"field": "total", "type": "quantitative", "format": ",d" } ]
    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM