flot-demo.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. var d1 = [];
  50. for (var i = 0; i <= 10; i += 1) {
  51. d1.push([i, parseInt(Math.random() * 30)]);
  52. }
  53. var d2 = [];
  54. for (var i = 0; i <= 10; i += 1) {
  55. d2.push([i, parseInt(Math.random() * 30)]);
  56. }
  57. var d3 = [];
  58. for (var i = 0; i <= 10; i += 1) {
  59. d3.push([i, parseInt(Math.random() * 30)]);
  60. }
  61. var d4 = [];
  62. for (var i = 0; i <= 10; i += 1) {
  63. d4.push([i, parseInt(Math.random() * 30)]);
  64. }
  65. var stack = 0,
  66. bars = true,
  67. lines = false,
  68. steps = false;
  69. function plotWithOptions() {
  70. $.plot("#data-example-2", [ d1, d2, d3, d4 ], {
  71. series: {
  72. shadowSize: 0,
  73. stack: stack,
  74. lines: {
  75. show: lines,
  76. lineWidth: 1,
  77. },
  78. bars: {
  79. show: bars,
  80. lineWidth: 1,
  81. }
  82. },
  83. grid: {
  84. labelMargin: 10,
  85. borderWidth: 0
  86. },
  87. legend: {
  88. backgroundColor: '#fff'
  89. },
  90. yaxis: { tickColor: 'rgba(0, 0, 0, 0.06)', font: {color: 'rgba(0, 0, 0, 0.4)'}},
  91. xaxis: { tickColor: 'rgba(0, 0, 0, 0.06)', font: {color: 'rgba(0, 0, 0, 0.4)'}},
  92. colors: [getUIColor('default'),getUIColor('warning'),getUIColor('danger'),getUIColor('primary')],
  93. tooltip: true,
  94. tooltipOpts: {
  95. content: "x: %x, y: %y"
  96. }
  97. });
  98. }
  99. plotWithOptions();
  100. $(".stackControls button").click(function (e) {
  101. e.preventDefault();
  102. stack = $(this).text() == "With stacking" ? true : null;
  103. plotWithOptions();
  104. });
  105. $(".graphControls button").click(function (e) {
  106. e.preventDefault();
  107. bars = $(this).text().indexOf("Bars") != -1;
  108. lines = $(this).text().indexOf("Lines") != -1;
  109. steps = $(this).text().indexOf("steps") != -1;
  110. plotWithOptions();
  111. });
  112. });
  113. $(function() {
  114. // We use an inline data source in the example, usually data would
  115. // be fetched from a server
  116. var data = [],
  117. totalPoints = 300;
  118. function getRandomData() {
  119. if (data.length > 0)
  120. data = data.slice(1);
  121. // Do a random walk
  122. while (data.length < totalPoints) {
  123. var prev = data.length > 0 ? data[data.length - 1] : 50,
  124. y = prev + Math.random() * 10 - 5;
  125. if (y < 0) {
  126. y = 0;
  127. } else if (y > 100) {
  128. y = 100;
  129. }
  130. data.push(y);
  131. }
  132. // Zip the generated y values with the x values
  133. var res = [];
  134. for (var i = 0; i < data.length; ++i) {
  135. res.push([i, data[i]])
  136. }
  137. return res;
  138. }
  139. // Set up the control widget
  140. var updateInterval = 30;
  141. var plot = $.plot("#data-example-3", [ getRandomData() ], {
  142. series: {
  143. lines: {
  144. show: true,
  145. lineWidth: 2,
  146. fill: 0.5,
  147. fillColor: { colors: [ { opacity: 0.01 }, { opacity: 0.08 } ] }
  148. },
  149. shadowSize: 0 // Drawing is faster without shadows
  150. },
  151. grid: {
  152. labelMargin: 10,
  153. hoverable: true,
  154. clickable: true,
  155. borderWidth: 1,
  156. borderColor: 'rgba(82, 167, 224, 0.06)'
  157. },
  158. yaxis: {
  159. min: 0,
  160. max: 150,
  161. tickColor: 'rgba(0, 0, 0, 0.06)', font: {color: 'rgba(0, 0, 0, 0.4)'}},
  162. xaxis: { show: false },
  163. colors: [getUIColor('default'),getUIColor('gray')]
  164. });
  165. function update() {
  166. plot.setData([getRandomData()]);
  167. // Since the axes don't change, we don't need to call plot.setupGrid()
  168. plot.draw();
  169. setTimeout(update, updateInterval);
  170. }
  171. update();
  172. });
  173. $(function() {
  174. // Randomly Generated Data
  175. var dataSet = [
  176. {label: "Asia", data: 1119630000, color: getUIColor('info') },
  177. { label: "Latin America", data: 690950000, color: getUIColor('warning') },
  178. { label: "Africa", data: 1012960000, color: getUIColor('danger') },
  179. { label: "Oceania", data: 5100000, color: getUIColor('gray') },
  180. { label: "Europe", data: 727080000, color: getUIColor('primary') },
  181. { label: "North America", data: 344120000, color: getUIColor('success') }
  182. ];
  183. var data = [],
  184. series = Math.floor(Math.random() * 5) + 3;
  185. for (var i = 0; i < series; i++) {
  186. data[i] = {
  187. label: "Series" + (i + 1),
  188. data: Math.floor(Math.random() * 100) + 1
  189. }
  190. }
  191. $.plot('#data-donut-1', dataSet, {
  192. series: {
  193. pie: {
  194. innerRadius: 0.5,
  195. show: true,
  196. },
  197. }
  198. });
  199. $.plot('#data-donut-2', dataSet, {
  200. series: {
  201. pie: {
  202. show: true
  203. },
  204. },
  205. tooltip: true,
  206. tooltipOpts: {
  207. content: "%p.0%, %s"
  208. },
  209. grid: {
  210. hoverable: true,
  211. clickable: true
  212. }
  213. });
  214. $.plot('#data-donut-3', dataSet, {
  215. series: {
  216. pie: {
  217. show: true,
  218. radius: 500,
  219. label: {
  220. show: true,
  221. formatter: labelFormatter,
  222. threshold: 0.1
  223. }
  224. },
  225. },
  226. legend: {
  227. show: false
  228. }
  229. });
  230. function labelFormatter(label, series) {
  231. return "<div style='font-size:12px; text-align:center; padding:5px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";
  232. }
  233. });