2016-07-15 70 views
2

我需要在plotly.js中自定義圖形的懸停交互。我顯示時間序列,並希望懸停光標只是一條垂直線。光標下方的值應顯示在圖表下方的表格中(不在圖表本身內)。我設法顯示垂直線光標並顯示下表中的值,但無法弄清楚如何禁用顯示圖中的值(我的意思是懸停在圖上時具有值的形狀的工具提示),請參閱代碼段。如何在懸停事件觸發時隱藏懸停的值信息?

我只發現我可以通過在佈局上設置屬性hovermode: false來禁用工具提示,但是沒有發生懸停事件,我需要繪製垂直線光標。

有沒有辦法做到這一點?

var gd = document.getElementById('tester'); 
 
var hoverInfo = document.getElementById('hoverinfo'); 
 

 
var traceX = { 
 
    name: "X", 
 
    x: ['2001-06-11 11:50', '2001-06-12 00:00', '2001-06-12 12:30'], 
 
    y: [35, 21, 28], 
 
    type: 'scatter', // set the chart type 
 
    mode: 'lines+markers', 
 
    line: { 
 
    width: 1 
 
    } 
 
}; 
 

 
var cursor1 = { 
 
    xid: 1, 
 
    type: 'line', 
 
    // x-reference is assigned to the x-values 
 
    xref: 'x', 
 
    // y-reference is assigned to the plot paper [0,1] 
 
    yref: 'paper', 
 
    x0: '2001-06-12 12:30', 
 
    y0: 0, 
 
    x1: '2001-06-12 12:30', 
 
    y1: 1, 
 
    fillcolor: '#d3d3d3', 
 
    opacity: 0.1, 
 
}; 
 

 
var layout = { 
 
    yaxis: { 
 
    title: "Wind Speed", 
 
    hoverformat: '' 
 
    }, // set the y axis title 
 
    xaxis: { 
 
    showgrid: false, // remove the x-axis grid lines 
 
    tickformat: "%B, %Y", // customize the date format to "month, day" 
 
    hoverformat: '' 
 
    }, 
 
    margin: { // update the left, bottom, right, top margin 
 
    l: 40, 
 
    b: 40, 
 
    r: 20, 
 
    t: 20 
 
    }, 
 
    showline: true, 
 
    hovermode: 'x', 
 
    shapes: [] 
 
}; 
 

 
var hoverFn = function(data) { 
 
    if (gd.layout.shapes.length === 0) { 
 
    gd.layout.shapes.push(cursor1); 
 
    } 
 
    var update = { 
 
    'shapes[0].x0': data.points[0].x, 
 
    'shapes[0].x1': data.points[0].x 
 
    }; 
 
    Plotly.relayout(gd, update); 
 

 
    var infotext = data.points.map(function(d) { 
 
    return (d.data.name + ': ' + d.x + ' | ' + d.y.toPrecision(3)); 
 
    }); 
 

 
    hoverInfo.innerHTML = infotext.join('<br/>'); 
 
}; 
 

 
var unhoverFn = function(data) { 
 
    //hoverInfo.innerHTML = ''; 
 
} 
 

 
var draw = function(data, layout) { 
 

 
    Plotly.newPlot(gd, data, layout, { 
 
    showLink: false, 
 
    displaylogo: false 
 
    }); 
 

 
    gd.on('plotly_click', function(data) { 
 
     //console.log('click'); 
 
    }) 
 
    .on('plotly_beforehover', function(data) { 
 
     //console.log('beforehover'); 
 
    }) 
 
    .on('plotly_hover', function(data) { 
 
     //var pointNum = data.points[0].pointNumber; 
 
     var pointNum = data; 
 
     hoverFn(data); 
 
    }) 
 
    .on('plotly_unhover', function(data) { 
 
     unhoverFn(data); 
 
    }); 
 

 
    Plotly.addTraces(gd, [traceX]); 
 
}; 
 

 
Plotly.d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_speed_laurel_nebraska.csv', function(rows) { 
 
    var data = [{ 
 
    name: 'P1', 
 
    type: 'scatter', // set the chart type 
 
    mode: 'lines', // connect points with lines 
 
    x: rows.map(function(row) { // set the x-data 
 
     return row['Time']; 
 
    }), 
 
    y: rows.map(function(row) { // set the x-data 
 
     return row['10 Min Sampled Avg']; 
 
    }), 
 
    line: { // set the width of the line. 
 
     width: 1 
 
    } 
 
    }, { 
 
    name: 'P2', 
 
    type: 'scatter', // set the chart type 
 
    mode: 'lines', // connect points with lines 
 
    x: rows.map(function(row) { // set the x-data 
 
     return row['Time']; 
 
    }), 
 
    y: rows.map(function(row) { // set the x-data 
 
     return Number(row['10 Min Sampled Avg']) + 3.0; 
 
    }), 
 
    line: { // set the width of the line. 
 
     width: 1 
 
    } 
 
    }]; 
 

 
    draw(data, layout); 
 
});
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> 
 
<div id="tester" style="width:600px;height:300px;"></div> 
 
<div id="hoverinfo" style="margin-left:80px;"></div>

回答

3

發現了它。將Plotly.Fx.hover(gd, []);加入我的hoverFn()。數組(第二個參數)指定顯示哪些點,只需將其保留爲空即可。

(關於這個example在文檔基於)

var gd = document.getElementById('tester'); 
 
