chart-int.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //[Dashboard Javascript]
  2. //Project: Riday Admin - Responsive Admin Template
  3. //Primary use: Used only for the main dashboard (index.html)
  4. // ------------------------------
  5. var Widgetschart = function() {
  6. // Simple bar charts
  7. var _barChartWidget = function(element, barQty, height, animate, easing, duration, delay, color, tooltip) {
  8. if (typeof d3 == 'undefined') {
  9. console.warn('Warning - d3.min.js is not loaded.');
  10. return;
  11. }
  12. // Initialize chart only if element exsists in the DOM
  13. if(element) {
  14. // Basic setup
  15. // ------------------------------
  16. // Add data set
  17. var bardata = [];
  18. for (var i=0; i < barQty; i++) {
  19. bardata.push(Math.round(Math.random() * 10) + 10);
  20. }
  21. // Main variables
  22. var d3Container = d3.select(element),
  23. width = d3Container.node().getBoundingClientRect().width;
  24. // Construct scales
  25. // ------------------------------
  26. // Horizontal
  27. var x = d3.scale.ordinal()
  28. .rangeBands([0, width], 0.3);
  29. // Vertical
  30. var y = d3.scale.linear()
  31. .range([0, height]);
  32. // Set input domains
  33. // ------------------------------
  34. // Horizontal
  35. x.domain(d3.range(0, bardata.length));
  36. // Vertical
  37. y.domain([0, d3.max(bardata)]);
  38. // Create chart
  39. // ------------------------------
  40. // Add svg element
  41. var container = d3Container.append('svg');
  42. // Add SVG group
  43. var svg = container
  44. .attr('width', width)
  45. .attr('height', height)
  46. .append('g');
  47. //
  48. // Append chart elements
  49. //
  50. // Bars
  51. var bars = svg.selectAll('rect')
  52. .data(bardata)
  53. .enter()
  54. .append('rect')
  55. .attr('class', 'd3-random-bars')
  56. .attr('width', x.rangeBand())
  57. .attr('x', function(d,i) {
  58. return x(i);
  59. })
  60. .style('fill', color);
  61. // Tooltip
  62. // ------------------------------
  63. // Initiate
  64. var tip = d3.tip()
  65. .attr('class', 'd3-tip')
  66. .offset([-10, 0]);
  67. // Show and hide
  68. if(tooltip == "hours" || tooltip == "goal" || tooltip == "members") {
  69. bars.call(tip)
  70. .on('mouseover', tip.show)
  71. .on('mouseout', tip.hide);
  72. }
  73. // Daily meetings tooltip content
  74. if(tooltip == "hours") {
  75. tip.html(function (d, i) {
  76. return "<div class='text-center'>" +
  77. "<h6 class='mb-0'>" + d + "</h6>" +
  78. "<span class='font-size-16'>meetings</span>" +
  79. "<div class='font-size-16'>" + i + ":00" + "</div>" +
  80. "</div>";
  81. });
  82. }
  83. // Statements tooltip content
  84. if(tooltip == "goal") {
  85. tip.html(function (d, i) {
  86. return "<div class='text-center'>" +
  87. "<h6 class='mb-0'>" + d + "</h6>" +
  88. "<span class='font-size-16'>statements</span>" +
  89. "<div class='font-size-16'>" + i + ":00" + "</div>" +
  90. "</div>";
  91. });
  92. }
  93. // Online members tooltip content
  94. if(tooltip == "members") {
  95. tip.html(function (d, i) {
  96. return "<div class='text-center bg-dark p-5'>" +
  97. "<h6 class='mb-0'>" + d + "0" + "</h6>" +
  98. "<span class='font-size-14'>members</span>" +
  99. "<div class='font-size-14'>" + i + ":00" + "</div>" +
  100. "</div>";
  101. });
  102. }
  103. // Bar loading animation
  104. // ------------------------------
  105. // Choose between animated or static
  106. if(animate) {
  107. withAnimation();
  108. } else {
  109. withoutAnimation();
  110. }
  111. // Animate on load
  112. function withAnimation() {
  113. bars
  114. .attr('height', 0)
  115. .attr('y', height)
  116. .transition()
  117. .attr('height', function(d) {
  118. return y(d);
  119. })
  120. .attr('y', function(d) {
  121. return height - y(d);
  122. })
  123. .delay(function(d, i) {
  124. return i * delay;
  125. })
  126. .duration(duration)
  127. .ease(easing);
  128. }
  129. // Load without animateion
  130. function withoutAnimation() {
  131. bars
  132. .attr('height', function(d) {
  133. return y(d);
  134. })
  135. .attr('y', function(d) {
  136. return height - y(d);
  137. });
  138. }
  139. // Resize chart
  140. // ------------------------------
  141. // Call function on window resize
  142. $(window).on('resize', barsResize);
  143. // Call function on sidebar width change
  144. $(document).on('click', '.sidebar-control', barsResize);
  145. // Resize function
  146. //
  147. // Since D3 doesn't support SVG resize by default,
  148. // we need to manually specify parts of the graph that need to
  149. // be updated on window resize
  150. function barsResize() {
  151. // Layout variables
  152. width = d3Container.node().getBoundingClientRect().width;
  153. // Layout
  154. // -------------------------
  155. // Main svg width
  156. container.attr("width", width);
  157. // Width of appended group
  158. svg.attr("width", width);
  159. // Horizontal range
  160. x.rangeBands([0, width], 0.3);
  161. // Chart elements
  162. // -------------------------
  163. // Bars
  164. svg.selectAll('.d3-random-bars')
  165. .attr('width', x.rangeBand())
  166. .attr('x', function(d,i) {
  167. return x(i);
  168. });
  169. }
  170. }
  171. };
  172. // Simple sparklines
  173. var _sparklinesWidget = function(element, chartType, qty, chartHeight, interpolation, duration, interval, color) {
  174. if (typeof d3 == 'undefined') {
  175. console.warn('Warning - d3.min.js is not loaded.');
  176. return;
  177. }
  178. // Initialize chart only if element exsists in the DOM
  179. if(element) {
  180. // Basic setup
  181. // ------------------------------
  182. // Define main variables
  183. var d3Container = d3.select(element),
  184. margin = {top: 0, right: 0, bottom: 0, left: 0},
  185. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
  186. height = chartHeight - margin.top - margin.bottom;
  187. // Generate random data (for demo only)
  188. var data = [];
  189. for (var i=0; i < qty; i++) {
  190. data.push(Math.floor(Math.random() * qty) + 5);
  191. }
  192. // Construct scales
  193. // ------------------------------
  194. // Horizontal
  195. var x = d3.scale.linear().range([0, width]);
  196. // Vertical
  197. var y = d3.scale.linear().range([height - 5, 5]);
  198. // Set input domains
  199. // ------------------------------
  200. // Horizontal
  201. x.domain([1, qty - 3]);
  202. // Vertical
  203. y.domain([0, qty]);
  204. // Construct chart layout
  205. // ------------------------------
  206. // Line
  207. var line = d3.svg.line()
  208. .interpolate(interpolation)
  209. .x(function(d, i) { return x(i); })
  210. .y(function(d, i) { return y(d); });
  211. // Area
  212. var area = d3.svg.area()
  213. .interpolate(interpolation)
  214. .x(function(d,i) {
  215. return x(i);
  216. })
  217. .y0(height)
  218. .y1(function(d) {
  219. return y(d);
  220. });
  221. // Create SVG
  222. // ------------------------------
  223. // Container
  224. var container = d3Container.append('svg');
  225. // SVG element
  226. var svg = container
  227. .attr('width', width + margin.left + margin.right)
  228. .attr('height', height + margin.top + margin.bottom)
  229. .append("g")
  230. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  231. // Add mask for animation
  232. // ------------------------------
  233. // Add clip path
  234. var clip = svg.append("defs")
  235. .append("clipPath")
  236. .attr('id', function(d, i) { return "load-clip-" + element.substring(1); });
  237. // Add clip shape
  238. var clips = clip.append("rect")
  239. .attr('class', 'load-clip')
  240. .attr("width", 0)
  241. .attr("height", height);
  242. // Animate mask
  243. clips
  244. .transition()
  245. .duration(1000)
  246. .ease('linear')
  247. .attr("width", width);
  248. //
  249. // Append chart elements
  250. //
  251. // Main path
  252. var path = svg.append("g")
  253. .attr("clip-path", function(d, i) { return "url(#load-clip-" + element.substring(1) + ")"; })
  254. .append("path")
  255. .datum(data)
  256. .attr("transform", "translate(" + x(0) + ",0)");
  257. // Add path based on chart type
  258. if(chartType == "area") {
  259. path.attr("d", area).attr('class', 'd3-area').style("fill", color); // area
  260. }
  261. else {
  262. path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color); // line
  263. }
  264. // Animate path
  265. path
  266. .style('opacity', 0)
  267. .transition()
  268. .duration(500)
  269. .style('opacity', 1);
  270. // Set update interval. For demo only
  271. // ------------------------------
  272. setInterval(function() {
  273. // push a new data point onto the back
  274. data.push(Math.floor(Math.random() * qty) + 5);
  275. // pop the old data point off the front
  276. data.shift();
  277. update();
  278. }, interval);
  279. // Update random data. For demo only
  280. // ------------------------------
  281. function update() {
  282. // Redraw the path and slide it to the left
  283. path
  284. .attr("transform", null)
  285. .transition()
  286. .duration(duration)
  287. .ease("linear")
  288. .attr("transform", "translate(" + x(0) + ",0)");
  289. // Update path type
  290. if(chartType == "area") {
  291. path.attr("d", area).attr('class', 'd3-area').style("fill", color);
  292. }
  293. else {
  294. path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color);
  295. }
  296. }
  297. // Resize chart
  298. // ------------------------------
  299. // Call function on window resize
  300. $(window).on('resize', resizeSparklines);
  301. // Call function on sidebar width change
  302. $(document).on('click', '.sidebar-control', resizeSparklines);
  303. // Resize function
  304. //
  305. // Since D3 doesn't support SVG resize by default,
  306. // we need to manually specify parts of the graph that need to
  307. // be updated on window resize
  308. function resizeSparklines() {
  309. // Layout variables
  310. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
  311. // Layout
  312. // -------------------------
  313. // Main svg width
  314. container.attr("width", width + margin.left + margin.right);
  315. // Width of appended group
  316. svg.attr("width", width + margin.left + margin.right);
  317. // Horizontal range
  318. x.range([0, width]);
  319. // Chart elements
  320. // -------------------------
  321. // Clip mask
  322. clips.attr("width", width);
  323. // Line
  324. svg.select(".d3-line").attr("d", line);
  325. // Area
  326. svg.select(".d3-area").attr("d", area);
  327. }
  328. }
  329. };
  330. //
  331. // Return objects assigned to module
  332. //
  333. return {
  334. init: function() {
  335. _barChartWidget("#chart_bar_basic", 24, 50, true, "elastic", 1200, 50, "#3e8ef7", "members");
  336. _sparklinesWidget("#sparklines_basic", "area", 30, 50, "basis", 750, 2000, "#0bb2d4");
  337. }
  338. }
  339. }();
  340. // Initialize module
  341. // ------------------------------
  342. // When content loaded
  343. document.addEventListener('DOMContentLoaded', function() {
  344. Widgetschart.init();
  345. });
  346. // donut chart
  347. $('.donut').peity('donut');
  348. // ------------------------------
  349. // Basic bar chart
  350. // ------------------------------
  351. // based on prepared DOM, initialize echarts instance
  352. var myChart = echarts.init(document.getElementById('basic-bar'));
  353. // specify chart configuration item and data
  354. var option = {
  355. // Setup grid
  356. grid: {
  357. left: '1%',
  358. right: '2%',
  359. bottom: '3%',
  360. containLabel: true
  361. },
  362. // Add Tooltip
  363. tooltip : {
  364. trigger: 'axis'
  365. },
  366. legend: {
  367. data:['Site A','Site B']
  368. },
  369. toolbox: {
  370. show : true,
  371. feature : {
  372. magicType : {show: true, type: ['line', 'bar']},
  373. restore : {show: true},
  374. saveAsImage : {show: true}
  375. }
  376. },
  377. color: ["#689f38", "#389f99"],
  378. calculable : true,
  379. xAxis : [
  380. {
  381. type : 'category',
  382. data : ['Jan','Feb','Mar','Apr','May','Jun','July','Aug','Sept','Oct','Nov','Dec']
  383. }
  384. ],
  385. yAxis : [
  386. {
  387. type : 'value'
  388. }
  389. ],
  390. series : [
  391. {
  392. name:'Site A',
  393. type:'bar',
  394. data:[7.2, 5.3, 6.1, 32.1, 23.1, 89.2, 158.4, 178.1, 36.4, 22.7, 7.1, 9.4],
  395. markPoint : {
  396. data : [
  397. {type : 'max', name: 'Max'},
  398. {type : 'min', name: 'Min'}
  399. ]
  400. },
  401. markLine : {
  402. data : [
  403. {type : 'average', name: 'Average'}
  404. ]
  405. }
  406. },
  407. {
  408. name:'Site B',
  409. type:'bar',
  410. data:[19.4, 7.9, 8.9, 27.9, 24.8, 88.1, 167.8, 197.5, 47.1, 16.7, 7.1, 1.5],
  411. markPoint : {
  412. data : [
  413. {name : 'The highest year', value : 182.2, xAxis: 7, yAxis: 183, symbolSize:18},
  414. {name : 'Year minimum', value : 2.3, xAxis: 11, yAxis: 3}
  415. ]
  416. },
  417. markLine : {
  418. data : [
  419. {type : 'average', name : 'Average'}
  420. ]
  421. }
  422. }
  423. ]
  424. };
  425. // use configuration item and data specified to show chart
  426. myChart.setOption(option);