问题
I would like to use jqPlot to create 3 separate charts on a page, is it possible to configure jqPlot so that as the cursor moves across one chart, a vertical line would also move across the other charts?
回答1:
I also needed to track a vertical line on 2 charts simultaneously and using Boro's answer as starting point, this is what I came up with:
var mydata1 = [
[0, 3],
[1, 7],
[2, 9],
[3, 1],
[4, 4],
[5, 6],
[6, 8],
[7, 2],
[8, 5]
];
var mydata2 = [
[0, 5],
[1, 4],
[2, 8],
[3, 7],
[4, 2],
[5, 8],
[6, 5],
[7, 1],
[8, 3]
];
$(document).ready(function () {
var plot1 = $.jqplot(
'chart1', [mydata1], {
seriesDefaults: {
showMarker: false
},
cursor: {
show: true,
showTooltip: false,
showVerticalLine: true,
showHorizontalLine: false
},
highlighter: {
show: true,
showTooltip: false
},
canvasOverlay: {
show: true,
objects: [{
verticalLine: {
show: false,
name: "vline1",
xOffset: '-1',
yOffset: '0',
xaxis: "xaxis",
lineWidth: '0.5',
shadow: false
}
}]
}
});
var plot2 = $.jqplot(
'chart2', [mydata2], {
seriesDefaults: {
showMarker: false
},
cursor: {
show: true,
showTooltip: false,
showVerticalLine: true,
showHorizontalLine: false
},
highlighter: {
show: true,
showTooltip: false
},
canvasOverlay: {
show: true,
objects: [{
verticalLine: {
show: false,
name: "vline2",
xOffset: '-1',
yOffset: '0',
xaxis: "xaxis",
lineWidth: '0.5',
shadow: false
}
}]
}
});
var co1 = plot1.plugins.canvasOverlay;
var co2 = plot2.plugins.canvasOverlay;
var line1 = co1.get('vline1');
var line2 = co2.get('vline2');
$("#chart1").bind('jqplotMouseMove', function (ev, gridpos, datapos, neighbor, data) {
line2.options.show = true;
line2.options.x = datapos.xaxis;
co2.draw(plot2);
});
$("#chart2").bind('jqplotMouseMove', function (ev, gridpos, datapos, neighbor, data) {
line1.options.show = true;
line1.options.x = datapos.xaxis;
co1.draw(plot1);
});
$("#chart1").bind('jqplotMouseLeave', function () {
line2.options.show = false;
co2.draw(plot2);
});
$("#chart2").bind('jqplotMouseLeave', function () {
line1.options.show = false;
co1.draw(plot1);
});
});
Here's the demo
回答2:
Yea you could do it. In your approach you would have to track the mouse position on a plot, e.g:
$('#chart').bind('jqplotMouseMove', function(ev, seriesIndex, pointIndex, data) {
//do your painting here
});
Then on every move of the mouse in your plot you would do your custom painting on the other plot's canvas. I do some custom painting in this example showing highlight of a plot's data from code level.
来源:https://stackoverflow.com/questions/10783228/jqplot-synchronize-cursor-across-multiple-charts