You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chart_side.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. $(function () {
  2. var sin = [], cos = [];
  3. for (var i = 0; i < 9; i += 0.8) {
  4. sin.push([i, Math.sin(i)]);
  5. cos.push([i, Math.cos(i)]);
  6. }
  7. var plot = $.plot($(".chartS"),
  8. [ { data: sin, label: "sin(x)"}, { data: cos, label: "cos(x)" } ], {
  9. series: {
  10. lines: { show: true },
  11. points: { show: true }
  12. },
  13. grid: { hoverable: true, clickable: true },
  14. yaxis: { min: -1.1, max: 1.1 },
  15. xaxis: { min: 0, max: 8 }
  16. });
  17. function showTooltip(x, y, contents) {
  18. $('<div id="tooltip" class="tooltip">' + contents + '</div>').css( {
  19. position: 'absolute',
  20. display: 'none',
  21. top: y + 5,
  22. left: x + 5,
  23. 'z-index': '9999',
  24. 'color': '#fff',
  25. 'font-size': '11px',
  26. opacity: 0.8
  27. }).appendTo("body").fadeIn(200);
  28. }
  29. var previousPoint = null;
  30. $(".chartS").bind("plothover", function (event, pos, item) {
  31. $("#x").text(pos.x.toFixed(2));
  32. $("#y").text(pos.y.toFixed(2));
  33. if ($(".chartS").length > 0) {
  34. if (item) {
  35. if (previousPoint != item.dataIndex) {
  36. previousPoint = item.dataIndex;
  37. $("#tooltip").remove();
  38. var x = item.datapoint[0].toFixed(2),
  39. y = item.datapoint[1].toFixed(2);
  40. showTooltip(item.pageX, item.pageY,
  41. item.series.label + " of " + x + " = " + y);
  42. }
  43. }
  44. else {
  45. $("#tooltip").remove();
  46. previousPoint = null;
  47. }
  48. }
  49. });
  50. $(".chartS").bind("plotclick", function (event, pos, item) {
  51. if (item) {
  52. $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
  53. plot.highlight(item.series, item.datapoint);
  54. }
  55. });
  56. });