flot-charts.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /* Flot Charts
  2. * -------
  3. * Here we will create a few charts using Flot Charts
  4. */
  5. /*---------------------
  6. ----- PIE CHART -----
  7. ---------------------*/
  8. var data = [
  9. {data: 18000, color: '#f8bd19', label: 'Linda'},
  10. {data: 20000, color: '#e44a00', label: 'John'},
  11. {data: 30000, color: '#008ee4', label: 'David'},
  12. {data: 13000, color: '#33bdda', label: 'Margaret'},
  13. {data: 15000, color: '#6baa01', label: 'Richard'},
  14. ];
  15. $.plot("#pie-chart", data, {
  16. series: {
  17. pie: {
  18. show: true,
  19. radius: 1,
  20. label: {
  21. show: true,
  22. radius: 3/4,
  23. formatter: labelFormatter,
  24. background: {
  25. opacity: 0.5
  26. }
  27. }
  28. }
  29. },
  30. legend: {
  31. show: false
  32. }
  33. });
  34. function labelFormatter(label, series) {
  35. return "<div style='font-size:8pt; text-align:center; padding:2px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";
  36. }
  37. /*---------------------
  38. ----- LINE CHART -----
  39. ---------------------*/
  40. var d1 = [[0,30],[1,35],[2,35],[3,30],[4,30],[5,35],[6,32],[7,37],[8,30],[9,35],[10,30],[11,31]];
  41. var d2 = [[0,50],[1,40],[2,45],[3,60],[4,50],[5,50],[6,60],[7,55],[8,50],[9,50],[10,60],[11,35]];
  42. var d3 = [[0,40],[1,50],[2,35],[3,25],[4,40],[5,45],[6,55],[7,50],[8,35],[9,40],[10,48],[11,40]];
  43. /*---------------------------------------------------
  44. Make some random data for Recent Items chart
  45. ---------------------------------------------------*/
  46. var options = {
  47. series: {
  48. shadowSize: 0,
  49. lines: {
  50. show: true,
  51. },
  52. },
  53. grid: {
  54. borderWidth: 1,
  55. labelMargin:10,
  56. mouseActiveRadius:6,
  57. borderColor: '#eee',
  58. show : true,
  59. hoverable : true,
  60. clickable : true
  61. },
  62. xaxis: {
  63. tickColor: '#eee',
  64. tickDecimals: 0,
  65. font :{
  66. lineHeight: 15,
  67. style: "normal",
  68. color: "#000"
  69. },
  70. shadowSize: 0,
  71. ticks: [[0,"Jan"], [1,"Feb"], [2,"Mar"], [3,"Apr"], [4,"May"], [5,"Jun"], [6,"Jul"], [7,"Aug"], [8,"Sep"], [9,"Oct"], [10,"Nov"], [11,"Dec"]]
  72. },
  73. yaxis: {
  74. tickColor: '#eee',
  75. tickDecimals: 0,
  76. font :{
  77. lineHeight: 15,
  78. style: "normal",
  79. color: "#000",
  80. },
  81. shadowSize: 0
  82. },
  83. legend: {
  84. container: '.flc-line',
  85. backgroundOpacity: 0.5,
  86. noColumns: 0,
  87. backgroundColor: "white",
  88. lineWidth: 0
  89. },
  90. colors: ["#EB5E28", "#F3BB45", "#68B3C8"]
  91. };
  92. if ($("#line-chart")[0]) {
  93. $.plot($("#line-chart"), [
  94. {data: d1, lines: { show: true }, label: 'Product A', stack: true, color: '#EB5E28' },
  95. {data: d2, lines: { show: true }, label: 'Product B', stack: true, color: '#F3BB45' },
  96. {data: d3, lines: { show: true }, label: 'Product C', stack: true, color: '#68B3C8' }
  97. ], options);
  98. }
  99. /*---------------------------------
  100. Tooltips for Flot Charts
  101. ---------------------------------*/
  102. if ($(".flot-chart-line")[0]) {
  103. $(".flot-chart-line").bind("plothover", function (event, pos, item) {
  104. if (item) {
  105. var x = item.datapoint[0].toFixed(2),
  106. y = item.datapoint[1].toFixed(2);
  107. $(".flot-tooltip").html(item.series.label + " Sales " + " : " + y).css({top: item.pageY+5, left: item.pageX+5}).show();
  108. }
  109. else {
  110. $(".flot-tooltip").hide();
  111. }
  112. });
  113. $("<div class='flot-tooltip' class='chart-tooltip'></div>").appendTo("body");
  114. }
  115. /*---------------------
  116. ----- AREA CHART -----
  117. ---------------------*/
  118. var d1 = [[0,0],[1,35],[2,35],[3,30],[4,30],[5,5],[6,32],[7,37],[8,30],[9,35],[10,30],[11,5]];
  119. var options = {
  120. series: {
  121. shadowSize: 0,
  122. curvedLines: { //This is a third party plugin to make curved lines
  123. apply: true,
  124. active: true,
  125. monotonicFit: true
  126. },
  127. lines: {
  128. show: false,
  129. fill:0.98,
  130. lineWidth: 0,
  131. },
  132. },
  133. grid: {
  134. borderWidth: 0,
  135. labelMargin:10,
  136. hoverable: true,
  137. clickable: true,
  138. mouseActiveRadius:6,
  139. },
  140. xaxis: {
  141. tickDecimals: 0,
  142. ticks: false
  143. },
  144. yaxis: {
  145. tickDecimals: 0,
  146. ticks: false
  147. },
  148. legend: {
  149. show: false
  150. }
  151. };
  152. if ($("#area-chart")[0]) {
  153. $.plot($("#area-chart"), [
  154. {data: d1, lines: { show: true, fill: 0.6 }, label: 'Product 1', stack: true, color: '#3F51B5' }
  155. ], options);
  156. }
  157. /*---------------------
  158. ----- COLUMN CHART -----
  159. ---------------------*/
  160. $(function() {
  161. var data = [ ["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9] ];
  162. $.plot("#column-chart", [ data ], {
  163. series: {
  164. bars: {
  165. show: true,
  166. barWidth: 0.6,
  167. align: "center"
  168. }
  169. },
  170. xaxis: {
  171. mode: "categories",
  172. tickLength: 0
  173. },
  174. grid: {
  175. borderWidth: 0,
  176. labelMargin:10,
  177. hoverable: true,
  178. clickable: true,
  179. mouseActiveRadius:6,
  180. }
  181. });
  182. });
  183. /*--------------------------------
  184. ----- STACKED CHART -----
  185. --------------------------------*/
  186. $(function() {
  187. var d1 = [];
  188. for (var i = 0; i <= 10; i += 1) {
  189. d1.push([i, parseInt(Math.random() * 30)]);
  190. }
  191. var d2 = [];
  192. for (var i = 0; i <= 10; i += 1) {
  193. d2.push([i, parseInt(Math.random() * 30)]);
  194. }
  195. var d3 = [];
  196. for (var i = 0; i <= 10; i += 1) {
  197. d3.push([i, parseInt(Math.random() * 30)]);
  198. }
  199. $.plot("#stacked-bar-chart", [ d1, d2, d3 ], {
  200. series: {
  201. stack: 0,
  202. lines: {
  203. show: false,
  204. fill: true,
  205. steps: false
  206. },
  207. bars: {
  208. show: true,
  209. barWidth: 0.6
  210. },
  211. },
  212. grid: {
  213. borderWidth: 0,
  214. labelMargin:10,
  215. hoverable: true,
  216. clickable: true,
  217. mouseActiveRadius:6,
  218. }
  219. });
  220. });
  221. /*--------------------------------
  222. ----- REALTIME CHART -----
  223. --------------------------------*/
  224. $(function() {
  225. // We use an inline data source in the example, usually data would
  226. // be fetched from a server
  227. var data = [],
  228. totalPoints = 300;
  229. function getRandomData() {
  230. if (data.length > 0)
  231. data = data.slice(1);
  232. // Do a random walk
  233. while (data.length < totalPoints) {
  234. var prev = data.length > 0 ? data[data.length - 1] : 50,
  235. y = prev + Math.random() * 10 - 5;
  236. if (y < 0) {
  237. y = 0;
  238. } else if (y > 100) {
  239. y = 100;
  240. }
  241. data.push(y);
  242. }
  243. // Zip the generated y values with the x values
  244. var res = [];
  245. for (var i = 0; i < data.length; ++i) {
  246. res.push([i, data[i]])
  247. }
  248. return res;
  249. }
  250. // Set up the control widget
  251. var updateInterval = 30;
  252. var plot = $.plot("#realtime-chart", [ getRandomData() ], {
  253. series: {
  254. shadowSize: 0 // Drawing is faster without shadows
  255. },
  256. yaxis: {
  257. min: 0,
  258. max: 100
  259. },
  260. xaxis: {
  261. show: false
  262. },
  263. grid: {
  264. borderWidth: 0,
  265. labelMargin:10,
  266. hoverable: true,
  267. clickable: true,
  268. mouseActiveRadius:6,
  269. }
  270. });
  271. function update() {
  272. plot.setData([getRandomData()]);
  273. // Since the axes don't change, we don't need to call plot.setupGrid()
  274. plot.draw();
  275. setTimeout(update, updateInterval);
  276. }
  277. update();
  278. });