charts.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. var Charts = function () {
  2. return {
  3. //main function to initiate the module
  4. init: function () {
  5. App.addResponsiveHandler(function () {
  6. Charts.initPieCharts();
  7. });
  8. },
  9. initCharts: function () {
  10. if (!jQuery.plot) {
  11. return;
  12. }
  13. var data = [];
  14. var totalPoints = 250;
  15. // random data generator for plot charts
  16. function getRandomData() {
  17. if (data.length > 0) data = data.slice(1);
  18. // do a random walk
  19. while (data.length < totalPoints) {
  20. var prev = data.length > 0 ? data[data.length - 1] : 50;
  21. var y = prev + Math.random() * 10 - 5;
  22. if (y < 0) y = 0;
  23. if (y > 100) y = 100;
  24. data.push(y);
  25. }
  26. // zip the generated y values with the x values
  27. var res = [];
  28. for (var i = 0; i < data.length; ++i) res.push([i, data[i]])
  29. return res;
  30. }
  31. //Basic Chart
  32. function chart1() {
  33. var d1 = [];
  34. for (var i = 0; i < Math.PI * 2; i += 0.25)
  35. d1.push([i, Math.sin(i)]);
  36. var d2 = [];
  37. for (var i = 0; i < Math.PI * 2; i += 0.25)
  38. d2.push([i, Math.cos(i)]);
  39. var d3 = [];
  40. for (var i = 0; i < Math.PI * 2; i += 0.1)
  41. d3.push([i, Math.tan(i)]);
  42. $.plot($("#chart_1"), [{
  43. label: "sin(x)",
  44. data: d1
  45. }, {
  46. label: "cos(x)",
  47. data: d2
  48. }, {
  49. label: "tan(x)",
  50. data: d3
  51. }
  52. ], {
  53. series: {
  54. lines: {
  55. show: true
  56. },
  57. points: {
  58. show: true
  59. }
  60. },
  61. xaxis: {
  62. ticks: [0, [Math.PI / 2, "\u03c0/2"],
  63. [Math.PI, "\u03c0"],
  64. [Math.PI * 3 / 2, "3\u03c0/2"],
  65. [Math.PI * 2, "2\u03c0"]
  66. ]
  67. },
  68. yaxis: {
  69. ticks: 10,
  70. min: -2,
  71. max: 2
  72. },
  73. grid: {
  74. backgroundColor: {
  75. colors: ["#fff", "#eee"]
  76. }
  77. }
  78. });
  79. }
  80. //Interactive Chart
  81. function chart2() {
  82. function randValue() {
  83. return (Math.floor(Math.random() * (1 + 40 - 20))) + 20;
  84. }
  85. var pageviews = [
  86. [1, randValue()],
  87. [2, randValue()],
  88. [3, 2 + randValue()],
  89. [4, 3 + randValue()],
  90. [5, 5 + randValue()],
  91. [6, 10 + randValue()],
  92. [7, 15 + randValue()],
  93. [8, 20 + randValue()],
  94. [9, 25 + randValue()],
  95. [10, 30 + randValue()],
  96. [11, 35 + randValue()],
  97. [12, 25 + randValue()],
  98. [13, 15 + randValue()],
  99. [14, 20 + randValue()],
  100. [15, 45 + randValue()],
  101. [16, 50 + randValue()],
  102. [17, 65 + randValue()],
  103. [18, 70 + randValue()],
  104. [19, 85 + randValue()],
  105. [20, 80 + randValue()],
  106. [21, 75 + randValue()],
  107. [22, 80 + randValue()],
  108. [23, 75 + randValue()],
  109. [24, 70 + randValue()],
  110. [25, 65 + randValue()],
  111. [26, 75 + randValue()],
  112. [27, 80 + randValue()],
  113. [28, 85 + randValue()],
  114. [29, 90 + randValue()],
  115. [30, 95 + randValue()]
  116. ];
  117. var visitors = [
  118. [1, randValue() - 5],
  119. [2, randValue() - 5],
  120. [3, randValue() - 5],
  121. [4, 6 + randValue()],
  122. [5, 5 + randValue()],
  123. [6, 20 + randValue()],
  124. [7, 25 + randValue()],
  125. [8, 36 + randValue()],
  126. [9, 26 + randValue()],
  127. [10, 38 + randValue()],
  128. [11, 39 + randValue()],
  129. [12, 50 + randValue()],
  130. [13, 51 + randValue()],
  131. [14, 12 + randValue()],
  132. [15, 13 + randValue()],
  133. [16, 14 + randValue()],
  134. [17, 15 + randValue()],
  135. [18, 15 + randValue()],
  136. [19, 16 + randValue()],
  137. [20, 17 + randValue()],
  138. [21, 18 + randValue()],
  139. [22, 19 + randValue()],
  140. [23, 20 + randValue()],
  141. [24, 21 + randValue()],
  142. [25, 14 + randValue()],
  143. [26, 24 + randValue()],
  144. [27, 25 + randValue()],
  145. [28, 26 + randValue()],
  146. [29, 27 + randValue()],
  147. [30, 31 + randValue()]
  148. ];
  149. var plot = $.plot($("#chart_2"), [{
  150. data: pageviews,
  151. label: "Unique Visits"
  152. }, {
  153. data: visitors,
  154. label: "Page Views"
  155. }
  156. ], {
  157. series: {
  158. lines: {
  159. show: true,
  160. lineWidth: 2,
  161. fill: true,
  162. fillColor: {
  163. colors: [{
  164. opacity: 0.05
  165. }, {
  166. opacity: 0.01
  167. }
  168. ]
  169. }
  170. },
  171. points: {
  172. show: true
  173. },
  174. shadowSize: 2
  175. },
  176. grid: {
  177. hoverable: true,
  178. clickable: true,
  179. tickColor: "#eee",
  180. borderWidth: 0
  181. },
  182. colors: ["#d12610", "#37b7f3", "#52e136"],
  183. xaxis: {
  184. ticks: 11,
  185. tickDecimals: 0
  186. },
  187. yaxis: {
  188. ticks: 11,
  189. tickDecimals: 0
  190. }
  191. });
  192. function showTooltip(x, y, contents) {
  193. $('<div id="tooltip">' + contents + '</div>').css({
  194. position: 'absolute',
  195. display: 'none',
  196. top: y + 5,
  197. left: x + 15,
  198. border: '1px solid #333',
  199. padding: '4px',
  200. color: '#fff',
  201. 'border-radius': '3px',
  202. 'background-color': '#333',
  203. opacity: 0.80
  204. }).appendTo("body").fadeIn(200);
  205. }
  206. var previousPoint = null;
  207. $("#chart_2").bind("plothover", function (event, pos, item) {
  208. $("#x").text(pos.x.toFixed(2));
  209. $("#y").text(pos.y.toFixed(2));
  210. if (item) {
  211. if (previousPoint != item.dataIndex) {
  212. previousPoint = item.dataIndex;
  213. $("#tooltip").remove();
  214. var x = item.datapoint[0].toFixed(2),
  215. y = item.datapoint[1].toFixed(2);
  216. showTooltip(item.pageX, item.pageY, item.series.label + " of " + x + " = " + y);
  217. }
  218. } else {
  219. $("#tooltip").remove();
  220. previousPoint = null;
  221. }
  222. });
  223. }
  224. //Tracking Curves
  225. function chart3() {
  226. //tracking curves:
  227. var sin = [],
  228. cos = [];
  229. for (var i = 0; i < 14; i += 0.1) {
  230. sin.push([i, Math.sin(i)]);
  231. cos.push([i, Math.cos(i)]);
  232. }
  233. plot = $.plot($("#chart_3"), [{
  234. data: sin,
  235. label: "sin(x) = -0.00"
  236. }, {
  237. data: cos,
  238. label: "cos(x) = -0.00"
  239. }
  240. ], {
  241. series: {
  242. lines: {
  243. show: true
  244. }
  245. },
  246. crosshair: {
  247. mode: "x"
  248. },
  249. grid: {
  250. hoverable: true,
  251. autoHighlight: false
  252. },
  253. yaxis: {
  254. min: -1.2,
  255. max: 1.2
  256. }
  257. });
  258. var legends = $("#chart_3 .legendLabel");
  259. legends.each(function () {
  260. // fix the widths so they don't jump around
  261. $(this).css('width', $(this).width());
  262. });
  263. var updateLegendTimeout = null;
  264. var latestPosition = null;
  265. function updateLegend() {
  266. updateLegendTimeout = null;
  267. var pos = latestPosition;
  268. var axes = plot.getAxes();
  269. if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max || pos.y < axes.yaxis.min || pos.y > axes.yaxis.max) return;
  270. var i, j, dataset = plot.getData();
  271. for (i = 0; i < dataset.length; ++i) {
  272. var series = dataset[i];
  273. // find the nearest points, x-wise
  274. for (j = 0; j < series.data.length; ++j)
  275. if (series.data[j][0] > pos.x) break;
  276. // now interpolate
  277. var y, p1 = series.data[j - 1],
  278. p2 = series.data[j];
  279. if (p1 == null) y = p2[1];
  280. else if (p2 == null) y = p1[1];
  281. else y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
  282. legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2)));
  283. }
  284. }
  285. $("#chart_3").bind("plothover", function (event, pos, item) {
  286. latestPosition = pos;
  287. if (!updateLegendTimeout) updateLegendTimeout = setTimeout(updateLegend, 50);
  288. });
  289. }
  290. //Dynamic Chart
  291. function chart4() {
  292. //server load
  293. var options = {
  294. series: {
  295. shadowSize: 1
  296. },
  297. lines: {
  298. show: true,
  299. lineWidth: 0.5,
  300. fill: true,
  301. fillColor: {
  302. colors: [{
  303. opacity: 0.1
  304. }, {
  305. opacity: 1
  306. }
  307. ]
  308. }
  309. },
  310. yaxis: {
  311. min: 0,
  312. max: 100,
  313. tickFormatter: function (v) {
  314. return v + "%";
  315. }
  316. },
  317. xaxis: {
  318. show: false
  319. },
  320. colors: ["#6ef146"],
  321. grid: {
  322. tickColor: "#a8a3a3",
  323. borderWidth: 0
  324. }
  325. };
  326. var updateInterval = 30;
  327. var plot = $.plot($("#chart_4"), [getRandomData()], options);
  328. function update() {
  329. plot.setData([getRandomData()]);
  330. plot.draw();
  331. setTimeout(update, updateInterval);
  332. }
  333. update();
  334. }
  335. //bars with controls
  336. function chart5() {
  337. var d1 = [];
  338. for (var i = 0; i <= 10; i += 1)
  339. d1.push([i, parseInt(Math.random() * 30)]);
  340. var d2 = [];
  341. for (var i = 0; i <= 10; i += 1)
  342. d2.push([i, parseInt(Math.random() * 30)]);
  343. var d3 = [];
  344. for (var i = 0; i <= 10; i += 1)
  345. d3.push([i, parseInt(Math.random() * 30)]);
  346. var stack = 0,
  347. bars = true,
  348. lines = false,
  349. steps = false;
  350. function plotWithOptions() {
  351. $.plot($("#chart_5"), [d1, d2, d3], {
  352. series: {
  353. stack: stack,
  354. lines: {
  355. show: lines,
  356. fill: true,
  357. steps: steps
  358. },
  359. bars: {
  360. show: bars,
  361. barWidth: 0.6
  362. }
  363. }
  364. });
  365. }
  366. $(".stackControls input").click(function (e) {
  367. e.preventDefault();
  368. stack = $(this).val() == "With stacking" ? true : null;
  369. plotWithOptions();
  370. });
  371. $(".graphControls input").click(function (e) {
  372. e.preventDefault();
  373. bars = $(this).val().indexOf("Bars") != -1;
  374. lines = $(this).val().indexOf("Lines") != -1;
  375. steps = $(this).val().indexOf("steps") != -1;
  376. plotWithOptions();
  377. });
  378. plotWithOptions();
  379. }
  380. //graph
  381. chart1();
  382. chart2();
  383. chart3();
  384. chart4();
  385. chart5();
  386. },
  387. initPieCharts: function () {
  388. var data = [];
  389. var series = Math.floor(Math.random() * 10) + 1;
  390. series = series < 5 ? 5 : series;
  391. for (var i = 0; i < series; i++) {
  392. data[i] = {
  393. label: "Series" + (i + 1),
  394. data: Math.floor(Math.random() * 100) + 1
  395. }
  396. }
  397. // DEFAULT
  398. $.plot($("#pie_chart"), data, {
  399. series: {
  400. pie: {
  401. show: true
  402. }
  403. }
  404. });
  405. // GRAPH 1
  406. $.plot($("#pie_chart_1"), data, {
  407. series: {
  408. pie: {
  409. show: true
  410. }
  411. },
  412. legend: {
  413. show: false
  414. }
  415. });
  416. // GRAPH 2
  417. $.plot($("#pie_chart_2"), data, {
  418. series: {
  419. pie: {
  420. show: true,
  421. radius: 1,
  422. label: {
  423. show: true,
  424. radius: 1,
  425. formatter: function (label, series) {
  426. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  427. },
  428. background: {
  429. opacity: 0.8
  430. }
  431. }
  432. }
  433. },
  434. legend: {
  435. show: false
  436. }
  437. });
  438. // GRAPH 3
  439. $.plot($("#pie_chart_3"), data, {
  440. series: {
  441. pie: {
  442. show: true,
  443. radius: 1,
  444. label: {
  445. show: true,
  446. radius: 3 / 4,
  447. formatter: function (label, series) {
  448. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  449. },
  450. background: {
  451. opacity: 0.5
  452. }
  453. }
  454. }
  455. },
  456. legend: {
  457. show: false
  458. }
  459. });
  460. // GRAPH 4
  461. $.plot($("#pie_chart_4"), data, {
  462. series: {
  463. pie: {
  464. show: true,
  465. radius: 1,
  466. label: {
  467. show: true,
  468. radius: 3 / 4,
  469. formatter: function (label, series) {
  470. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  471. },
  472. background: {
  473. opacity: 0.5,
  474. color: '#000'
  475. }
  476. }
  477. }
  478. },
  479. legend: {
  480. show: false
  481. }
  482. });
  483. // GRAPH 5
  484. $.plot($("#pie_chart_5"), data, {
  485. series: {
  486. pie: {
  487. show: true,
  488. radius: 3 / 4,
  489. label: {
  490. show: true,
  491. radius: 3 / 4,
  492. formatter: function (label, series) {
  493. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  494. },
  495. background: {
  496. opacity: 0.5,
  497. color: '#000'
  498. }
  499. }
  500. }
  501. },
  502. legend: {
  503. show: false
  504. }
  505. });
  506. // GRAPH 6
  507. $.plot($("#pie_chart_6"), data, {
  508. series: {
  509. pie: {
  510. show: true,
  511. radius: 1,
  512. label: {
  513. show: true,
  514. radius: 2 / 3,
  515. formatter: function (label, series) {
  516. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  517. },
  518. threshold: 0.1
  519. }
  520. }
  521. },
  522. legend: {
  523. show: false
  524. }
  525. });
  526. // GRAPH 7
  527. $.plot($("#pie_chart_7"), data, {
  528. series: {
  529. pie: {
  530. show: true,
  531. combine: {
  532. color: '#999',
  533. threshold: 0.1
  534. }
  535. }
  536. },
  537. legend: {
  538. show: false
  539. }
  540. });
  541. // GRAPH 8
  542. $.plot($("#pie_chart_8"), data, {
  543. series: {
  544. pie: {
  545. show: true,
  546. radius: 300,
  547. label: {
  548. show: true,
  549. formatter: function (label, series) {
  550. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  551. },
  552. threshold: 0.1
  553. }
  554. }
  555. },
  556. legend: {
  557. show: false
  558. }
  559. });
  560. // GRAPH 9
  561. $.plot($("#pie_chart_9"), data, {
  562. series: {
  563. pie: {
  564. show: true,
  565. radius: 1,
  566. tilt: 0.5,
  567. label: {
  568. show: true,
  569. radius: 1,
  570. formatter: function (label, series) {
  571. return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
  572. },
  573. background: {
  574. opacity: 0.8
  575. }
  576. },
  577. combine: {
  578. color: '#999',
  579. threshold: 0.1
  580. }
  581. }
  582. },
  583. legend: {
  584. show: false
  585. }
  586. });
  587. // DONUT
  588. $.plot($("#donut"), data, {
  589. series: {
  590. pie: {
  591. innerRadius: 0.5,
  592. show: true
  593. }
  594. }
  595. });
  596. // INTERACTIVE
  597. $.plot($("#interactive"), data, {
  598. series: {
  599. pie: {
  600. show: true
  601. }
  602. },
  603. grid: {
  604. hoverable: true,
  605. clickable: true
  606. }
  607. });
  608. $("#interactive").bind("plothover", pieHover);
  609. $("#interactive").bind("plotclick", pieClick);
  610. function pieHover(event, pos, obj) {
  611. if (!obj)
  612. return;
  613. percent = parseFloat(obj.series.percent).toFixed(2);
  614. $("#hover").html('<span style="font-weight: bold; color: ' + obj.series.color + '">' + obj.series.label + ' (' + percent + '%)</span>');
  615. }
  616. function pieClick(event, pos, obj) {
  617. if (!obj)
  618. return;
  619. percent = parseFloat(obj.series.percent).toFixed(2);
  620. alert('' + obj.series.label + ': ' + percent + '%');
  621. }
  622. }
  623. };
  624. }();