这是最新版本的 Rickshaw 的上述代码的修改版本:
Rickshaw.Graph.RangeSlider = Rickshaw.Class.create({
initialize: function(args) {
var element = this.element = args.element;
var graph = this.graph = args.graph;
this.onslide = args.onslide;
this.build();
if( graph.constructor === Array ) {
for( var i=0; i<graph.length; i++ ) {
graph[i].onUpdate( function() { this.update() }.bind(this) );
}
} else {
graph.onUpdate( function() { this.update() }.bind(this) );
}
},
build: function() {
var element = this.element;
var graph = this.graph;
var onslide = this.onslide;
if( graph.constructor === Array ) {
$( function() {
$(element).slider( {
range: true,
min: graph[0].dataDomain()[0],
max: graph[0].dataDomain()[1],
values: [
graph[0].dataDomain()[0],
graph[0].dataDomain()[1]
],
slide: function( event, ui ) {
for( var i=0; i<graph.length; i++) {
graph[i].window.xMin = ui.values[0];
graph[i].window.xMax = ui.values[1];
graph[i].update();
// if we're at an extreme, stick there
if (graph[i].dataDomain()[0] == ui.values[0]) {
graph[i].window.xMin = undefined;
}
if (graph[i].dataDomain()[1] == ui.values[1]) {
graph[i].window.xMax = undefined;
}
}
if( onslide ) {
onslide(ui.values[0],ui.values[1]);
}
}
} );
} );
element[0].style.width = graph[0].width + 'px';
} else {
$( function() {
$(element).slider( {
range: true,
min: graph.dataDomain()[0],
max: graph.dataDomain()[1],
values: [
graph.dataDomain()[0],
graph.dataDomain()[1]
],
slide: function( event, ui ) {
graph.window.xMin = ui.values[0];
graph.window.xMax = ui.values[1];
graph.update();
// if we're at an extreme, stick there
if (graph.dataDomain()[0] == ui.values[0]) {
graph.window.xMin = undefined;
}
if (graph.dataDomain()[1] == ui.values[1]) {
graph.window.xMax = undefined;
}
if( onslide ) {
onslide(ui.values[0],ui.values[1]);
}
}
} );
} );
element[0].style.width = graph.width + 'px';
}
},
update: function() {
var element = this.element;
var graph = this.graph;
var values = $(element).slider('option', 'values');
if( graph.constructor === Array ) {
graph = graph[0];
}
$(element).slider('option', 'min', graph.dataDomain()[0]);
$(element).slider('option', 'max', graph.dataDomain()[1]);
if (graph.window.xMin == null) {
values[0] = graph.dataDomain()[0];
}
if (graph.window.xMax == null) {
values[1] = graph.dataDomain()[1];
}
$(element).slider('option', 'values', values);
}
});