当前位置: 首页 > 知识库问答 >
问题:

d3.js过滤器()不显示预期结果

有宏峻
2023-03-14

filter命令未按预期工作。如何重构代码以获得请求的输出?

. filter((d, i)=

测试1-注释掉-测试2和3的反转,并且它工作(演示测试2和3中的条件不匹配,因为显示点的代码工作)
测试2-在周一1月4日08:00没有看到绿点类Data point(速记语法)
Test3-在星期六1月02日08:00数据点(传统语法,差异数据点)
Test4-类似于上述,使用console.log
Test5-测试类型-console.log不显示任何内容,console.dir只显示文本单词“对象”Test6-console.log显示其中一个数据点与testdate1完全匹配

jsFiddle用于实验

clearconsole();
var chartWidth = 500;
var myCSV = [	
{"shift":"1","date":"01/01/2016/08/00/00","car":"178","truck":"125","bike":"317","moto":"237"},
{"shift":"2","date":"01/01/2016/17/00/00","car":"125","truck":"189","bike":"125","moto":"273"},
{"shift":"3","date":"02/01/2016/08/00/00","car":"140","truck":"219","bike":"328","moto":"1252"},
{"shift":"4","date":"02/01/2016/17/00/00","car":"222","truck":"290","bike":"432","moto":"378"},
{"shift":"5","date":"03/01/2016/08/00/00","car":"200","truck":"250","bike":"420","moto":"319"},
{"shift":"6","date":"03/01/2016/17/00/00","car":"230","truck":"220","bike":"310","moto":"413"},
{"shift":"7","date":"04/01/2016/08/00/00","car":"155","truck":"177","bike":"377","moto":"180"},
{"shift":"8","date":"04/01/2016/17/00/00","car":"179","truck":"203","bike":"405","moto":"222"},
{"shift":"9","date":"05/01/2016/08/00/00","car":"208","truck":"185","bike":"360","moto":"195"},
{"shift":"10","date":"05/01/2016/17/00/00","car":"150","truck":"290","bike":"315","moto":"280"},
{"shift":"11","date":"06/01/2016/08/00/00","car":"200","truck":"220","bike":"350","moto":"205"},
{"shift":"12","date":"06/01/2016/17/00/00","car":"230","truck":"170","bike":"390","moto":"400"},
];
var testdate1 = 'Mon Jan 04 2016 08:00:00 GMT-0500 (Eastern Standard Time)';
var testdate2 = 'Sat Jan 02 2016 08:00:00 GMT-0500 (Eastern Standard Time)';
lc1 = dc.lineChart("#line1");

var dateFormat = d3.time.format("%d/%m/%Y/%H/%M/%S");
myCSV.forEach(function (d) {
	d.date = dateFormat.parse(d.date);
});

myCSV.forEach(function (d) {
	d['car'] = +d['car'];
});

//console.log(myCSV);

var facts = crossfilter(myCSV);
var dateDim = facts.dimension(function (d) {return d.date});

var carDim = facts.dimension(function (d) {return d['car']});
var dgCar = dateDim.group().reduceSum(function (d) {return d['car']});

var minDate = new Date ("2016-01-01T08:00:00.000Z");
var maxDate = new Date ("2016-01-06T17:00:00.000Z");	

var maxY = d3.max(myCSV, function(d) {return d['car']});

lc1
.renderArea(false)
.width(chartWidth)
.height(250)
.dimension(dateDim)
.group(dgCar)
.defined(function(d) {if(d.y !==null) {return d.y;}})
.transitionDuration(1000)
.margins({top: 30, right: 20, bottom: 35, left: 60})
.yAxisLabel('Cars')
.renderHorizontalGridLines(true)
.brushOn(false)
.x(d3.time.scale().domain([minDate,maxDate]));
lc1.yAxis().ticks(5);
lc1.xAxis().ticks(3);

lc1.on('renderlet', function(lc1) {
console.log('myCSV.length: '+myCSV.length);
   var allDots1 = lc1.selectAll('circle.dot');
   //allDots1.filter((d,i) => i != testdate1).classed('pinkdot',true);
   allDots1.filter((d,i) => i === testdate1).classed('greendot',true);
   allDots1.filter(function(d){ return d.x===testdate2 }).classed('reddot',true);
   allDots1.filter(function(d){ if (d.x===testdate2) console.log('Found it') });
   allDots1.filter(function(d){ var xyz = d.x; console.dir(typeof xyz) });
   allDots1.filter((d, k) => console.log("\r\n["+d.x+"]\r\n["+testdate1+"]") );

});//END lc1.on(renderlet)

dc.renderAll();
dc.redrawAll();

function clearconsole(){
console.API;
if (typeof console._commandLineAPI !== 'undefined') {
    console.API = console._commandLineAPI; //chrome
} else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
    console.API = console._inspectorCommandLineAPI; //Safari
} else if (typeof console.clear !== 'undefined') {
    console.API = console;
}
console.API.clear();
}
svg{height:280px;width:500px;}
.greendot{stroke:green !important; fill:green !important; fill-opacity:1 !important;} 
.reddot{stroke:red !important; fill:red !important; fill-opacity:1 !important;}
.pinkdot{stroke:pink; fill:pink; fill-opacity:1 !important;}
<script src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.3.3/d3.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.1/crossfilter.min.js"></script>
<script src="http://dc-js.github.io/dc.js/js/dc.js"></script>
<link href="http://dc-js.github.io/dc.js/css/dc.css" rel="stylesheet"/>

