【问题标题】:How to access the underlying data from a viz如何从可视化项访问基础数据
【发布时间】:2022-01-05 18:57:45
【问题描述】:

我正在使用 vega-lite 生成可视化。

viz 包括以下变换:

        {
            calculate: "year(datum['Order Date'])",
            as: "Year"
        },

我希望允许用户根据该字段的值过滤可视化项。例如,我想给我的用户一个包含 2018、2019、2020 和 2021 的菜单,并允许他们选择一个。

vega(或 vega-lite)是否公开允许我根据数据以编程方式获取此过滤器的有效值的函数? (例如 2018 年、2019 年、2020 年和 2021 年)。

我的替代方法是自己解析数据,并重新实现 vega-lite 已经在幕后执行的计算逻辑。我偶然发现了 vega 库中公开的 loaderinferTypes 函数,这使我可以稍微检查数据,但这些对生成的字段没有帮助(例如,通过 calculate 转换)。

另一种选择是为上述calculate 转换创建具有不同聚合的可视化项,并刮取生成的可视化项,但这似乎有点太老套了。

【问题讨论】:

    标签: vega-lite vega


    【解决方案1】:

    您可以将paramsbindselect 输入一起使用,该输入可以有一个下拉选项。可以使用element 配置将此输入附加到任何元素。然后,在 filter 转换中使用这些值来过滤掉您的数据。

    下面是一个示例配置或参考editor:

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "",
      "data": {
        "url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
      },
      "height": 300,
      "width": 350,
      "mark": {"type": "bar", "color": "skyblue"},
      "transform": [
        {"filter": "datum.year==year"},
        {
          "filter": {
            "field": "country",
            "oneOf": [
              "United Kingdom",
              "Spain",
              "France",
              "Netherlands",
              "Portugal",
              "Italy",
              "Poland",
              "Albania",
              "Germany",
              "Belgium",
              "Austria",
              "Denmark"
            ]
          }
        }
      ],
      "params": [
        {
          "name": "year",
          "value": 2019,
          "bind": {
            "input": "select",
            "options": ["2015", "2016", "2018", "2019", "2020", "2021"],
            "name": "Select the year:"
          }
        }
      ],
      "encoding": {
        "y": {
          "field": "percentage",
          "type": "quantitative",
          "title": "Percentage of low carbon energy",
          "axis": {"grid": false}
        },
        "x": {
          "field": "country",
          "type": "nominal",
          "title": "",
          "axis": {"grid": false, "labelAngle": 20},
          "sort": "-y"
        },
        "tooltip": [
          {"field": "country", "title": "Country"},
          {"field": "percentage", "title": "percentage of low carbon energy"}
        ]
      }
    }
    

    编辑

    要获取数据,您只需将 vega api 用作view.data('source_0'),其中 source_0 是数据查看器中的数据名称。要设置或添加数据,请使用相同的 api,但第二个参数为 view.data('source_0', data)。请参阅documentation 了解更多信息。

    我的建议是使用上面示例中所做的 params 方法,您不需要在 vega 中创建下拉列表。您可以在外部拥有自己的下拉菜单,并使用 API 更新视图参数。

    请参考以下使用 API 更改参数的示例,并且我已经在控制台上记录了数据,以防您更喜欢其他方法或参考 fiddle

    var yourVlSpec = {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "",
      "data": {
        "url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
      },
      "height": 300,
      "width": 350,
      "mark": {"type": "bar", "color": "skyblue"},
      "transform": [
        {"filter": "datum.year==year"},
        {
          "filter": {
            "field": "country",
            "oneOf": [
              "United Kingdom",
              "Spain",
              "France",
              "Netherlands",
              "Portugal",
              "Italy",
              "Poland",
              "Albania",
              "Germany",
              "Belgium",
              "Austria",
              "Denmark"
            ]
          }
        }
      ],
      "params": [
        {
          "name": "year",
          "value": 2018
        }
      ],
      "encoding": {
        "y": {
          "field": "percentage",
          "type": "quantitative",
          "title": "Percentage of low carbon energy",
          "axis": {"grid": false}
        },
        "x": {
          "field": "country",
          "type": "nominal",
          "title": "",
          "axis": {"grid": false, "labelAngle": 20},
          "sort": "-y"
        },
        "tooltip": [
          {"field": "country", "title": "Country"},
          {"field": "percentage", "title": "percentage of low carbon energy"}
        ]
      }
    }
    var view;
    vegaEmbed("#vis", yourVlSpec).then(res => {
        view = res.view;
      //To get the data
      console.log(view.data('source_0'));
      
    });
    
    /* To set the Data
    view.data('source_0',[new data to be set]);
    */
    
    function handleChange(a,b){
    var selectValue = document.getElementById("myselect").value;
    //Set the param name value dynamically.
    view.signal('year',selectValue)
    view.runAsync();
    }
     <script src="https://cdn.jsdelivr.net/npm/vega@5.20.2/build/vega.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/vega-lite@5.0.0/build/vega-lite.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/vega-embed@6.17.0/build/vega-embed.min.js"></script>
    
    <select id="myselect" style="width:100px;" onchange="handleChange()">
      <option>2018</option>
      <option>2019</option>
      <option>2020</option>
    </select>
    <br>
    <div id="vis"></div>

    【讨论】:

    • 这不适用于我的方案,原因有两个。 1)我需要自己托管过滤器控件。我正在使用 vega-lite 在我的后端创建可视化的图像,并在不支持原生运行 vega lite 的应用程序中显示这些可视化。 2) 选项需要根据数据动态。我无法将年份硬编码到规范中。这就是我有兴趣访问基础数据的原因。
    • 我在上面的答案中添加了更多方法,您可以在其中查看我访问过的数据,还提到了更新数据的方法以及使用参数的另一种有效方法。
    • view.data 看起来像我需要的!
    猜你喜欢
    • 2013-04-02
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    • 2012-08-13
    • 2016-05-02
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多