将字符串转换为日期 vega lite 的问题

issues transforming string to date vega lite

我想在折线图中显示一些数据。但是,我的“Harvest_Year”数据(以年为单位的日期,例如 2017 年或 2018 年)显示为我认为是字符串

我从 .csv 文件导入数据,以下是我将字符串更改为日期格式的步骤。我累了:

"Harvest_Year": "year"

但这没有用,因为它使我的所有值都为空。所以我想先把它变成一个整数,然后再把它变成年。然而,在 Vega-Lite 中,我所有的岁月都在 table 中正确显示,但是当我在折线图上显示它时,我只看到 1970,我确信我在数据集中没有,它只显示那个年.

在下图中,您可以看到我的数据中有所有年份:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {
    "url": "https://raw.githubusercontent.com/DanStein91/Info-vis/master/CoffeeRobN.csv",
    "format": {
      "type": "csv",
            "parse": {
       "Number_of_Bags": "number",
        "Bag_weight": "number",
        "Harvest_Year": "number"
      }
    }
  },
  "transform": [
{
    "timeUnit": "year",
    "field": "Harvest_Year",
    "as": "Year"
  },
     {
      "calculate": "datum.Number_of_Bags * datum.Bag_Weight ",
      "as": "Total_Export"
    }
  ],  
      "width": 300,
      "height": 200,
  "mark": "line",
  "encoding": {
    
    "y": {
      "field": "Total_Export",
      "type": "quantitative"
    },
    "x": {
      "field": "Harvest_Year",
      "type": "temporal"
    }
  },
  "config": {}
}

当你告诉 vega-lite 将数字解释为日期时,它会将它们视为 unix 时间戳,即 1970 年 1 月 1 日之后的毫秒数。你得到的每个日期都是 1970 年,这导致你看到的图表.

你的日期似乎是非标准格式(例如 "2017.0" 表示 2017 年)所以你必须使用 vega expressions to manually parse them into date objects. Here is an example of this (view in editor:

{
  "data": {
    "url": "https://raw.githubusercontent.com/DanStein91/Info-vis/master/CoffeeRobN.csv",
    "format": {
      "type": "csv",
      "parse": {
        "Number_of_Bags": "number",
        "Bag_weight": "number",
        "Harvest_Year": "number"
      }
    }
  },
  "transform": [
    {"filter": "isValid(datum.Harvest_Year)"},
    {"calculate": "datetime(datum.Harvest_Year, 1)", "as": "Harvest_Year"},
    {
      "calculate": "datum.Number_of_Bags * datum.Bag_Weight ",
      "as": "Total_Export"
    }
  ],
  "mark": "point",
  "encoding": {
    "y": {"field": "Total_Export", "type": "quantitative"},
    "x": {"field": "Harvest_Year", "type": "ordinal", "timeUnit": "year"}
  },
  "width": 300,
  "height": 200
}

另一种选择是完全避免 datetimetimeUnit 逻辑(因为您的数据实际上不包含任何日期),直接在编码中使用年份数字;例如

{
  "data": {
    "url": "https://raw.githubusercontent.com/DanStein91/Info-vis/master/CoffeeRobN.csv",
    "format": {
      "type": "csv",
      "parse": {
        "Number_of_Bags": "number",
        "Bag_weight": "number",
        "Harvest_Year": "number"
      }
    }
  },
  "transform": [
    {"filter": "isValid(datum.Harvest_Year)"},
    {
      "calculate": "datum.Number_of_Bags * datum.Bag_Weight ",
      "as": "Total_Export"
    }
  ],
  "mark": "point",
  "encoding": {
    "y": {"field": "Total_Export", "type": "quantitative"},
    "x": {"field": "Harvest_Year", "type": "ordinal"}
  },
  "width": 300,
  "height": 200
}