|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Plotly Tooltip Button Test</title> |
| 6 | + <script src="../dist/plotly.js"></script> |
| 7 | + <style> |
| 8 | + body { |
| 9 | + font-family: Arial, sans-serif; |
| 10 | + } |
| 11 | + .plot { |
| 12 | + width: 100%; |
| 13 | + height: 600px; |
| 14 | + margin-bottom: 50px; |
| 15 | + } |
| 16 | + </style> |
| 17 | +</head> |
| 18 | +<body> |
| 19 | + <h1>Plotly Tooltip Button Test</h1> |
| 20 | + <div id="scatter-plot" class="plot"></div> |
| 21 | + <div id="bar-plot" class="plot"></div> |
| 22 | + <div id="heatmap-plot" class="plot"></div> |
| 23 | + <script> |
| 24 | + document.addEventListener("DOMContentLoaded", function() { |
| 25 | + |
| 26 | + function generateDateTime(start, count) { |
| 27 | + let dates = []; |
| 28 | + let current = new Date(start); |
| 29 | + for (let i = 0; i < count; i++) { |
| 30 | + dates.push(new Date(current)); |
| 31 | + current.setHours(current.getHours() + 7); |
| 32 | + } |
| 33 | + return dates; |
| 34 | + } |
| 35 | + |
| 36 | + function generateRandomYValues(count) { |
| 37 | + return Array.from({ length: count }, () => Math.random() * 20); |
| 38 | + } |
| 39 | + |
| 40 | + var scatterTooltipTemplate = "%{fullData.name}<br>%{x|%H:%M:%S}<br>y: %{y:.2e}"; |
| 41 | + |
| 42 | + var scatterData = [{ |
| 43 | + name: 'Scatter Trace 1', |
| 44 | + x: generateDateTime('2025-04-01T12:00:00Z', 5), |
| 45 | + y: generateRandomYValues(5), |
| 46 | + type: 'scatter', |
| 47 | + tooltiptemplate: scatterTooltipTemplate, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'Scatter Trace 2', |
| 51 | + x: generateDateTime('2025-04-01T12:00:00Z', 5), |
| 52 | + y: generateRandomYValues(5), |
| 53 | + type: 'scatter', |
| 54 | + tooltiptemplate: scatterTooltipTemplate, |
| 55 | + }]; |
| 56 | + |
| 57 | + |
| 58 | + var barData = [{ |
| 59 | + x: ['A', 'B', 'C', 'D'], |
| 60 | + y: [10, 20, 15, 25], |
| 61 | + type: 'bar', |
| 62 | + tooltiptemplate: "%{x}<br>y: %{y}", |
| 63 | + }]; |
| 64 | + |
| 65 | + // Generate heatmap data |
| 66 | + var x = Array.from({ length: 400 }, (_, i) => i / 40); |
| 67 | + var y = Array.from({ length: 400 }, (_, i) => i / 40); |
| 68 | + var z = Array.from({ length: 400 }, (_, i) => |
| 69 | + Array.from({ length: 400 }, (_, j) => |
| 70 | + Math.sin(i / 40) * Math.cos(j / 40) |
| 71 | + ) |
| 72 | + ); |
| 73 | + |
| 74 | + var heatmapData = [{ |
| 75 | + z: z, |
| 76 | + x: x, |
| 77 | + y: y, |
| 78 | + type: 'heatmap', |
| 79 | + colorscale: 'Viridis', |
| 80 | + tooltip: { |
| 81 | + bgcolor: "rgba(255, 255, 255, 0.2)" |
| 82 | + }, |
| 83 | + tooltiptemplate: "x: %{x:.2f},<br>y: %{y:.2f},<br>z: %{z:.3f}" |
| 84 | + }]; |
| 85 | + |
| 86 | + var layout = { |
| 87 | + title: 'Custom Tooltip Example' |
| 88 | + }; |
| 89 | + |
| 90 | + var config = { |
| 91 | + editable: true, |
| 92 | + modeBarButtonsToAdd: ['tooltip', 'hoverclosest', 'hovercompare', 'togglespikelines'], |
| 93 | + displaylogo: false, |
| 94 | + displayModeBar: true, |
| 95 | + locale: 'en' |
| 96 | + }; |
| 97 | + |
| 98 | + function createPlot(id, data, layout, config) { |
| 99 | + Plotly.newPlot(id, data, layout, config).then(function() { |
| 100 | + var tooltipButton = document.querySelector('.modebar-btn[data-title="Add Tooltip to Points"]'); |
| 101 | + if (tooltipButton) { |
| 102 | + tooltipButton.addEventListener('click', function() { |
| 103 | + console.log('Tooltip button clicked'); |
| 104 | + }); |
| 105 | + } else { |
| 106 | + console.error('Tooltip button not found'); |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + createPlot('scatter-plot', scatterData, Object.assign({}, layout), config); |
| 112 | + createPlot('bar-plot', barData, Object.assign({}, Object.assign({}, layout), { xaxis: { type: 'category' } }), config); |
| 113 | + createPlot('heatmap-plot', heatmapData, Object.assign({}, layout), config); |
| 114 | + |
| 115 | + }); |
| 116 | + </script> |
| 117 | +</body> |
| 118 | +</html> |
0 commit comments