Is it possible to display vega/vega-lite bar based on period time column?

谁说我不能喝 提交于 2021-01-29 05:52:54

问题


I was wonder if it is possible to create a vega/vega-lite visualization , based on time periodic columns. For example , assume that i would like to display a graph of athletics results in a bar/other visualization.

rio-2016-athletics

I have tried to make some changes to simple bar chart bellow. I have edited only the "amount" column in order to display time-period results:

 "data": [
    {
      "name": "table",
      "values": [
        {"category": "1", "amount": 1:42.15},
        {"category": "2", "amount": 1:42.61},
        {"category": "3", "amount": 1:42.93},
        {"category": "4", "amount": 1:43.41}
      ]
    }
  ]

I got the following error message: [Error] Unexpected token : in JSON at position 208 and line 12

Can one advise how to display time periodic column IN THE FORMAT THAT I AM LOOKING FOR (mm:ss.ms)

The original code was:

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "width": 400,
  "height": 200,
  "padding": 5,

  "data": [
    {
      "name": "table",
      "values": [
        {"category": "1", "amount": 42.15},
        {"category": "2", "amount": 42.61},
        {"category": "3", "amount": 42.93},
        {"category": "4", "amount": 43.41}
      ]
    }
  ],

  "signals": [
    {
      "name": "tooltip",
      "value": {},
      "on": [
        {"events": "rect:mouseover", "update": "datum"},
        {"events": "rect:mouseout",  "update": "{}"}
      ]
    }
  ],

  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "table", "field": "category"},
      "range": "width",
      "padding": 0.05,
      "round": true
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "amount"},
      "nice": true,
      "range": "height"
    }
  ],

  "axes": [
    { "orient": "bottom", "scale": "xscale" },
    { "orient": "left", "scale": "yscale" }
  ],

  "marks": [
    {
      "type": "rect",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {
          "fill": {"value": "steelblue"}
        },
        "hover": {
          "fill": {"value": "red"}
        }
      }
    },
    {
      "type": "text",
      "encode": {
        "enter": {
          "align": {"value": "center"},
          "baseline": {"value": "bottom"},
          "fill": {"value": "#333"}
        },
        "update": {
          "x": {"scale": "xscale", "signal": "tooltip.category", "band": 0.5},
          "y": {"scale": "yscale", "signal": "tooltip.amount", "offset": -2},
          "text": {"signal": "tooltip.amount"},
          "fillOpacity": [
            {"test": "datum === tooltip", "value": 0},
            {"value": 1}
          ]
        }
      }
    }
  ]
}

回答1:


Your first issue is that you have created invalid JSON: your timestamps need to be quoted. That is, replace 1:42.15 with "1:42.15".

As for parsing the dates, this is easiest to do in Vega-Lite. You can use the Data Format value to control how your date strings are interpreted. For example (online editor):

{
  "data": {
      "values": [
        {"category": "1", "amount": "1:42.15"},
        {"category": "2", "amount": "1:42.61"},
        {"category": "3", "amount": "1:42.93"},
        {"category": "4", "amount": "1:43.41"}
      ],
      "format": {"parse": {"amount": "date:'%M:%S.%L'"}}
  },
  "mark": "point",
  "encoding": {
    "x": {"field": "category", "type": "ordinal"},
    "y": {"field": "amount", "timeUnit": "minutessecondsmilliseconds", "type": "temporal"}
  }
}

If you're interested in how this translates to Vega, you can click the "Compiled Vega" tab in the above editor link.



来源:https://stackoverflow.com/questions/59787286/is-it-possible-to-display-vega-vega-lite-bar-based-on-period-time-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!