var hoverInfo = document.getElementById('hoverinfo'); 
 

 
var traceX = { 
 
    name: "X", 
 
    x: ['2001-06-11 11:50', '2001-06-12 00:00', '2001-06-12 12:30'], 
 
    y: [35, 21, 28], 
 
    type: 'scatter', // set the chart type 
 
    mode: 'lines+markers', 
 
    line: { 
 
    width: 1 
 
    } 
 
}; 
 

 
var cursor1 = { 
 
    xid: 1, 
 
    type: 'line', 
 
    // x-reference is assigned to the x-values 
 
    xref: 'x', 
 
    // y-reference is assigned to the plot paper [0,1] 
 
    yref: 'paper', 
 
    x0: '2001-06-12 12:30', 
 
    y0: 0, 
 
    x1: '2001-06-12 12:30', 
 
    y1: 1, 
 
    fillcolor: '#d3d3d3', 
 
    opacity: 0.1, 
 
}; 
 

 
var layout = { 
 
    yaxis: { 
 
    title: "Wind Speed", 
 
    hoverformat: '' 
 
    }, // set the y axis title 
 
    xaxis: { 
 
    showgrid: false, // remove the x-axis grid lines 
 
    tickformat: "%B, %Y", // customize the date format to "month, day" 
 
    hoverformat: '' 
 
    }, 
 
    margin: { // update the left, bottom, right, top margin 
 
    l: 40, 
 
    b: 40, 
 
    r: 20, 
 
    t: 20 
 
    }, 
 
    showline: true, 
 
    hovermode: 'x', 
 
    shapes: [] 
 
}; 
 

 
var hoverFn = function(data) { 
 
    Plotly.Fx.hover(gd, []); 
 
    if (gd.layout.shapes.length === 0) { 
 
    gd.layout.shapes.push(cursor1); 
 
    } 
 
    var update = { 
 
    'shapes[0].x0': data.points[0].x, 
 
    'shapes[0].x1': data.points[0].x 
 
    }; 
 
    Plotly.relayout(gd, update); 
 

 
    var infotext = data.points.map(function(d) { 
 
    return (d.data.name + ': ' + d.x + ' | ' + d.y.toPrecision(3)); 
 
    }); 
 

 
    hoverInfo.innerHTML = infotext.join('<br/>'); 
 
}; 
 

 
var unhoverFn = function(data) { 
 
    //hoverInfo.innerHTML = ''; 
 
} 
 

 
var draw = function(data, layout) { 
 

 
    Plotly.newPlot(gd, data, layout, { 
 
    showLink: false, 
 
    displaylogo: false 
 
    }); 
 

 
    gd.on('plotly_click', function(data) { 
 
     //console.log('click'); 
 
    }) 
 
    .on('plotly_beforehover', function(data) { 
 
     //console.log('beforehover'); 
 
    }) 
 
    .on('plotly_hover', function(data) { 
 
     //var pointNum = data.points[0].pointNumber; 
 
     var pointNum = data; 
 
     hoverFn(data); 
 
    }) 
 
    .on('plotly_unhover', function(data) { 
 
     unhoverFn(data); 
 
    }); 
 

 
    Plotly.addTraces(gd, [traceX]); 
 
}; 
 

 
Plotly.d3.csv('https://raw.githubusercontent.com/plotly/datasets/master/wind_speed_laurel_nebraska.csv', function(rows) { 
 
    var data = [{ 
 
    name: 'P1', 
 
    type: 'scatter', // set the chart type 
 
    mode: 'lines', // connect points with lines 
 
    x: rows.map(function(row) { // set the x-data 
 
     return row['Time']; 
 
    }), 
 
    y: rows.map(function(row) { // set the x-data 
 
     return row['10 Min Sampled Avg']; 
 
    }), 
 
    line: { // set the width of the line. 
 
     width: 1 
 
    } 
 
    }, { 
 
    name: 'P2', 
 
    type: 'scatter', // set the chart type 
 
    mode: 'lines', // connect points with lines 
 
    x: rows.map(function(row) { // set the x-data 
 
     return row['Time']; 
 
    }), 
 
    y: rows.map(function(row) { // set the x-data 
 
     return Number(row['10 Min Sampled Avg']) + 3.0; 
 
    }), 
 
    line: { // set the width of the line. 
 
     width: 1 
 
    } 
 
    }]; 
 

 
    draw(data, layout); 
 
});
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> 
 
<div id="tester" style="width:600px;height:300px;"></div> 
 
<div id="hoverinfo" style="margin-left:80px;"></div>

0

另一種解決方案是使用hoverinfo值並將其設置爲出現在參考 '無':https://plot.ly/javascript/reference/#scatter-hoverinfo

是這樣的:

var trace1 = 
{ 
    x: [1, 2, 3], 
    y: [40, 50, 60], 
    name: 'data1', 
    type: 'scatter', 
    hoverinfo: 'none' 
}; 

好處是,無人駕駛事件仍然火災。如果您改用Plotly.Fx.hover(gd, []);,則不會觸發非停留事件。

從參考複製: hoverinfo(flaglist字符串)

的 「X」 的任何組合, 「Y」, 「Z」, 「文本」, 「名稱」 接合用 「+」 或「全部」或「無」或「跳過」。 示例:「x」,「y」,「x + y」,「x + y + z」,「全部」 默認值:「全部」 確定在懸停時顯示哪些跟蹤信息。如果設置了noneskip,則懸停時不顯示任何信息。但是,如果設置了none,則點擊和懸停事件仍會觸發。