Guest User

Untitled

a guest
Dec 11th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. $(function () {
  3.     var sin = [], cos = [];
  4.     for (var i = 0; i < 14; i += 0.5) {
  5.         sin.push([i, Math.sin(i)]);
  6.         cos.push([i, Math.cos(i)]);
  7.     }
  8.  
  9.     var plot = $.plot($("#placeholder"),
  10.            [ { data: sin, label: "sin(x)"}, { data: cos, label: "cos(x)" } ], {
  11.                series: {
  12.                    lines: { show: true },
  13.                    points: { show: true }
  14.                },
  15.                grid: { hoverable: true, clickable: true },
  16.                yaxis: { min: -1.2, max: 1.2 }
  17.              });
  18.  
  19.     function showTooltip(x, y, contents) {
  20.         $('<div id="tooltip">' + contents + '</div>').css( {
  21.             position: 'absolute',
  22.             display: 'none',
  23.             top: y + 5,
  24.             left: x + 5,
  25.             border: '1px solid #fdd',
  26.             padding: '2px',
  27.             'background-color': '#fee',
  28.             opacity: 0.80
  29.         }).appendTo("body").fadeIn(200);
  30.     }
  31.  
  32.     var previousPoint = null;
  33.     $("#placeholder").bind("plothover", function (event, pos, item) {
  34.         $("#x").text(pos.x.toFixed(2));
  35.         $("#y").text(pos.y.toFixed(2));
  36.  
  37.         if ($("#enableTooltip:checked").length > 0) {
  38.             if (item) {
  39.                 if (previousPoint != item.dataIndex) {
  40.                     previousPoint = item.dataIndex;
  41.                    
  42.                     $("#tooltip").remove();
  43.                     var x = item.datapoint[0].toFixed(2),
  44.                         y = item.datapoint[1].toFixed(2);
  45.                    
  46.                     showTooltip(item.pageX, item.pageY,
  47.                                 item.series.label + " of " + x + " = " + y);
  48.                 }
  49.             }
  50.             else {
  51.                 $("#tooltip").remove();
  52.                 previousPoint = null;            
  53.             }
  54.         }
  55.     });
  56.  
  57.     $("#placeholder").bind("plotclick", function (event, pos, item) {
  58.         if (item) {
  59.             $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
  60.             plot.highlight(item.series, item.datapoint);
  61.         }
  62.     });
  63. });
  64. </script>
Add Comment
Please, Sign In to add comment