be_comp_charts.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Document : be_comp_charts.js
  3. * Author : pixelcave
  4. * Description: Custom JS code used in Charts Page
  5. */
  6. var BeCompCharts = function() {
  7. // Randomize Easy Pie Chart values
  8. var initRandomEasyPieChart = function(){
  9. jQuery('.js-pie-randomize').on('click', function(){
  10. jQuery(this)
  11. .parents('.block')
  12. .find('.pie-chart')
  13. .each(function() {
  14. var random = Math.floor((Math.random() * 100) + 1);
  15. jQuery(this)
  16. .data('easyPieChart')
  17. .update(random);
  18. });
  19. });
  20. };
  21. // jQuery Sparkline Charts, for more examples you can check out http://omnipotent.net/jquery.sparkline/#s-docs
  22. var initChartsSparkline = function(){
  23. // Chart Containers
  24. var slcLine1 = jQuery('.js-slc-line1');
  25. var slcLine2 = jQuery('.js-slc-line2');
  26. var slcLine3 = jQuery('.js-slc-line3');
  27. var slcBar1 = jQuery('.js-slc-bar1');
  28. var slcBar2 = jQuery('.js-slc-bar2');
  29. var slcBar3 = jQuery('.js-slc-bar3');
  30. var slcPie1 = jQuery('.js-slc-pie1');
  31. var slcPie2 = jQuery('.js-slc-pie2');
  32. var slcPie3 = jQuery('.js-slc-pie3');
  33. var slcTristate1 = jQuery('.js-slc-tristate1');
  34. var slcTristate2 = jQuery('.js-slc-tristate2');
  35. var slcTristate3 = jQuery('.js-slc-tristate3');
  36. // Line Charts
  37. var lineOptions = {
  38. type: 'line',
  39. width: '120px',
  40. height: '80px',
  41. tooltipOffsetX: -25,
  42. tooltipOffsetY: 20,
  43. lineColor: '#ffca28',
  44. fillColor: '#ffca28',
  45. spotColor: '#555',
  46. minSpotColor: '#555',
  47. maxSpotColor: '#555',
  48. highlightSpotColor: '#555',
  49. highlightLineColor: '#555',
  50. spotRadius: 2,
  51. tooltipPrefix: '',
  52. tooltipSuffix: ' Tickets',
  53. tooltipFormat: '{{prefix}}{{y}}{{suffix}}'
  54. };
  55. if ( slcLine1.length ) {
  56. slcLine1.sparkline('html', lineOptions);
  57. }
  58. lineOptions['lineColor'] = '#9ccc65';
  59. lineOptions['fillColor'] = '#9ccc65';
  60. lineOptions['tooltipPrefix'] = '$ ';
  61. lineOptions['tooltipSuffix'] = '';
  62. if ( slcLine2.length ) {
  63. slcLine2.sparkline('html', lineOptions);
  64. }
  65. lineOptions['lineColor'] = '#42a5f5';
  66. lineOptions['fillColor'] = '#42a5f5';
  67. lineOptions['tooltipPrefix'] = '';
  68. lineOptions['tooltipSuffix'] = ' Sales';
  69. if ( slcLine3.length ) {
  70. slcLine3.sparkline('html', lineOptions);
  71. }
  72. // Bar Charts
  73. var barOptions = {
  74. type: 'bar',
  75. barWidth: 8,
  76. barSpacing: 6,
  77. height: '80px',
  78. barColor: '#ffca28',
  79. tooltipPrefix: '',
  80. tooltipSuffix: ' Tickets',
  81. tooltipFormat: '{{prefix}}{{value}}{{suffix}}'
  82. };
  83. if ( slcBar1.length ) {
  84. slcBar1.sparkline('html', barOptions);
  85. }
  86. barOptions['barColor'] = '#9ccc65';
  87. barOptions['tooltipPrefix'] = '$ ';
  88. barOptions['tooltipSuffix'] = '';
  89. if ( slcBar2.length ) {
  90. slcBar2.sparkline('html', barOptions);
  91. }
  92. barOptions['barColor'] = '#42a5f5';
  93. barOptions['tooltipPrefix'] = '';
  94. barOptions['tooltipSuffix'] = ' Sales';
  95. if ( slcBar3.length ) {
  96. slcBar3.sparkline('html', barOptions);
  97. }
  98. // Pie Charts
  99. var pieCharts = {
  100. type: 'pie',
  101. width: '80px',
  102. height: '80px',
  103. sliceColors: ['#ffca28','#9ccc65', '#42a5f5','#ef5350'],
  104. highlightLighten: 1.1,
  105. tooltipPrefix: '',
  106. tooltipSuffix: ' Tickets',
  107. tooltipFormat: '{{prefix}}{{value}}{{suffix}}'
  108. };
  109. if ( slcPie1.length ) {
  110. slcPie1.sparkline('html', pieCharts);
  111. }
  112. pieCharts['tooltipPrefix'] = '$ ';
  113. pieCharts['tooltipSuffix'] = '';
  114. if ( slcPie2.length ) {
  115. slcPie2.sparkline('html', pieCharts);
  116. }
  117. pieCharts['tooltipPrefix'] = '';
  118. pieCharts['tooltipSuffix'] = ' Sales';
  119. if ( slcPie3.length ) {
  120. slcPie3.sparkline('html', pieCharts);
  121. }
  122. // Tristate Charts
  123. var tristateOptions = {
  124. type: 'tristate',
  125. barWidth: 8,
  126. barSpacing: 6,
  127. height: '110px',
  128. posBarColor: '#9ccc65',
  129. negBarColor: '#ef5350'
  130. };
  131. if ( slcTristate1.length ) {
  132. slcTristate1.sparkline('html', tristateOptions);
  133. }
  134. if ( slcTristate2.length ) {
  135. slcTristate2.sparkline('html', tristateOptions);
  136. }
  137. if ( slcTristate3.length ) {
  138. slcTristate3.sparkline('html', tristateOptions);
  139. }
  140. };
  141. // Chart.js Charts, for more examples you can check out http://www.chartjs.org/docs
  142. var initChartsChartJS = function () {
  143. // Set Global Chart.js configuration
  144. Chart.defaults.global.defaultFontColor = '#555555';
  145. Chart.defaults.scale.gridLines.color = "rgba(0,0,0,.04)";
  146. Chart.defaults.scale.gridLines.zeroLineColor = "rgba(0,0,0,.1)";
  147. Chart.defaults.scale.ticks.beginAtZero = true;
  148. Chart.defaults.global.elements.line.borderWidth = 2;
  149. Chart.defaults.global.elements.point.radius = 5;
  150. Chart.defaults.global.elements.point.hoverRadius = 7;
  151. Chart.defaults.global.tooltips.cornerRadius = 3;
  152. Chart.defaults.global.legend.labels.boxWidth = 12;
  153. // Get Chart Containers
  154. var chartLinesCon = jQuery('.js-chartjs-lines');
  155. var chartBarsCon = jQuery('.js-chartjs-bars');
  156. var chartRadarCon = jQuery('.js-chartjs-radar');
  157. var chartPolarCon = jQuery('.js-chartjs-polar');
  158. var chartPieCon = jQuery('.js-chartjs-pie');
  159. var chartDonutCon = jQuery('.js-chartjs-donut');
  160. // Set Chart and Chart Data variables
  161. var chartLines, chartBars, chartRadar, chartPolar, chartPie, chartDonut;
  162. var chartLinesBarsRadarData, chartPolarPieDonutData;
  163. // Lines/Bar/Radar Chart Data
  164. var chartLinesBarsRadarData = {
  165. labels: ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'],
  166. datasets: [
  167. {
  168. label: 'This Week',
  169. fill: true,
  170. backgroundColor: 'rgba(66,165,245,.75)',
  171. borderColor: 'rgba(66,165,245,1)',
  172. pointBackgroundColor: 'rgba(66,165,245,1)',
  173. pointBorderColor: '#fff',
  174. pointHoverBackgroundColor: '#fff',
  175. pointHoverBorderColor: 'rgba(66,165,245,1)',
  176. data: [25, 38, 62, 45, 90, 115, 130]
  177. },
  178. {
  179. label: 'Last Week',
  180. fill: true,
  181. backgroundColor: 'rgba(66,165,245,.25)',
  182. borderColor: 'rgba(66,165,245,1)',
  183. pointBackgroundColor: 'rgba(66,165,245,1)',
  184. pointBorderColor: '#fff',
  185. pointHoverBackgroundColor: '#fff',
  186. pointHoverBorderColor: 'rgba(66,165,245,1)',
  187. data: [112, 90, 142, 130, 170, 188, 196]
  188. }
  189. ]
  190. };
  191. // Polar/Pie/Donut Data
  192. var chartPolarPieDonutData = {
  193. labels: [
  194. 'Earnings',
  195. 'Sales',
  196. 'Tickets'
  197. ],
  198. datasets: [{
  199. data: [
  200. 50,
  201. 25,
  202. 25
  203. ],
  204. backgroundColor: [
  205. 'rgba(156,204,101,1)',
  206. 'rgba(255,202,40,1)',
  207. 'rgba(239,83,80,1)'
  208. ],
  209. hoverBackgroundColor: [
  210. 'rgba(156,204,101,.5)',
  211. 'rgba(255,202,40,.5)',
  212. 'rgba(239,83,80,.5)'
  213. ]
  214. }]
  215. };
  216. // Init Charts
  217. if ( chartLinesCon.length ) {
  218. chartLines = new Chart(chartLinesCon, { type: 'line', data: chartLinesBarsRadarData });
  219. }
  220. if ( chartBarsCon.length ) {
  221. chartBars = new Chart(chartBarsCon, { type: 'bar', data: chartLinesBarsRadarData });
  222. }
  223. if ( chartRadarCon.length ) {
  224. chartRadar = new Chart(chartRadarCon, { type: 'radar', data: chartLinesBarsRadarData });
  225. }
  226. if ( chartPolarCon.length ) {
  227. chartPolar = new Chart(chartPolarCon, { type: 'polarArea', data: chartPolarPieDonutData });
  228. }
  229. if ( chartPieCon.length ) {
  230. chartPie = new Chart(chartPieCon, { type: 'pie', data: chartPolarPieDonutData });
  231. }
  232. if ( chartDonutCon.length ) {
  233. chartDonut = new Chart(chartDonutCon, { type: 'doughnut', data: chartPolarPieDonutData });
  234. }
  235. };
  236. // Flot charts, for more examples you can check out http://www.flotcharts.org/flot/examples/
  237. var initChartsFlot = function(){
  238. // Get the elements where we will attach the charts
  239. var flotLive = jQuery('.js-flot-live');
  240. var flotLines = jQuery('.js-flot-lines');
  241. var flotStacked = jQuery('.js-flot-stacked');
  242. var flotPie = jQuery('.js-flot-pie');
  243. var flotBars = jQuery('.js-flot-bars');
  244. // Demo Data
  245. var dataEarnings = [[1, 1500], [2, 1700], [3, 1400], [4, 1900], [5, 2500], [6, 2300], [7, 2700], [8, 3200], [9, 3500], [10, 3260], [11, 4100], [12, 4600]];
  246. var dataSales = [[1, 500], [2, 600], [3, 400], [4, 750], [5, 1150], [6, 950], [7, 1400], [8, 1700], [9, 1800], [10, 1300], [11, 1750], [12, 2900]];
  247. var dataSalesBefore = [[1, 500], [4, 600], [7, 1000], [10, 600], [13, 800], [16, 1200], [19, 1500], [22, 1600], [25, 2500], [28, 2700], [31, 3500], [34, 4500]];
  248. var dataSalesAfter = [[2, 900], [5, 1200], [8, 2000], [11, 1200], [14, 1600], [17, 2400], [20, 3000], [23, 3200], [26, 5000], [29, 5400], [32, 7000], [35, 9000]];
  249. var dataMonths = [[1, 'Jan'], [2, 'Feb'], [3, 'Mar'], [4, 'Apr'], [5, 'May'], [6, 'Jun'], [7, 'Jul'], [8, 'Aug'], [9, 'Sep'], [10, 'Oct'], [11, 'Nov'], [12, 'Dec']];
  250. var dataMonthsBars = [[2, 'Jan'], [5, 'Feb'], [8, 'Mar'], [11, 'Apr'], [14, 'May'], [17, 'Jun'], [20, 'Jul'], [23, 'Aug'], [26, 'Sep'], [29, 'Oct'], [32, 'Nov'], [35, 'Dec']];
  251. // Live Chart
  252. var dataLive = [];
  253. function getRandomData() { // Random data generator
  254. if (dataLive.length > 0)
  255. dataLive = dataLive.slice(1);
  256. while (dataLive.length < 300) {
  257. var prev = dataLive.length > 0 ? dataLive[dataLive.length - 1] : 50;
  258. var y = prev + Math.random() * 10 - 5;
  259. if (y < 0)
  260. y = 0;
  261. if (y > 100)
  262. y = 100;
  263. dataLive.push(y);
  264. }
  265. var res = [];
  266. for (var i = 0; i < dataLive.length; ++i)
  267. res.push([i, dataLive[i]]);
  268. jQuery('.js-flot-live-info').html(y.toFixed(0) + '%'); // Show live chart info
  269. return res;
  270. }
  271. function updateChartLive() { // Update live chart
  272. chartLive.setData([getRandomData()]);
  273. chartLive.draw();
  274. setTimeout(updateChartLive, 100);
  275. }
  276. if ( flotLive.length ) {
  277. var chartLive = jQuery.plot(flotLive, // Init live chart
  278. [{ data: getRandomData() }],
  279. {
  280. series: {
  281. shadowSize: 0
  282. },
  283. lines: {
  284. show: true,
  285. lineWidth: 1,
  286. fill: true,
  287. fillColor: {
  288. colors: [{opacity: 1}, {opacity: .5}]
  289. }
  290. },
  291. colors: ['#42a5f5'],
  292. grid: {
  293. borderWidth: 0,
  294. color: '#cccccc'
  295. },
  296. yaxis: {
  297. show: true,
  298. min: 0,
  299. max: 100
  300. },
  301. xaxis: {
  302. show: false
  303. }
  304. }
  305. );
  306. updateChartLive(); // Start getting new data
  307. }
  308. // Init lines chart
  309. if ( flotLines.length ) {
  310. jQuery.plot(flotLines,
  311. [
  312. {
  313. label: 'Earnings',
  314. data: dataEarnings,
  315. lines: {
  316. show: true,
  317. fill: true,
  318. fillColor: {
  319. colors: [{opacity: .7}, {opacity: .7}]
  320. }
  321. },
  322. points: {
  323. show: true,
  324. radius: 5
  325. }
  326. },
  327. {
  328. label: 'Sales',
  329. data: dataSales,
  330. lines: {
  331. show: true,
  332. fill: true,
  333. fillColor: {
  334. colors: [{opacity: .5}, {opacity: .5}]
  335. }
  336. },
  337. points: {
  338. show: true,
  339. radius: 5
  340. }
  341. }
  342. ],
  343. {
  344. colors: ['#ffca28', '#555555'],
  345. legend: {
  346. show: true,
  347. position: 'nw',
  348. backgroundOpacity: 0
  349. },
  350. grid: {
  351. borderWidth: 0,
  352. hoverable: true,
  353. clickable: true
  354. },
  355. yaxis: {
  356. tickColor: '#ffffff',
  357. ticks: 3
  358. },
  359. xaxis: {
  360. ticks: dataMonths,
  361. tickColor: '#f5f5f5'
  362. }
  363. }
  364. );
  365. // Creating and attaching a tooltip to the classic chart
  366. var previousPoint = null, ttlabel = null;
  367. flotLines.bind('plothover', function(event, pos, item) {
  368. if (item) {
  369. if (previousPoint !== item.dataIndex) {
  370. previousPoint = item.dataIndex;
  371. jQuery('.js-flot-tooltip').remove();
  372. var x = item.datapoint[0], y = item.datapoint[1];
  373. if (item.seriesIndex === 0) {
  374. ttlabel = '$ <strong>' + y + '</strong>';
  375. } else if (item.seriesIndex === 1) {
  376. ttlabel = '<strong>' + y + '</strong> sales';
  377. } else {
  378. ttlabel = '<strong>' + y + '</strong> tickets';
  379. }
  380. jQuery('<div class="js-flot-tooltip flot-tooltip">' + ttlabel + '</div>')
  381. .css({top: item.pageY - 45, left: item.pageX + 5}).appendTo("body").show();
  382. }
  383. }
  384. else {
  385. jQuery('.js-flot-tooltip').remove();
  386. previousPoint = null;
  387. }
  388. });
  389. }
  390. // Stacked Chart
  391. if ( flotStacked.length ) {
  392. jQuery.plot(flotStacked,
  393. [
  394. {
  395. label: 'Sales',
  396. data: dataSales
  397. },
  398. {
  399. label: 'Earnings',
  400. data: dataEarnings
  401. }
  402. ],
  403. {
  404. colors: ['#555555', '#26c6da'],
  405. series: {
  406. stack: true,
  407. lines: {
  408. show: true,
  409. fill: true
  410. }
  411. },
  412. lines: {show: true,
  413. lineWidth: 0,
  414. fill: true,
  415. fillColor: {
  416. colors: [{opacity: 1}, {opacity: 1}]
  417. }
  418. },
  419. legend: {
  420. show: true,
  421. position: 'nw',
  422. sorted: true,
  423. backgroundOpacity: 0
  424. },
  425. grid: {
  426. borderWidth: 0
  427. },
  428. yaxis: {
  429. tickColor: '#ffffff',
  430. ticks: 3
  431. },
  432. xaxis: {
  433. ticks: dataMonths,
  434. tickColor: '#f5f5f5'
  435. }
  436. }
  437. );
  438. }
  439. // Bars Chart
  440. if ( flotBars.length ) {
  441. jQuery.plot(flotBars,
  442. [
  443. {
  444. label: 'Sales Before Release',
  445. data: dataSalesBefore,
  446. bars: {
  447. show: true,
  448. lineWidth: 0,
  449. fillColor: {
  450. colors: [{opacity: .75}, {opacity: .75}]
  451. }
  452. }
  453. },
  454. {
  455. label: 'Sales After Release',
  456. data: dataSalesAfter,
  457. bars: {
  458. show: true,
  459. lineWidth: 0,
  460. fillColor: {
  461. colors: [{opacity: .75}, {opacity: .75}]
  462. }
  463. }
  464. }
  465. ],
  466. {
  467. colors: ['#ef5350', '#9ccc65'],
  468. legend: {
  469. show: true,
  470. position: 'nw',
  471. backgroundOpacity: 0
  472. },
  473. grid: {
  474. borderWidth: 0
  475. },
  476. yaxis: {
  477. ticks: 3,
  478. tickColor: '#f5f5f5'
  479. },
  480. xaxis: {
  481. ticks: dataMonthsBars,
  482. tickColor: '#f5f5f5'
  483. }
  484. }
  485. );
  486. }
  487. // Pie Chart
  488. if ( flotPie.length ) {
  489. jQuery.plot(flotPie,
  490. [
  491. {
  492. label: 'Sales',
  493. data: 15
  494. },
  495. {
  496. label: 'Tickets',
  497. data: 12
  498. },
  499. {
  500. label: 'Earnings',
  501. data: 73
  502. }
  503. ],
  504. {
  505. colors: ['#26c6da', '#ffca28', '#9ccc65'],
  506. legend: {show: false},
  507. series: {
  508. pie: {
  509. show: true,
  510. radius: 1,
  511. label: {
  512. show: true,
  513. radius: 2/3,
  514. formatter: function(label, pieSeries) {
  515. return '<div class="flot-pie-label">' + label + '<br>' + Math.round(pieSeries.percent) + '%</div>';
  516. },
  517. background: {
  518. opacity: .75,
  519. color: '#000000'
  520. }
  521. }
  522. }
  523. }
  524. }
  525. );
  526. }
  527. };
  528. return {
  529. init: function () {
  530. // Randomize Easy Pie values functionality
  531. initRandomEasyPieChart();
  532. // Init Sparkline Charts
  533. initChartsSparkline();
  534. // Init Chart.js Charts
  535. initChartsChartJS();
  536. // Init Flot Charts
  537. initChartsFlot();
  538. }
  539. };
  540. }();
  541. // Initialize when page loads
  542. jQuery(function(){ BeCompCharts.init(); });