charts.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. jQuery(document).ready(function() {
  2. "use strict";
  3. function showTooltip(x, y, contents) {
  4. jQuery('<div id="tooltip" class="tooltipflot">' + contents + '</div>').css( {
  5. position: 'absolute',
  6. display: 'none',
  7. top: y + 5,
  8. left: x + 5
  9. }).appendTo("body").fadeIn(200);
  10. }
  11. /*****SIMPLE CHART*****/
  12. var flash = [[0, 2], [1, 6], [2,3], [3, 8], [4, 5], [5, 13], [6, 8]];
  13. var html5 = [[0, 5], [1, 4], [2,4], [3, 1], [4, 9], [5, 10], [6, 13]];
  14. var plot = jQuery.plot(jQuery("#basicflot"),
  15. [ { data: flash,
  16. label: "Flash",
  17. color: "#1CAF9A"
  18. },
  19. { data: html5,
  20. label: "HTML5",
  21. color: "#428BCA"
  22. }
  23. ],
  24. {
  25. series: {
  26. lines: {
  27. show: true,
  28. fill: true,
  29. lineWidth: 1,
  30. fillColor: {
  31. colors: [ { opacity: 0.5 },
  32. { opacity: 0.5 }
  33. ]
  34. }
  35. },
  36. points: {
  37. show: true
  38. },
  39. shadowSize: 0
  40. },
  41. legend: {
  42. position: 'nw'
  43. },
  44. grid: {
  45. hoverable: true,
  46. clickable: true,
  47. borderColor: '#ddd',
  48. borderWidth: 1,
  49. labelMargin: 10,
  50. backgroundColor: '#fff'
  51. },
  52. yaxis: {
  53. min: 0,
  54. max: 15,
  55. color: '#eee'
  56. },
  57. xaxis: {
  58. color: '#eee'
  59. }
  60. });
  61. var previousPoint = null;
  62. jQuery("#basicflot").bind("plothover", function (event, pos, item) {
  63. jQuery("#x").text(pos.x.toFixed(2));
  64. jQuery("#y").text(pos.y.toFixed(2));
  65. if(item) {
  66. if (previousPoint != item.dataIndex) {
  67. previousPoint = item.dataIndex;
  68. jQuery("#tooltip").remove();
  69. var x = item.datapoint[0].toFixed(2),
  70. y = item.datapoint[1].toFixed(2);
  71. showTooltip(item.pageX, item.pageY,
  72. item.series.label + " of " + x + " = " + y);
  73. }
  74. } else {
  75. jQuery("#tooltip").remove();
  76. previousPoint = null;
  77. }
  78. });
  79. jQuery("#basicflot").bind("plotclick", function (event, pos, item) {
  80. if (item) {
  81. plot.highlight(item.series, item.datapoint);
  82. }
  83. });
  84. /***** USING OTHER SYMBOLS *****/
  85. var firefox = [[0, 5], [1, 8], [2,6], [3, 11], [4, 7], [5, 13], [6, 9], [7,8], [8,10], [9,9],[10,13]];
  86. var chrome = [[0, 3], [1, 6], [2,4], [3, 9], [4, 5], [5, 11], [6, 7], [7,6], [8,8], [9,7],[10,11]];
  87. var plot2 = jQuery.plot(jQuery("#basicflot2"),
  88. [ { data: firefox,
  89. label: "Firefox",
  90. color: "#D9534F",
  91. points: {
  92. symbol: "square"
  93. }
  94. },
  95. { data: chrome,
  96. label: "Chrome",
  97. color: "#428BCA",
  98. lines: {
  99. fill: true
  100. },
  101. points: {
  102. symbol: 'diamond',
  103. lineWidth: 2
  104. }
  105. }
  106. ],
  107. {
  108. series: {
  109. lines: {
  110. show: true,
  111. lineWidth: 2
  112. },
  113. points: {
  114. show: true
  115. },
  116. shadowSize: 0
  117. },
  118. legend: {
  119. position: 'nw'
  120. },
  121. grid: {
  122. hoverable: true,
  123. clickable: true,
  124. borderColor: '#ddd',
  125. borderWidth: 1,
  126. labelMargin: 10,
  127. backgroundColor: '#fff'
  128. },
  129. yaxis: {
  130. min: 0,
  131. max: 15,
  132. color: '#eee'
  133. },
  134. xaxis: {
  135. color: '#eee',
  136. max: 10
  137. }
  138. });
  139. var previousPoint2 = null;
  140. jQuery("#basicflot2").bind("plothover", function (event, pos, item) {
  141. jQuery("#x").text(pos.x.toFixed(2));
  142. jQuery("#y").text(pos.y.toFixed(2));
  143. if(item) {
  144. if (previousPoint2 != item.dataIndex) {
  145. previousPoint2 = item.dataIndex;
  146. jQuery("#tooltip").remove();
  147. var x = item.datapoint[0].toFixed(2),
  148. y = item.datapoint[1].toFixed(2);
  149. showTooltip(item.pageX, item.pageY,
  150. item.series.label + " of " + x + " = " + y);
  151. }
  152. } else {
  153. jQuery("#tooltip").remove();
  154. previousPoint2 = null;
  155. }
  156. });
  157. jQuery("#basicflot2").bind("plotclick", function (event, pos, item) {
  158. if (item) {
  159. plot2.highlight(item.series, item.datapoint);
  160. }
  161. });
  162. /***** TRACKING WITH CROSSHAIR *****/
  163. var sin = [], cos = [];
  164. for (var i = 0; i < 14; i += 0.1) {
  165. sin.push([i, Math.sin(i)]);
  166. cos.push([i, Math.cos(i)]);
  167. }
  168. var plot3 = jQuery.plot("#trackingchart", [
  169. { data: sin, label: "sin(x) = -0.00", color: '#666' },
  170. { data: cos, label: "cos(x) = -0.00", color: '#999' }
  171. ], {
  172. series: {
  173. lines: {
  174. show: true,
  175. lineWidth: 2,
  176. },
  177. shadowSize: 0
  178. },
  179. legend: {
  180. show: false
  181. },
  182. crosshair: {
  183. mode: "xy",
  184. color: '#D9534F'
  185. },
  186. grid: {
  187. borderColor: '#ddd',
  188. borderWidth: 1,
  189. labelMargin: 10
  190. },
  191. yaxis: {
  192. color: '#eee'
  193. },
  194. xaxis: {
  195. color: '#eee'
  196. }
  197. });
  198. var legends = jQuery("#trackingchart .legendLabel");
  199. legends.each(function () {
  200. // fix the widths so they don't jump around
  201. jQuery(this).css('width', jQuery(this).width());
  202. });
  203. var updateLegendTimeout = null;
  204. var latestPosition = null;
  205. function updateLegend() {
  206. updateLegendTimeout = null;
  207. var pos = latestPosition;
  208. var axes = plot3.getAxes();
  209. if (pos.x < axes.xaxis.min || pos.x > axes.xaxis.max ||
  210. pos.y < axes.yaxis.min || pos.y > axes.yaxis.max) {
  211. return;
  212. }
  213. var i, j, dataset = plot3.getData();
  214. for (i = 0; i < dataset.length; ++i) {
  215. var series = dataset[i];
  216. // Find the nearest points, x-wise
  217. for (j = 0; j < series.data.length; ++j) {
  218. if (series.data[j][0] > pos.x) {
  219. break;
  220. }
  221. }
  222. // Now Interpolate
  223. var y,
  224. p1 = series.data[j - 1],
  225. p2 = series.data[j];
  226. if (p1 == null) {
  227. y = p2[1];
  228. } else if (p2 == null) {
  229. y = p1[1];
  230. } else {
  231. y = p1[1] + (p2[1] - p1[1]) * (pos.x - p1[0]) / (p2[0] - p1[0]);
  232. }
  233. legends.eq(i).text(series.label.replace(/=.*/, "= " + y.toFixed(2)));
  234. }
  235. }
  236. jQuery("#trackingchart").bind("plothover", function (event, pos, item) {
  237. latestPosition = pos;
  238. if (!updateLegendTimeout) {
  239. updateLegendTimeout = setTimeout(updateLegend, 50);
  240. }
  241. });
  242. /***** REAL TIME UPDATES *****/
  243. var data = [], totalPoints = 50;
  244. function getRandomData() {
  245. if (data.length > 0)
  246. data = data.slice(1);
  247. // Do a random walk
  248. while (data.length < totalPoints) {
  249. var prev = data.length > 0 ? data[data.length - 1] : 50,
  250. y = prev + Math.random() * 10 - 5;
  251. if (y < 0) {
  252. y = 0;
  253. } else if (y > 100) {
  254. y = 100;
  255. }
  256. data.push(y);
  257. }
  258. // Zip the generated y values with the x values
  259. var res = [];
  260. for (var i = 0; i < data.length; ++i) {
  261. res.push([i, data[i]])
  262. }
  263. return res;
  264. }
  265. // Set up the control widget
  266. var updateInterval = 1000;
  267. var plot4 = jQuery.plot("#realtimechart", [ getRandomData() ], {
  268. colors: ["#F0AD4E"],
  269. series: {
  270. lines: {
  271. fill: true,
  272. lineWidth: 0
  273. },
  274. shadowSize: 0 // Drawing is faster without shadows
  275. },
  276. grid: {
  277. borderColor: '#ddd',
  278. borderWidth: 1,
  279. labelMargin: 10
  280. },
  281. xaxis: {
  282. color: '#eee'
  283. },
  284. yaxis: {
  285. min: 0,
  286. max: 100,
  287. color: '#eee'
  288. }
  289. });
  290. function update() {
  291. plot4.setData([getRandomData()]);
  292. // Since the axes don't change, we don't need to call plot.setupGrid()
  293. plot4.draw();
  294. setTimeout(update, updateInterval);
  295. }
  296. update();
  297. /***** BAR CHART *****/
  298. var bardata = [ ["Jan", 10], ["Feb", 23], ["Mar", 18], ["Apr", 13], ["May", 17], ["Jun", 30], ["Jul", 26], ["Aug", 16], ["Sep", 17], ["Oct", 5], ["Nov", 8], ["Dec", 15] ];
  299. jQuery.plot("#barchart", [ bardata ], {
  300. series: {
  301. lines: {
  302. lineWidth: 1
  303. },
  304. bars: {
  305. show: true,
  306. barWidth: 0.5,
  307. align: "center",
  308. lineWidth: 0,
  309. fillColor: "#428BCA"
  310. }
  311. },
  312. grid: {
  313. borderColor: '#ddd',
  314. borderWidth: 1,
  315. labelMargin: 10
  316. },
  317. xaxis: {
  318. mode: "categories",
  319. tickLength: 0
  320. }
  321. });
  322. /***** PIE CHART *****/
  323. var piedata = [
  324. { label: "Series 1", data: [[1,10]], color: '#D9534F'},
  325. { label: "Series 2", data: [[1,30]], color: '#1CAF9A'},
  326. { label: "Series 3", data: [[1,90]], color: '#F0AD4E'},
  327. { label: "Series 4", data: [[1,70]], color: '#428BCA'},
  328. { label: "Series 5", data: [[1,80]], color: '#5BC0DE'}
  329. ];
  330. jQuery.plot('#piechart', piedata, {
  331. series: {
  332. pie: {
  333. show: true,
  334. radius: 1,
  335. label: {
  336. show: true,
  337. radius: 2/3,
  338. formatter: labelFormatter,
  339. threshold: 0.1
  340. }
  341. }
  342. },
  343. grid: {
  344. hoverable: true,
  345. clickable: true
  346. }
  347. });
  348. function labelFormatter(label, series) {
  349. return "<div style='font-size:8pt; text-align:center; padding:2px; color:white;'>" + label + "<br/>" + Math.round(series.percent) + "%</div>";
  350. }
  351. /***** MORRIS CHARTS *****/
  352. var m1 = new Morris.Line({
  353. // ID of the element in which to draw the chart.
  354. element: 'line-chart',
  355. // Chart data records -- each entry in this array corresponds to a point on
  356. // the chart.
  357. data: [
  358. { y: '2006', a: 30, b: 20 },
  359. { y: '2007', a: 75, b: 65 },
  360. { y: '2008', a: 50, b: 40 },
  361. { y: '2009', a: 75, b: 65 },
  362. { y: '2010', a: 50, b: 40 },
  363. { y: '2011', a: 75, b: 65 },
  364. { y: '2012', a: 100, b: 90 }
  365. ],
  366. xkey: 'y',
  367. ykeys: ['a', 'b'],
  368. labels: ['Series A', 'Series B'],
  369. lineColors: ['#D9534F', '#428BCA'],
  370. lineWidth: '2px',
  371. hideHover: true
  372. });
  373. var m2 = new Morris.Area({
  374. // ID of the element in which to draw the chart.
  375. element: 'area-chart',
  376. // Chart data records -- each entry in this array corresponds to a point on
  377. // the chart.
  378. data: [
  379. { y: '2006', a: 30, b: 20 },
  380. { y: '2007', a: 75, b: 65 },
  381. { y: '2008', a: 50, b: 40 },
  382. { y: '2009', a: 75, b: 65 },
  383. { y: '2010', a: 50, b: 40 },
  384. { y: '2011', a: 75, b: 65 },
  385. { y: '2012', a: 100, b: 90 }
  386. ],
  387. xkey: 'y',
  388. ykeys: ['a', 'b'],
  389. labels: ['Series A', 'Series B'],
  390. lineColors: ['#1CAF9A', '#F0AD4E'],
  391. lineWidth: '1px',
  392. fillOpacity: 0.8,
  393. smooth: false,
  394. hideHover: true
  395. });
  396. var m3 = new Morris.Bar({
  397. // ID of the element in which to draw the chart.
  398. element: 'bar-chart',
  399. // Chart data records -- each entry in this array corresponds to a point on
  400. // the chart.
  401. data: [
  402. { y: '2006', a: 30, b: 20 },
  403. { y: '2007', a: 75, b: 65 },
  404. { y: '2008', a: 50, b: 40 },
  405. { y: '2009', a: 75, b: 65 },
  406. { y: '2010', a: 50, b: 40 },
  407. { y: '2011', a: 75, b: 65 },
  408. { y: '2012', a: 100, b: 90 }
  409. ],
  410. xkey: 'y',
  411. ykeys: ['a', 'b'],
  412. labels: ['Series A', 'Series B'],
  413. lineWidth: '1px',
  414. fillOpacity: 0.8,
  415. smooth: false,
  416. hideHover: true
  417. });
  418. var m4 = new Morris.Bar({
  419. // ID of the element in which to draw the chart.
  420. element: 'stacked-chart',
  421. // Chart data records -- each entry in this array corresponds to a point on
  422. // the chart.
  423. data: [
  424. { y: '2006', a: 30, b: 20 },
  425. { y: '2007', a: 75, b: 65 },
  426. { y: '2008', a: 50, b: 40 },
  427. { y: '2009', a: 75, b: 65 },
  428. { y: '2010', a: 50, b: 40 },
  429. { y: '2011', a: 75, b: 65 },
  430. { y: '2012', a: 100, b: 90 }
  431. ],
  432. xkey: 'y',
  433. ykeys: ['a', 'b'],
  434. labels: ['Series A', 'Series B'],
  435. barColors: ['#1CAF9A', '#428BCA'],
  436. lineWidth: '1px',
  437. fillOpacity: 0.8,
  438. smooth: false,
  439. stacked: true,
  440. hideHover: true
  441. });
  442. var m5 = new Morris.Donut({
  443. element: 'donut-chart',
  444. data: [
  445. {label: "Download Sales", value: 12},
  446. {label: "In-Store Sales", value: 30},
  447. {label: "Mail-Order Sales", value: 20}
  448. ]
  449. });
  450. var m6 = new Morris.Donut({
  451. element: 'donut-chart2',
  452. data: [
  453. {label: "Chrome", value: 30},
  454. {label: "Firefox", value: 20},
  455. {label: "Opera", value: 20},
  456. {label: "Safari", value: 20},
  457. {label: "Internet Explorer", value: 10}
  458. ],
  459. colors: ['#D9534F','#1CAF9A','#428BCA','#5BC0DE','#428BCA']
  460. });
  461. /***** SPARKLINE CHARTS *****/
  462. jQuery('#sparkline').sparkline([4,3,3,1,4,3,2,2,3], {
  463. type: 'bar',
  464. height:'30px',
  465. barColor: '#428BCA'
  466. });
  467. jQuery('#sparkline2').sparkline([4,3,3,1,4,3,2,2,3], {
  468. type: 'line',
  469. height:'33px',
  470. width: '50px',
  471. lineColor: false,
  472. fillColor: '#1CAF9A'
  473. });
  474. jQuery('#sparkline3').sparkline([4,3,3,1,4,3,2,2,3], {
  475. type: 'pie',
  476. height:'33px',
  477. sliceColors: ['#F0AD4E','#428BCA','#D9534F','#1CAF9A','#5BC0DE']
  478. });
  479. jQuery('#sparkline4').sparkline([4,3,3,5,4,3,2,5,3], {
  480. type: 'line',
  481. height:'33px',
  482. width: '50px',
  483. lineColor: '#5BC0DE',
  484. fillColor: false
  485. });
  486. jQuery('#sparkline4').sparkline([3,6,6,2,6,5,3,2,1], {
  487. type: 'line',
  488. height:'33px',
  489. width: '50px',
  490. lineColor: '#D9534F',
  491. fillColor: false,
  492. composite: true
  493. });
  494. var delay = (function() {
  495. var timer = 0;
  496. return function(callback, ms) {
  497. clearTimeout(timer);
  498. timer = setTimeout(callback, ms);
  499. };
  500. })();
  501. jQuery(window).resize(function() {
  502. delay(function() {
  503. m1.redraw();
  504. m2.redraw();
  505. m3.redraw();
  506. m4.redraw();
  507. m5.redraw();
  508. m6.redraw();
  509. }, 200);
  510. }).trigger('resize');
  511. });