微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于 vega lite API 的具有定量和名义属性的交叉过滤器

如何解决基于 vega lite API 的具有定量和名义属性的交叉过滤器

我想创建一个基于 vega lite API 的通用交叉过滤器。定量属性显示为直方图,名义/有序属性显示为条形图。

在这个 observable (https://observablehq.com/@ee2dev/exploring-a-csv-file-with-vega-lite) 中,我尝试实现它(参见单元格 vis3 - 最后一个可视化旁边)。

代码如下:

vis3 = {
  vl.vega.timeFormatLocale(locale); // show dates in German 
  vl.vega.formatLocale(locale); // show numbers with German groupings/ separators
  const brush = vl.selectInterval().encodings('x').resolve('intersect');

  // 1 histograms for quantitative data
  const hist1 = vl.markBar({tooltip: true}).encode(
    vl.x().fieldQ(vl.repeat('row'))
      //.bin({maxbins: 100,minstep: 1}) // up to 100 bins,but no smaller than 1 unit
      .bin({maxbins: 100}) 
      //.axis({format: 'd',titleAnchor: 'start'}),// integer format,left-aligned title
      .axis({titleAnchor: 'start'}),vl.y().count().title(null) // no y-axis title
  );
  
  const hist1r = vl.layer(
      hist1.select(brush).encode(vl.color().value('lightgrey')),hist1.transform(vl.filter(brush))
    )
    .width(800).height(100)
    .repeat({row: Object.keys(attributes).filter(d => attributes[d] === "quantitative")});

  // 2 bars for non-quantitative data
  const hist2 = vl.markBar({tooltip: true}).encode(
    vl.x().field(vl.repeat('row'))
      .axis({titleAnchor: 'start'}),// left-aligned title
    vl.y().count().title(null) // no y-axis title
  );
  const hist2r = vl.layer(
      hist2.select(brush).encode(vl.color().value('lightgrey')),hist2.transform(vl.filter(brush))
    )
    .width(800).height(100)
    .repeat({row: Object.keys(attributes).filter(d => (attributes[d] !== "quantitative") && (cardinality[d] < 30) )});

  const plot = vl
    .data(processedData)
    .config({view: {stroke: null}}) // no outline
    .vconcat(hist1r,hist2r);
  
  return plot.render();
  // return html`<pre>${JSON.stringify(plot.toObject(),2)}</pre>`; // format JSON data
} 

但是该代码至少有两个缺点:

  • 画笔选择始终是 selectInterval()。我希望它为定量属性为 selectInterval(),否则为 selectMulti()

  • 完成这项工作的更有效方法是什么?我为定量和其余属性创建了两个不同的图表,仅仅是因为后者不需要分箱。

任何帮助将不胜感激!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?