flot.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*---- placeholder1----*/
  2. $(function() {
  3. 'use strict'
  4. var sin = [],
  5. cos = [];
  6. for (var i = 0; i < 14; i += 0.1) {
  7. sin.push([i, Math.sin(i)]);
  8. cos.push([i, Math.cos(i)]);
  9. }
  10. var plot = $.plot("#placeholder1", [{
  11. data: sin
  12. }, {
  13. data: cos
  14. }], {
  15. series: {
  16. lines: {
  17. show: true
  18. }
  19. },
  20. lines: {
  21. show: true,
  22. fill: true,
  23. fillColor: {
  24. colors: [{
  25. opacity: 0.7
  26. }, {
  27. opacity: 0.7
  28. }]
  29. }
  30. },
  31. crosshair: {
  32. mode: "x"
  33. },
  34. grid: {
  35. hoverable: false,
  36. autoHighlight: false,
  37. borderColor: "rgba(119, 119, 142, 0.1)",
  38. verticalLines: false,
  39. horizontalLines: false
  40. },
  41. colors: ['#6c5ffc', '#05c3fb'],
  42. yaxis: {
  43. min: -1.2,
  44. max: 1.2,
  45. tickLength: 0
  46. },
  47. xaxis: {
  48. tickLength: 0
  49. }
  50. });
  51. });
  52. /*---- placeholder2----*/
  53. $(function() {
  54. 'use strict'
  55. var sin = [],
  56. cos = [];
  57. for (var i = 0; i < 14; i += 0.5) {
  58. sin.push([i, Math.sin(i)]);
  59. cos.push([i, Math.cos(i)]);
  60. }
  61. var plot = $.plot("#placeholder2", [{
  62. data: sin,
  63. label: "data1"
  64. }, {
  65. data: cos,
  66. label: "data2"
  67. }], {
  68. series: {
  69. lines: {
  70. show: true
  71. },
  72. points: {
  73. show: true
  74. }
  75. },
  76. grid: {
  77. hoverable: true,
  78. clickable: true,
  79. borderColor: "rgba(119, 119, 142, 0.1)",
  80. verticalLines: false,
  81. horizontalLines: false
  82. },
  83. colors: ['#6c5ffc', '#05c3fb'],
  84. yaxis: {
  85. min: -1.2,
  86. max: 1.2,
  87. tickLength: 0
  88. },
  89. xaxis: {
  90. tickLength: 0
  91. }
  92. });
  93. });
  94. /*---- placeholder4----*/
  95. $(function() {
  96. 'use strict'
  97. // We use an inline data source in the example, usually data would
  98. // be fetched from a server
  99. var data = [],
  100. totalPoints = 300;
  101. function getRandomData() {
  102. 'use strict'
  103. if (data.length > 0) data = data.slice(1);
  104. // Do a random walk
  105. while (data.length < totalPoints) {
  106. var prev = data.length > 0 ? data[data.length - 1] : 50,
  107. y = prev + Math.random() * 10 - 5;
  108. if (y < 0) {
  109. y = 0;
  110. } else if (y > 100) {
  111. y = 100;
  112. }
  113. data.push(y);
  114. }
  115. var res = [];
  116. for (var i = 0; i < data.length; ++i) {
  117. res.push([i, data[i]])
  118. }
  119. return res;
  120. }
  121. var updateInterval = 30;
  122. $("#updateInterval").val(updateInterval).change(function() {
  123. var v = $(this).val();
  124. if (v && !isNaN(+v)) {
  125. updateInterval = +v;
  126. if (updateInterval < 1) {
  127. updateInterval = 1;
  128. } else if (updateInterval > 2000) {
  129. updateInterval = 2000;
  130. }
  131. $(this).val("" + updateInterval);
  132. }
  133. });
  134. var plot = $.plot("#placeholder4", [getRandomData()], {
  135. series: {
  136. shadowSize: 0 // Drawing is faster without shadows
  137. },
  138. grid: {
  139. borderColor: "rgba(119, 119, 142, 0.1)",
  140. },
  141. colors: ["#6c5ffc"],
  142. yaxis: {
  143. min: 0,
  144. max: 100,
  145. tickLength: 0
  146. },
  147. xaxis: {
  148. tickLength: 0,
  149. show: false
  150. }
  151. });
  152. function update() {
  153. 'use strict'
  154. plot.setData([getRandomData()]);
  155. plot.draw();
  156. setTimeout(update, updateInterval);
  157. }
  158. update();
  159. });
  160. /*---- placeholder6----*/
  161. $(function() {
  162. 'use strict'
  163. var d1 = [];
  164. for (var i = 0; i <= 10; i += 1) {
  165. d1.push([i, parseInt(Math.random() * 30)]);
  166. }
  167. var d2 = [];
  168. for (var i = 0; i <= 10; i += 1) {
  169. d2.push([i, parseInt(Math.random() * 30)]);
  170. }
  171. var d3 = [];
  172. for (var i = 0; i <= 10; i += 1) {
  173. d3.push([i, parseInt(Math.random() * 30)]);
  174. }
  175. var stack = 0,
  176. bars = true,
  177. lines = false,
  178. steps = false;
  179. function plotWithOptions() {
  180. 'use strict'
  181. $.plot("#placeholder6", [d1, d2, d3], {
  182. series: {
  183. stack: stack,
  184. lines: {
  185. show: lines,
  186. fill: true,
  187. steps: steps
  188. },
  189. bars: {
  190. show: bars,
  191. barWidth: 0.6
  192. }
  193. },
  194. grid: {
  195. borderColor: "rgba(119, 119, 142, 0.1)",
  196. },
  197. colors: ['#6c5ffc', '#05c3fb'],
  198. yaxis: {
  199. tickLength: 0
  200. },
  201. xaxis: {
  202. tickLength: 0,
  203. show: false
  204. }
  205. });
  206. }
  207. plotWithOptions();
  208. $(".stackControls button").click(function(e) {
  209. e.preventDefault();
  210. stack = $(this).text() == "With stacking" ? true : null;
  211. plotWithOptions();
  212. });
  213. $(".graphControls button").click(function(e) {
  214. e.preventDefault();
  215. bars = $(this).text().indexOf("Bars") != -1;
  216. lines = $(this).text().indexOf("Lines") != -1;
  217. steps = $(this).text().indexOf("steps") != -1;
  218. plotWithOptions();
  219. });
  220. });
  221. $(function() {
  222. 'use strict'
  223. var data = [],
  224. series = Math.floor(Math.random() * 4) + 3;
  225. for (var i = 0; i < series; i++) {
  226. data[i] = {
  227. label: "Series" + (i + 1),
  228. data: Math.floor(Math.random() * 100) + 1
  229. }
  230. }
  231. var placeholder = $("#placeholder");
  232. $("#example-1").on('click', function() {
  233. placeholder.unbind();
  234. $("#title").text("Default pie chart");
  235. $("#description").text("The default pie chart with no options set.");
  236. $.plot(placeholder, data, {
  237. series: {
  238. pie: {
  239. show: true
  240. }
  241. },
  242. colors: ['#6c5ffc', '#05c3fb', '#09ad95', '#1170e4', '#f82649'],
  243. });
  244. });
  245. $("#example-2").on('click', function() {
  246. placeholder.unbind();
  247. $("#title").text("Default without legend");
  248. $("#description").text("The default pie chart when the legend is disabled. Since the labels would normally be outside the container, the chart is resized to fit.");
  249. $.plot(placeholder, data, {
  250. series: {
  251. pie: {
  252. show: true
  253. }
  254. },
  255. colors: ['#6c5ffc', '#05c3fb', '#09ad95', '#1170e4', '#f82649'],
  256. legend: {
  257. show: false
  258. }
  259. });
  260. });
  261. $("#example-3").on('click', function() {
  262. placeholder.unbind();
  263. $("#title").text("Custom Label Formatter");
  264. $("#description").text("Added a semi-transparent background to the labels and a custom labelFormatter function.");
  265. $.plot(placeholder, data, {
  266. series: {
  267. pie: {
  268. show: true,
  269. radius: 1,
  270. label: {
  271. show: true,
  272. radius: 1,
  273. formatter: labelFormatter,
  274. background: {
  275. opacity: 0.8
  276. }
  277. }
  278. }
  279. },
  280. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  281. legend: {
  282. show: false
  283. }
  284. });
  285. });
  286. $("#example-4").on('click', function() {
  287. placeholder.unbind();
  288. $("#title").text("Label Radius");
  289. $("#description").text("Slightly more transparent label backgrounds and adjusted the radius values to place them within the pie.");
  290. $.plot(placeholder, data, {
  291. series: {
  292. pie: {
  293. show: true,
  294. radius: 1,
  295. label: {
  296. show: true,
  297. radius: 3 / 4,
  298. formatter: labelFormatter,
  299. background: {
  300. opacity: 0.5
  301. }
  302. }
  303. }
  304. },
  305. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#64E572'],
  306. legend: {
  307. show: false
  308. }
  309. });
  310. });
  311. $("#example-5").on('click', function() {
  312. placeholder.unbind();
  313. $("#title").text("Label Styles #1");
  314. $("#description").text("Semi-transparent, black-colored label background.");
  315. $.plot(placeholder, data, {
  316. series: {
  317. pie: {
  318. show: true,
  319. radius: 1,
  320. label: {
  321. show: true,
  322. radius: 3 / 4,
  323. formatter: labelFormatter,
  324. background: {
  325. opacity: 0.5,
  326. color: "#000"
  327. }
  328. }
  329. }
  330. },
  331. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  332. legend: {
  333. show: false
  334. }
  335. });
  336. });
  337. $("#example-6").on('click', function() {
  338. placeholder.unbind();
  339. $("#title").text("Label Styles #2");
  340. $("#description").text("Semi-transparent, black-colored label background placed at pie edge.");
  341. $.plot(placeholder, data, {
  342. series: {
  343. pie: {
  344. show: true,
  345. radius: 3 / 4,
  346. label: {
  347. show: true,
  348. radius: 3 / 4,
  349. formatter: labelFormatter,
  350. background: {
  351. opacity: 0.5,
  352. color: "#000"
  353. }
  354. }
  355. }
  356. },
  357. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  358. legend: {
  359. show: false
  360. }
  361. });
  362. });
  363. $("#example-7").on('click', function() {
  364. placeholder.unbind();
  365. $("#title").text("Hidden Labels");
  366. $("#description").text("Labels can be hidden if the slice is less than a given percentage of the pie (10% in this case).");
  367. $.plot(placeholder, data, {
  368. series: {
  369. pie: {
  370. show: true,
  371. radius: 1,
  372. label: {
  373. show: true,
  374. radius: 2 / 3,
  375. formatter: labelFormatter,
  376. threshold: 0.1
  377. }
  378. }
  379. },
  380. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  381. legend: {
  382. show: false
  383. }
  384. });
  385. });
  386. $("#example-8").on('click', function() {
  387. placeholder.unbind();
  388. $("#title").text("Combined Slice");
  389. $("#description").text("Multiple slices less than a given percentage (5% in this case) of the pie can be combined into a single, larger slice.");
  390. $.plot(placeholder, data, {
  391. series: {
  392. pie: {
  393. show: true,
  394. combine: {
  395. color: "#999",
  396. threshold: 0.05
  397. }
  398. }
  399. },
  400. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  401. legend: {
  402. show: false
  403. }
  404. });
  405. });
  406. $("#example-9").on('click', function() {
  407. placeholder.unbind();
  408. $("#title").text("Rectangular Pie");
  409. $("#description").text("The radius can also be set to a specific size (even larger than the container itself).");
  410. $.plot(placeholder, data, {
  411. series: {
  412. pie: {
  413. show: true,
  414. radius: 500,
  415. label: {
  416. show: true,
  417. formatter: labelFormatter,
  418. threshold: 0.1
  419. }
  420. }
  421. },
  422. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  423. legend: {
  424. show: false
  425. }
  426. });
  427. });
  428. $("#example-10").on('click', function() {
  429. placeholder.unbind();
  430. $("#title").text("Tilted Pie");
  431. $("#description").text("The pie can be tilted at an angle.");
  432. $.plot(placeholder, data, {
  433. series: {
  434. pie: {
  435. show: true,
  436. radius: 1,
  437. tilt: 0.5,
  438. label: {
  439. show: true,
  440. radius: 1,
  441. formatter: labelFormatter,
  442. background: {
  443. opacity: 0.8
  444. }
  445. },
  446. combine: {
  447. color: "#999",
  448. threshold: 0.1
  449. }
  450. }
  451. },
  452. colors: ['#1170e4', '#d43f8d', '#45aaf2', '#ecb403', '#e86a91'],
  453. legend: {
  454. show: false
  455. }
  456. });
  457. });
  458. $("#example-11").on('click', function() {
  459. placeholder.unbind();
  460. $("#title").text("Donut Hole");
  461. $("#description").text("A donut hole can be added.");
  462. $.plot(placeholder, data, {
  463. series: {
  464. pie: {
  465. innerRadius: 0.5,
  466. show: true
  467. }
  468. }
  469. });
  470. });
  471. $("#example-1").click();
  472. });
  473. // A custom label formatter used by several of the plots
  474. function labelFormatter(label, series) {
  475. 'use strict'
  476. return "<div style='font-size:8pt; text-align:center; padding:2px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";
  477. }
  478. //
  479. function setCode(lines) {
  480. 'use strict'
  481. $("#code").text(lines.join("\n"));
  482. }