/public/assets/webadmin/pages/chartjs.init.js

https://github.com/dev4living/HeyCommunity · JavaScript · 228 lines · 190 code · 19 blank · 19 comment · 0 complexity · a46f1ad9ec9a5d7b466eaa03f914e417 MD5 · raw file

  1. /**
  2. Template Name: WebAdmin
  3. Chartjs
  4. */
  5. !function($) {
  6. "use strict";
  7. var ChartJs = function() {};
  8. ChartJs.prototype.respChart = function(selector,type,data, options) {
  9. // get selector by context
  10. var ctx = selector.get(0).getContext("2d");
  11. // pointing parent container to make chart js inherit its width
  12. var container = $(selector).parent();
  13. // enable resizing matter
  14. $(window).resize( generateChart );
  15. // this function produce the responsive Chart JS
  16. function generateChart(){
  17. // make chart width fit with its container
  18. var ww = selector.attr('width', $(container).width() );
  19. switch(type){
  20. case 'Line':
  21. new Chart(ctx, {type: 'line', data: data, options: options});
  22. break;
  23. case 'Doughnut':
  24. new Chart(ctx, {type: 'doughnut', data: data, options: options});
  25. break;
  26. case 'Pie':
  27. new Chart(ctx, {type: 'pie', data: data, options: options});
  28. break;
  29. case 'Bar':
  30. new Chart(ctx, {type: 'bar', data: data, options: options});
  31. break;
  32. case 'Radar':
  33. new Chart(ctx, {type: 'radar', data: data, options: options});
  34. break;
  35. case 'PolarArea':
  36. new Chart(ctx, {data: data, type: 'polarArea', options: options});
  37. break;
  38. }
  39. // Initiate new chart or Redraw
  40. };
  41. // run function - render chart at first load
  42. generateChart();
  43. },
  44. //init
  45. ChartJs.prototype.init = function() {
  46. //creating lineChart
  47. var lineChart = {
  48. labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September","October"],
  49. datasets: [
  50. {
  51. label: "Sales Analytics",
  52. fill: false,
  53. lineTension: 0.1,
  54. backgroundColor: "#3292e0",
  55. borderColor: "#3292e0",
  56. borderCapStyle: 'butt',
  57. borderDash: [],
  58. borderDashOffset: 0.0,
  59. borderJoinStyle: 'miter',
  60. pointBorderColor: "#3292e0",
  61. pointBackgroundColor: "#fff",
  62. pointBorderWidth: 1,
  63. pointHoverRadius: 5,
  64. pointHoverBackgroundColor: "#3292e0",
  65. pointHoverBorderColor: "#eef0f2",
  66. pointHoverBorderWidth: 2,
  67. pointRadius: 1,
  68. pointHitRadius: 10,
  69. data: [65, 59, 80, 81, 56, 55, 40, 55, 30, 80]
  70. }
  71. ]
  72. };
  73. var lineOpts = {
  74. scales: {
  75. yAxes: [{
  76. ticks: {
  77. max: 100,
  78. min: 20,
  79. stepSize: 10
  80. }
  81. }]
  82. }
  83. };
  84. this.respChart($("#lineChart"),'Line',lineChart, lineOpts);
  85. //donut chart
  86. var donutChart = {
  87. labels: [
  88. "Desktops",
  89. "Tablets",
  90. "Mobiles"
  91. ],
  92. datasets: [
  93. {
  94. data: [300, 50, 100],
  95. backgroundColor: [
  96. "#45bbe0",
  97. "#3292e0",
  98. "#ebeff2"
  99. ],
  100. hoverBackgroundColor: [
  101. "#45bbe0",
  102. "#3292e0",
  103. "#ebeff2"
  104. ],
  105. hoverBorderColor: "#fff"
  106. }]
  107. };
  108. this.respChart($("#doughnut"),'Doughnut',donutChart);
  109. //Pie chart
  110. var pieChart = {
  111. labels: [
  112. "Desktops",
  113. "Tablets",
  114. "Mobiles"
  115. ],
  116. datasets: [
  117. {
  118. data: [300, 50, 100],
  119. backgroundColor: [
  120. "#45bbe0",
  121. "#3292e0",
  122. "#ebeff2"
  123. ],
  124. hoverBackgroundColor: [
  125. "#45bbe0",
  126. "#3292e0",
  127. "#ebeff2"
  128. ],
  129. hoverBorderColor: "#fff"
  130. }]
  131. };
  132. this.respChart($("#pie"),'Pie',pieChart);
  133. //barchart
  134. var barChart = {
  135. labels: ["January", "February", "March", "April", "May", "June", "July"],
  136. datasets: [
  137. {
  138. label: "Sales Analytics",
  139. backgroundColor: "#3292e0",
  140. borderColor: "#3292e0",
  141. borderWidth: 1,
  142. hoverBackgroundColor: "#3292e0",
  143. hoverBorderColor: "#3292e0",
  144. data: [65, 59, 81, 45, 56, 80, 50,20]
  145. }
  146. ]
  147. };
  148. this.respChart($("#bar"),'Bar',barChart);
  149. //radar chart
  150. var radarChart = {
  151. labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],
  152. datasets: [
  153. {
  154. label: "Desktops",
  155. backgroundColor: "rgba(179,181,198,0.2)",
  156. borderColor: "rgba(179,181,198,1)",
  157. pointBackgroundColor: "rgba(179,181,198,1)",
  158. pointBorderColor: "#fff",
  159. pointHoverBackgroundColor: "#fff",
  160. pointHoverBorderColor: "rgba(179,181,198,1)",
  161. data: [65, 59, 90, 81, 56, 55, 40]
  162. },
  163. {
  164. label: "Tablets",
  165. backgroundColor: "rgba(50,146,224,0.2)",
  166. borderColor: "#3292e0",
  167. pointBackgroundColor: "#3292e0",
  168. pointBorderColor: "#fff",
  169. pointHoverBackgroundColor: "#fff",
  170. pointHoverBorderColor: "#3292e0",
  171. data: [28, 48, 40, 19, 96, 27, 100]
  172. }
  173. ]
  174. };
  175. this.respChart($("#radar"),'Radar',radarChart);
  176. //Polar area chart
  177. var polarChart = {
  178. datasets: [{
  179. data: [
  180. 11,
  181. 16,
  182. 7,
  183. 18
  184. ],
  185. backgroundColor: [
  186. "#f62f37",
  187. "#188ae2",
  188. "#45bbe0",
  189. "#f8ca4e"
  190. ],
  191. label: 'My dataset', // for legend
  192. hoverBorderColor: "#fff"
  193. }],
  194. labels: [
  195. "Series 1",
  196. "Series 2",
  197. "Series 3",
  198. "Series 4"
  199. ]
  200. };
  201. this.respChart($("#polarArea"),'PolarArea',polarChart);
  202. },
  203. $.ChartJs = new ChartJs, $.ChartJs.Constructor = ChartJs
  204. }(window.jQuery),
  205. //initializing
  206. function($) {
  207. "use strict";
  208. $.ChartJs.init()
  209. }(window.jQuery);