charts.js 21 KB

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