D3.js d3:使用不同变量调用笔刷函数

D3.js d3:使用不同变量调用笔刷函数,d3.js,D3.js,我画了3张图,并为每个图创建了一个画笔,如下所示: var brush_month = d3.brushX() .extent([[xScale_month.range()[0], padding],[xScale_month.range()[1],h_bar-padding ]]) .on("brush", brushmove) .on("end", brushend); var brushg_month = svg_bar1.append(

我画了3张图,并为每个图创建了一个画笔,如下所示:

var brush_month = d3.brushX()
        .extent([[xScale_month.range()[0], padding],[xScale_month.range()[1],h_bar-padding ]]) 
        .on("brush", brushmove)
        .on("end", brushend);

var brushg_month = svg_bar1.append("g")
        .attr("class","brush")
        .call(brush_month);

var brush_day = d3.brushX()
        .extent([[xScale_day.range()[0], padding],[xScale_day.range()[1],h_bar-padding ]]) 
        .on("brush", brushmove)
        .on("end", brushend);

var brushg_day = svg_bar2.append("g")
        .attr("class","brush")
        .call(brush_day);

var brush_hour = d3.brushX()
        .extent([[xScale_hour.range()[0], padding],[xScale_hour.range()[1],h_bar-padding ]]) 
        .on("brush", brushmove)
        .on("end", brushend);

var brushg_hour = svg_bar3.append("g")
        .attr("class","brush")
        .call(brush_hour);
然后我创建了一个brushend和brushmove函数,根据使用的笔刷,应该使用不同的变量(dim和xScale)。我不太确定如何使用不同的变量调用函数,或者是否有必要为每个笔刷创建brushend和brushmove函数。我的brushmove功能如下所示,仅使用一个笔刷时效果良好:

function brushmove(){

        if(d3.event.selection != null){

            //map the coordinates of the brushs' ends to the month/day/hour 
            var d0 = d3.event.selection.map(xScale.invert),
                d1 = [Math.round(d0[0]),Math.round(d0[1])];


            // If empty when rounded, use floor & ceil instead.
            if (d1[0] >= d1[1]) {
                d1[0] = Math.floor(d0[0]);
                d1[1] = Math.ceil(d0[1]);
            }


            //filter dimension such that it is within the brush
            if (d1[0] == (d1[1]-1)){

                dim.filter(d1[0]);     

            } else {

                dim.filter([d1[0],d1[1]]);   

            }


        }

        //update everything
        renderAll();


}

一种方法是将id分配给画笔(例如,
.attr('class','brush').attr('id','day_brush')
,并基于此id,在
brushmove()
函数中,可以切换功能(例如,
if(d3.选择(this).attr('id')=='day_brush')){……
等等。哦,是的,当然了-这就成功了-非常感谢:)当然没问题!很高兴我能帮上忙。