如何仅获取在crossfilter中具有特定代码的数据

时间:2015-04-22 19:46:24

标签: d3.js dc.js crossfilter

我有以下数据集

var data=
[
{code:501, value:25},
{code:501, value:30},
{code:501, value:30},
{code:501, value:35},
{code:501, value:60},
{code:502, value:25},
{code:502, value:25}
]

我想只返回仅包含代码为501的数据的数据集。 我尝试了以下代码

var ndx=crossfilter(data);
var dim1=ndx.dimension(function(d){return d.code});
var filtereddata=dim1.filter(501);

但它不起作用。它没有返回我想要的数据。我尝试使用控制台打印返回的数据,但它打印了一些模糊的东西。

1 个答案:

答案 0 :(得分:1)

function getData(){
  var range = $("#range").val()*1;
  if(isNaN(range)){
    alert("Kindly Enter Valid Number");
  }else{
    var data=
[
{code:501, value:25},
{code:501, value:30},
{code:501, value:30},
{code:501, value:35},
{code:501, value:60},
{code:502, value:25},
{code:502, value:25}
];

var ndx=crossfilter(data);
var dim1=ndx.dimension(function(d){return d.code});
var filtereddata=dim1.filter(range);
    alert("Requested Data is\n"+(JSON.stringify(dim1.top(Infinity))));
}
  
}  
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.11/crossfilter.js"></script>
<input type='text' id='range'>
<input type='button' value='getData' onclick='getData()'>

我认为你正在寻找这个.... :d