d3时标给了我一个NaN

时间:2013-08-20 08:04:37

标签: d3.js rhino

我尝试通过Java + rhino + evjs + d3渲染svg。一切都与线性尺度相当好。只有时间尺度,我遇到一个问题,我无法解决。

请查看这一小段javascript:

function Scale_timed(f,domain,range){
    var obj = this;
    this.field = f;

    // print (DATA.jsGetDomain(domain)[0] + "/" + DATA.jsGetDomain(domain)[1]);
    // print (range[0] + "/" + range[1]);
    this.scale = d3
        .time.scale()
        .range(range)
        .domain(DATA.jsGetDomain(domain));

    this.get = function(i) {
        print("lala: " + DATA.jsGetPrimitive(obj.field,i) + " - " + (DATA.jsGetPrimitive(obj.field,i).getDate()) + " - " + obj.scale(DATA.jsGetPrimitive(obj.field,i)));
        return obj.scale(DATA.jsGetPrimitive(obj.field,i));
    };
}

其中DATA是rhino脚本范围中的Java对象。输出清楚地显示了scale函数input参数是一个Date,但我仍然从我的scale函数中得到一个NaN。

输出:

lala: Tue Jan 04 00:00:00 CET 2011 - 4 - NaN
lala: Wed Jan 05 00:00:00 CET 2011 - 5 - NaN
lala: Thu Jan 06 00:00:00 CET 2011 - 6 - NaN
lala: Fri Jan 07 00:00:00 CET 2011 - 7 - NaN
lala: Mon Jan 10 00:00:00 CET 2011 - 10 - NaN

我认为问题出现在d3规模的使用中...所以我只会问d3标签。

编辑:这将非常有效:

function Scale_timed(f,dom,range){
    var obj = this;
    this.field = f;

    // print (DATA.jsGetDomain(domain)[0].getDate() + "/" + DATA.jsGetDomain(domain)[1].getDate());
    // print ("range: "+ range[0] + "/" + range[1]);
    this.scale = d3
        .scale.linear() //.time.scale()
        .range(range)
        .domain([DATA.jsGetDomain(dom)[0].getTime(), DATA.jsGetDomain(dom)[1].getTime()])
        //.domain(DATA.jsGetDomain(dom))
        .nice();

    this.get = function(i) {
        print("time: " + DATA.jsGetPrimitive(obj.field,i) + " - " + (DATA.jsGetPrimitive(obj.field,i).getDate()) + " - " + obj.scale(DATA.jsGetPrimitive(obj.field,i).getTime()));
        return obj.scale(DATA.jsGetPrimitive(obj.field,i));
    };
}

现在我认为它是一个犀牛问题,可能缺少Date.protype函数......

1 个答案:

答案 0 :(得分:0)

实际上它是一个Rhino问题,rhino使用java.util.Date作为日期而不是ECMA日期,所以你必须自己转换它.. wtf ..但是这样可以工作:

function Scale_timed(f,dom,range){
    var obj = this;
    var domain = DATA.jsGetDomain(dom);
    for (var i=0;i<domain.length;i++) domain[i] = new Date(domain[i].getTime());

    this.field = f;

    this.scale = d3
        .time.scale()
        .range(range)
        .domain(domain)
        ;

    this.get = function(i) {
        // print("time: " + DATA.jsGetPrimitive(obj.field,i) + " - " + (DATA.jsGetPrimitive(obj.field,i).getDate()) + " - " + obj.scale(DATA.jsGetPrimitive(obj.field,i).getTime()));
        print("time: " + DATA.jsGetPrimitive(obj.field,i) + " - " + (DATA.jsGetPrimitive(obj.field,i).getDate()) + " - " + obj.scale(new Date(DATA.jsGetPrimitive(obj.field,i).getTime())));
        return obj.scale(new Date(DATA.jsGetPrimitive(obj.field,i).getTime()));
    };
}