flot-demo-1.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. $(function() {
  2. //Interacting with Data Points example
  3. var sin = [], cos = [];
  4. for (var i = 0; i < 354; i += 31) {
  5. sin.push([i, Math.random(i)]);
  6. cos.push([i, Math.random(i)]);
  7. }
  8. var plot = $.plot($("#data-example-1"),
  9. [{ data: sin, label: "Today" }, { data: cos, label: "Yesterday" }], {
  10. series: {
  11. shadowSize: 0,
  12. lines: {
  13. show: true,
  14. lineWidth: 2
  15. },
  16. points: { show: true }
  17. },
  18. grid: {
  19. labelMargin: 10,
  20. hoverable: true,
  21. clickable: true,
  22. borderWidth: 1,
  23. borderColor: 'rgba(82, 167, 224, 0.06)'
  24. },
  25. legend: {
  26. backgroundColor: '#fff'
  27. },
  28. yaxis: { tickColor: 'rgba(0, 0, 0, 0.06)', font: {color: 'rgba(0, 0, 0, 0.4)'}},
  29. xaxis: { tickColor: 'rgba(0, 0, 0, 0.06)', font: {color: 'rgba(0, 0, 0, 0.4)'}},
  30. colors: [getUIColor('success'), getUIColor('gray')],
  31. tooltip: true,
  32. tooltipOpts: {
  33. content: "x: %x, y: %y"
  34. }
  35. });
  36. var previousPoint = null;
  37. $("#data-example-1").bind("plothover", function (event, pos, item) {
  38. $("#x").text(pos.x.toFixed(2));
  39. $("#y").text(pos.y.toFixed(2));
  40. });
  41. $("#data-example-1").bind("plotclick", function (event, pos, item) {
  42. if (item) {
  43. $("#clickdata").text("You clicked point " + item.dataIndex + " in " + item.series.label + ".");
  44. plot.highlight(item.series, item.datapoint);
  45. }
  46. });
  47. });
  48. $(function() {
  49. // We use an inline data source in the example, usually data would
  50. // be fetched from a server
  51. var data = [],
  52. totalPoints = 300;
  53. function getRandomData() {
  54. if (data.length > 0)
  55. data = data.slice(1);
  56. // Do a random walk
  57. while (data.length < totalPoints) {
  58. var prev = data.length > 0 ? data[data.length - 1] : 50,
  59. y = prev + Math.random() * 10 - 5;
  60. if (y < 0) {
  61. y = 0;
  62. } else if (y > 100) {
  63. y = 100;
  64. }
  65. data.push(y);
  66. }
  67. // Zip the generated y values with the x values
  68. var res = [];
  69. for (var i = 0; i < data.length; ++i) {
  70. res.push([i, data[i]])
  71. }
  72. return res;
  73. }
  74. // Set up the control widget
  75. var updateInterval = 30;
  76. var plot = $.plot("#data-example-3", [ getRandomData() ], {
  77. series: {
  78. lines: {
  79. show: true,
  80. lineWidth: 2,
  81. fill: 0.5,
  82. fillColor: { colors: [ { opacity: 0.01 }, { opacity: 0.08 } ] }
  83. },
  84. shadowSize: 0 // Drawing is faster without shadows
  85. },
  86. grid: {
  87. labelMargin: 10,
  88. hoverable: true,
  89. clickable: true,
  90. borderWidth: 1,
  91. borderColor: 'rgba(82, 167, 224, 0.06)'
  92. },
  93. yaxis: {
  94. min: 0,
  95. max: 120,
  96. tickColor: 'rgba(0, 0, 0, 0.06)', font: {color: 'rgba(0, 0, 0, 0.4)'}},
  97. xaxis: { show: false },
  98. colors: [getUIColor('default'),getUIColor('gray')]
  99. });
  100. function update() {
  101. plot.setData([getRandomData()]);
  102. // Since the axes don't change, we don't need to call plot.setupGrid()
  103. plot.draw();
  104. setTimeout(update, updateInterval);
  105. }
  106. update();
  107. });