简体   繁体   English

如何将平均聚合 vega-lite 语法转换为 vega-lite-api 代码

[英]How to translate mean aggregation vega-lite grammar to vega-lite-api code

I was trying to learn vega-lite-api.我试图学习 vega-lite-api。 I came across this vega-lite example:我遇到了 这个vega-lite 示例:

{
  "data": {"url": "data/cars.json"},
  "mark": "bar",
  "encoding": {
    "x": {"field": "Cylinders"},
    "y": {"aggregate": "mean", "field": "Acceleration"}
  }
}

It outputs:它输出:

在此处输入图像描述

I tried to translate it to vega-lite-api code:我试图将其翻译成 vega-lite-api 代码:

 var data = JSON.parse($.getJSON({'url': "https://unpkg.com/vega-datasets@1.25.0/data/cars.json", 'async': false}).responseText); createChart = function (data) { const plot = vl.markBar() .data(data) .encode( vl.x() .fieldQ('Cylinders'), vl.y({"aggregate": "mean", "field": "Acceleration", "type": "quantitative"}) // .fieldQ('Acceleration') ) return plot.toObject(); } const chart_spec_json = this.createChart(data) const opt = { renderer: "canvas", actions: false }; vegaEmbed("#stats", chart_spec_json, opt);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.13.0/d3.min.js"></script> <script src="https://unpkg.com/vega@5.21.0/build/vega.min.js"></script> <script src="https://unpkg.com/vega-lite@5.2.0/build/vega-lite.min.js"></script> <script src="https://www.unpkg.com/vega-embed@6.20.8/build/vega-embed.min.js"></script> <script src="https://unpkg.com/vega-lite-api@5.0.0/build/vega-lite-api.min.js"></script> <div id="stats" />

Run above code.运行上面的代码。 It will output following:它将输出以下内容:

在此处输入图像描述

Now, I am unable to figure out how can I translate this vega-lite grammar to vega-lite-api code.现在,我无法弄清楚如何将这个 vega-lite 语法翻译成 vega-lite-api 代码。 Here is the link to observable notebook. 是可观察笔记本的链接。

Update更新

As pointed out in comments, the data shown by the same graphs is same.正如评论中所指出的,相同图表显示的数据是相同的。 So the output is definitely correct.所以输出肯定是正确的。 I started up without aggregation (as you can see in the observable notebook) which also had similar thin bar charts.我开始时没有聚合(正如您在可观察笔记本中看到的那样),它也有类似的细条形图。 Now after adding aggregation, I was expecting the exact visualization (at least bar widths).现在添加聚合后,我期待精确的可视化(至少条形宽度)。 They are exact in terms of data / height, but same width with non aggregated one tricked me in thinking that it did not change.它们在数据/高度方面是准确的,但是与非聚合宽度相同的宽度让我误以为它没有改变。

Now I have following doubts:现在我有以下疑问:

Q1. Q1。 Why the bars are thinner and not wider as in case of doc's visualization ?为什么条形图更细而不是像文档可视化那样更宽?

Q2. Q2。 Why there is difference between scales?为什么秤之间有差异? Does vega-lite-api defines different default scale configuration than vega-lite? vega-lite-api 是否定义了与 vega-lite 不同的默认比例配置?

Q3. Q3。 Also I have specified grammar inside vl() .我还在vl()中指定了语法。 How do I do it with "corresponding" api using aggregate() ?如何使用aggregate()使用“相应”api 来做到这一点?

Try this.尝试这个。

mean_acceleration = {
  const plot = vl.markBar()
                .data(cars_data)
                .encode(
                  vl.x()
                    .field('Cylinders'),
                  vl.y()
                    .field('Acceleration')
                    .aggregate('mean')
                )
  return plot.render();
}

在此处输入图像描述

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

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