简体   繁体   English

将字段设置为数组中的 object,Vega-lite

[英]Setting a field to an object in an array, Vega-lite

I have an array of objects that I am using as a dataset in an interactive data dashboard.我有一个对象数组,我将它们用作交互式数据仪表板中的数据集。 I want to add a new feature that displays data from only one object at a time, rather than pulling data from all objects (which I am already doing and it works well).我想添加一项新功能,一次只显示一个 object 的数据,而不是从所有对象中提取数据(我已经在这样做并且效果很好)。 As a test case, I have created a simple array:作为测试用例,我创建了一个简单的数组:

var test1 = [
  [{
    "name": "Piece One",
    "amount": 5
  }, {
    "name": "Piece Two",
    "amount": 5
  }, {
    "name": "Piece Three",
    "amount": 5
  }],
  [{
    "name": "Piece One",
    "amount": 1
  }, {
    "name": "Piece Two",
    "amount": 1
  }, {
    "name": "Piece Three",
    "amount": 5
  }]
];

and the Vega-lite javascript:和 Vega-lite javascript:

var pieCreate = {
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "title": "A pie chart",
  "description": "A simple pie chart with embedded data.",
  "width": "container",
  "height": "container",
  "data": {
    "values": test1[0]
  },
  "mark": "arc",
  "encoding": {
    "theta": {
      "field": "amount",
      "type": "quantitative"
    },
    "color": {
      "field": "name",
      "type": "nominal",
      "legend": null
    }
  }
};

This works, but I want the user to be able to choose which object to display (in the dashboard, each object contains data on different schools, and I want the user to be able to choose which school's data to display using a dropdown menu).这可行,但我希望用户能够选择显示哪个 object(在仪表板中,每个 object 都包含不同学校的数据,我希望用户能够使用下拉菜单选择要显示哪个学校的数据) . My first thought was to set up a signal in the "data": {"values": field that would change the number in brackets to the array I want, but after a lot of trial and error, I think that may be a dead end.我的第一个想法是在"data": {"values":字段中设置一个信号,将括号中的数字更改为我想要的数组,但经过反复试验,我认为这可能是一个死结尾。 Signals should work to modify "field": "amount" and "field": "name" but I've tried every iteration of [0].amount that I can think of, while dropping the brackets from test1[0] and nothing has worked.信号应该可以修改"field": "amount""field": "name"但我已经尝试了我能想到的 [0].amount 的每一次迭代,同时从test1[0]中删除了括号并且什么都没有已经工作了。 If I can manage to access the object by directly referencing it in "field": I believe I can figure out the process using a signal and html form, but I'm starting to doubt if I'm even on the right track here.如果我可以通过在"field":我相信我可以使用信号和 html 表格找出过程,但我开始怀疑我是否走在正确的轨道上。

I also tried the process outlined here in the vega-lite documentation: https://vega.github.io/vega-lite/tutorials/streaming.html , but it's doing something much more complicated than what I'm trying to do, and my javascript knowledge isn't sufficient to break it down to something usable.我还尝试了 vega-lite 文档中概述的过程: https://vega.github.io/vega-lite/tutorials/streaming.html ,但它做的事情比我想做的要复杂得多,而我的 javascript 知识不足以将其分解为可用的东西。 Does anyone have any ideas on how to make this work, using any of the above approaches (or a new, better one)?有没有人对如何使用上述任何方法(或新的更好的方法)使这项工作有任何想法?

You can use the vega Api's to change the data.您可以使用vega Api来更改数据。 On your selection, add a change event and on some conditions you can toggle between your data using those API's.根据您的选择,添加更改事件,在某些情况下,您可以使用这些 API 在数据之间切换。 Refer the below snippet or fiddle :请参考以下代码片段或小提琴

 var test1 = [ [{ "name": "Piece One", "amount": 5 }, { "name": "Piece Two", "amount": 5 }, { "name": "Piece Three", "amount": 5 }], [{ "name": "Piece One", "amount": 1 }, { "name": "Piece Two", "amount": 1 }, { "name": "Piece Three", "amount": 5 }] ]; var yourVlSpec = { "$schema": "https://vega.github.io/schema/vega-lite/v5.json", "title": "A pie chart", "description": "A simple pie chart with embedded data.", "width": "350", "height": "400", "data": { "values": test1[0] }, "mark": "arc", "encoding": { "theta": { "field": "amount", "type": "quantitative" }, "color": { "field": "name", "type": "nominal", "legend": null } } } var view; vegaEmbed("#vis", yourVlSpec).then(res => { view = res.view; }); function handleChange(a, b) { var selectValue = document.getElementById("myselect").value; if (selectValue == 'A') { view.data('source_0', test1[0]); } else { view.data('source_0', test1[1]); } 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>A</option> <option>B</option> </select> <br> <div id="vis"></div>

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

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