<svg id="line1"></svg>

共有2个答案

严永丰
2023-03-14

执行相等性检查时,testdatetestdate2仍然是字符串。尝试使用vanillanew Date()或d3的日期/时间函数解析它们:

. filter(函数(d){返回d. x

请仔细检查JS Date格式返回的时区-这些值可能不相等,即使它们都作为有效的JS date返回,如果其中一个位于不同的时区。

齐坚成
2023-03-14

比较日期时,请使用getTime()方法,例如:

allDots1.filter(function(d){ 
    console.log(d.data.value, d.x, +d.x, testdate2, d.x===testdate2, +d.x===testdate2); 
    return +d.x===testdate2; // + is a shortcut for .getTime() 
  }).classed('reddot',true);

此外,您应该设置时区的偏移量,

var myCSV = [   
  {"shift":"1","date":"01/01/2016/08/00/00/+0500","car":"178","truck":"125","bike":"317","moto":"237"},
  {"shift":"2","date":"01/01/2016/17/00/00/+0500","car":"125","truck":"189","bike":"125","moto":"273"},
  {"shift":"3","date":"02/01/2016/08/00/00/-0500","car":"140","truck":"219","bike":"328","moto":"1252"},
  //...
];

//...

var dateFormat = d3.time.format("%d/%m/%Y/%H/%M/%S/%Z");

演示

参考文献#1:日期比较

参考#2:Javascript的“”一元运算符

参考#3:D3 v3时间格式

 类似资料:
  • 有时,我们想要显示一个数组的过滤或排序副本,而不实际改变或重置原始数据。在这种情况下,可以创建返回过滤或排序数组的计算属性。例如: <li v-for="n in evenNumbers">{{ n }}</li> data: { numbers: [ 1, 2, 3, 4, 5 ] }, computed: { evenNumbers: function () { retu

  • 问题内容: 所以我创建了一个集成脚本,但是由于某种原因它没有显示出正确的结果。我的查询是 结果中出乎意料的一行之一是这个 ID为11的组具有display_style_priority 2000而不是5200。它应显示具有display_style_priority 5200的用户组ID。这是该用户所在的排名最高的组。有人可以指出我做错了什么。 问题答案: 尝试将GROUP BY更改为 其他大多数

  • 值a、b和c的组合是唯一的。 我想通过使用值a和b执行bool筛选搜索来查找“value_c”的值。 在我的代码中,我尝试如下: 我是不是漏掉了什么?会不会是因为数据集太小?我们在代码库的其他地方使用了相同的Bool Filter Elastica查询,这些查询如预期的那样工作,但是我似乎无法从这个索引中返回任何数据。 如有任何帮助,不胜感激。

  • Template prefilters are PHP functions that your templates are ran through before they are compiled. This is good for preprocessing your templates to remove unwanted comments, keeping an eye on what pe

  • 更新:我尝试将过滤器实现为OncePerRequestFilter,但仍然不起作用。有人能在这里进一步帮助我吗? 更新2:也试过这个,http://software.dzhuvinov.com/cors-filter-installation.html,运气不好 更新#3:这是我在控制台中的输出,我可以看到响应没有添加任何标题: register.html

  • 问题内容: 我像电话教程一样在angularJS中使用ng- repeat和filter,但我想在页面中突出显示搜索结果。使用基本的jQuery,我只需在输入的键上解析页面,但是我试图以有角度的方式进行操作。有任何想法吗 ? 我的代码: 问题答案: 对于AngularJS v1.2 + HTML: JS: CSS: