chart-widgets.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  1. //[custom Javascript]
  2. // Fullscreen
  3. $(function () {
  4. 'use strict'
  5. var options = {
  6. series: [{
  7. name: 'PRODUCT A',
  8. data: [44, 55, 41, 67, 22, 43]
  9. }, {
  10. name: 'PRODUCT B',
  11. data: [-44, -55, -41, -67, -22, -43]
  12. }],
  13. chart: {
  14. foreColor:"#bac0c7",
  15. type: 'bar',
  16. height: 350,
  17. stacked: true,
  18. toolbar: {
  19. show: false
  20. },
  21. zoom: {
  22. enabled: true
  23. }
  24. },
  25. responsive: [{
  26. breakpoint: 480,
  27. options: {
  28. legend: {
  29. position: 'bottom',
  30. offsetX: -10,
  31. offsetY: 0
  32. }
  33. }
  34. }],
  35. grid: {
  36. show: true,
  37. borderColor: '#f7f7f7',
  38. },
  39. colors:['#6993ff', '#f64e60'],
  40. plotOptions: {
  41. bar: {
  42. horizontal: false,
  43. columnWidth: '10%',
  44. endingShape: 'rounded'
  45. },
  46. },
  47. dataLabels: {
  48. enabled: false
  49. },
  50. xaxis: {
  51. type: 'datetime',
  52. categories: ['01/01/2011 GMT', '01/02/2011 GMT', '01/03/2011 GMT', '01/04/2011 GMT',
  53. '01/05/2011 GMT', '01/06/2011 GMT'
  54. ],
  55. },
  56. legend: {
  57. show: false,
  58. },
  59. fill: {
  60. opacity: 1
  61. }
  62. };
  63. var chart = new ApexCharts(document.querySelector("#charts_widget_1_chart"), options);
  64. chart.render();
  65. var options = {
  66. series: [{
  67. name: "Profit",
  68. data: [0, 40, 110, 70, 100, 60, 130, 55, 140, 125]
  69. }],
  70. chart: {
  71. foreColor:"#bac0c7",
  72. height: 350,
  73. type: 'area',
  74. zoom: {
  75. enabled: false
  76. }
  77. },
  78. colors:['#ea9715'],
  79. dataLabels: {
  80. enabled: false,
  81. },
  82. stroke: {
  83. show: true,
  84. curve: 'smooth',
  85. lineCap: 'butt',
  86. colors: undefined,
  87. width: 2,
  88. dashArray: 0,
  89. },
  90. markers: {
  91. size: 2,
  92. colors: '#ea9715',
  93. strokeColors: '#ffffff',
  94. strokeWidth: 2,
  95. strokeOpacity: 0.9,
  96. strokeDashArray: 0,
  97. fillOpacity: 1,
  98. discrete: [],
  99. shape: "circle",
  100. radius: 5,
  101. offsetX: 0,
  102. offsetY: 0,
  103. onClick: undefined,
  104. onDblClick: undefined,
  105. hover: {
  106. size: undefined,
  107. sizeOffset: 3
  108. }
  109. },
  110. grid: {
  111. borderColor: '#f7f7f7',
  112. row: {
  113. colors: ['transparent'], // takes an array which will be repeated on columns
  114. opacity: 0
  115. },
  116. yaxis: {
  117. lines: {
  118. show: true,
  119. },
  120. },
  121. },
  122. fill: {
  123. type: "gradient",
  124. gradient: {
  125. shadeIntensity: 1,
  126. opacityFrom: 0.01,
  127. opacityTo: 1,
  128. stops: [0, 90, 100]
  129. }
  130. },
  131. xaxis: {
  132. categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct'],
  133. labels: {
  134. show: true,
  135. },
  136. axisBorder: {
  137. show: true
  138. },
  139. axisTicks: {
  140. show: true
  141. },
  142. tooltip: {
  143. enabled: true,
  144. },
  145. },
  146. yaxis: {
  147. labels: {
  148. show: true,
  149. formatter: function (val) {
  150. return val + "K";
  151. }
  152. }
  153. },
  154. };
  155. var chart = new ApexCharts(document.querySelector("#charts_widget_2_chart"), options);
  156. chart.render();
  157. // Composite line charts, the second using values supplied via javascript
  158. $("#linechart").sparkline([32,24,26,24,32,26,40,34,22,24], {
  159. type: 'line',
  160. width: '100',
  161. height: '38',
  162. lineColor: '#f64e60',
  163. fillColor: '#ffffff',
  164. lineWidth: 2,
  165. minSpotColor: '#f64e60',
  166. maxSpotColor: '#f64e60',
  167. });
  168. $("#linechart2").sparkline([32,24,26,24,32,26,40,34,22,24], {
  169. type: 'line',
  170. width: '100',
  171. height: '38',
  172. lineColor: '#faa700',
  173. fillColor: '#ffffff',
  174. lineWidth: 2,
  175. minSpotColor: '#faa700',
  176. maxSpotColor: '#faa700',
  177. });
  178. $("#linechart3").sparkline([32,24,26,24,32,26,40,34,22,24,22,24,34,32,38,28,36,36,40,38,30,34,38], {
  179. type: 'line',
  180. width: '100%',
  181. height: '45',
  182. lineColor: '#1bc5bd',
  183. fillColor: '#ffffff',
  184. lineWidth: 2,
  185. minSpotColor: '#f64e60',
  186. maxSpotColor: '#f64e60',
  187. });
  188. $("#linechart4").sparkline([32,24,26,24,32,26,40,34,22,24], {
  189. type: 'line',
  190. width: '50%',
  191. height: '100',
  192. lineColor: '#1bc5bd',
  193. fillColor: '#ffffff',
  194. lineWidth: 2,
  195. minSpotColor: '#f64e60',
  196. maxSpotColor: '#f64e60',
  197. });
  198. $("#barchart").sparkline([32,24,26,24,32,26,40,34,22,24], {
  199. type: 'bar',
  200. height: '38',
  201. width: '100%',
  202. barWidth: 6,
  203. barSpacing: 4,
  204. barColor: '#3e8ef7',
  205. });
  206. $("#barchart2").sparkline([32,24,26,24,32,26,40,34,22,24], {
  207. type: 'bar',
  208. height: '38',
  209. barWidth: 6,
  210. barSpacing: 4,
  211. barColor: '#3e8ef7',
  212. });
  213. $("#barchart3").sparkline([32,24,26,24,32,26,40,34,22,24,22,24,34,32,38,28,36,36,40,38,30,34,38], {
  214. type: 'bar',
  215. height: '45',
  216. width: '100%',
  217. barWidth: 6,
  218. barSpacing: 4,
  219. barColor: '#f64e60',
  220. });
  221. $("#barchart4").sparkline([32,24,26,24,32,26,40,34,22,24], {
  222. type: 'bar',
  223. height: '100',
  224. width: '50%',
  225. barWidth: 6,
  226. barSpacing: 4,
  227. barColor: '#f64e60',
  228. });
  229. $("#discretechart").sparkline([32,24,26,24,32,26,40,34,22,24], {
  230. type: 'discrete',
  231. width: '50',
  232. height: '38',
  233. lineColor: '#6993ff',
  234. });
  235. $("#discretechart2").sparkline([32,24,26,24,32,26,40,34,22,24], {
  236. type: 'discrete',
  237. width: '50',
  238. height: '38',
  239. lineColor: '#6993ff',
  240. });
  241. $("#discretechart3").sparkline([32,24,26,24,32,26,40,34,22,24,22,24,34,32,38,28,36,36,40,38,30,34,38], {
  242. type: 'discrete',
  243. width: '180',
  244. height: '45',
  245. lineColor: '#0bb2d4',
  246. });
  247. $("#discretechart4").sparkline([32,24,26,24,32,26,40,34,22,24], {
  248. type: 'discrete',
  249. width: '100',
  250. height: '100',
  251. lineColor: '#0bb2d4',
  252. });
  253. $("#linearea").sparkline([32,24,26,24,32,26,40,34,22,24], {
  254. type: 'line',
  255. width: '100%',
  256. height: '80',
  257. lineColor: '#1bc5bd',
  258. fillColor: '#1bc5bd',
  259. lineWidth: 2,
  260. });
  261. $("#linearea2").sparkline([32,24,26,24,32,26,40,34,22,24,22,24,34,32,38,28,36,36,40,38,30,34,38], {
  262. type: 'line',
  263. width: '100%',
  264. height: '45',
  265. lineColor: '#0bb2d4',
  266. fillColor: '#0bb2d4',
  267. lineWidth: 2,
  268. });
  269. $("#linearea3").sparkline([32,24,26,24,32,26,40,34,22,24], {
  270. type: 'line',
  271. width: '50%',
  272. height: '100',
  273. lineColor: '#0bb2d4',
  274. fillColor: '#0bb2d4',
  275. lineWidth: 1,
  276. });
  277. $("#baralc").sparkline([32,24,26,24,32,26,40,34,22,24,22,24,34,32,38,28,36,36,40,38,30,34,38], {
  278. type: 'bar',
  279. height: '80',
  280. barWidth: 6,
  281. barSpacing: 4,
  282. barColor: '#faa700',
  283. });
  284. $("#lineIncrease").sparkline([32,24,26,24,32,26,40,34,22,24], {
  285. type: 'line',
  286. width: '98%',
  287. height: '92',
  288. lineWidth: 2,
  289. lineColor: '#ffffff',
  290. fillColor: "rgba(255, 255, 255, 0)",
  291. spotColor: '#ffffff',
  292. minSpotColor: '#ffffff',
  293. maxSpotColor: '#ffffff',
  294. spotRadius: 1,
  295. });
  296. $("#lineToday").sparkline([32,24,26,24,32,26,40,34,22,24], {
  297. type: 'line',
  298. width: '100%',
  299. height: '70',
  300. lineColor: '#ffffff',
  301. fillColor: 'rgba(255, 255, 255, 0)',
  302. lineWidth: 2,
  303. spotRadius: 3,
  304. });
  305. $("#baranl").sparkline([32,24,26,24,32,26,40,34,22,24,22,24,34,32,38,28,36,36,40,38,30,34,38], {
  306. type: 'bar',
  307. height: '70',
  308. barColor: '#f64e60',
  309. barWidth: 7,
  310. barSpacing: 5,
  311. });
  312. $("#lineTo").sparkline([32,24,26,24,32,26,40,34,22,24], {
  313. type: 'line',
  314. width: '100%',
  315. height: '70',
  316. lineColor: '#ffffff',
  317. fillColor: 'rgba(255, 255, 255, 0)',
  318. lineWidth: 3,
  319. spotColor: '#ffffff',
  320. highlightSpotColor: '#ffffff',
  321. highlightLineColor: '#ffffff',
  322. spotRadius: 3,
  323. });
  324. // donut chart
  325. $('.donut').peity('donut');
  326. // bar chart
  327. $(".bar").peity("bar");
  328. $('.countnm').each(function () {
  329. $(this).prop('Counter',0).animate({
  330. Counter: $(this).text()
  331. }, {
  332. duration: 1000,
  333. easing: 'swing',
  334. step: function (now) {
  335. $(this).text(Math.ceil(now));
  336. }
  337. });
  338. });
  339. }); // End of use strict
  340. // easypie chart
  341. $(function() {
  342. 'use strict'
  343. $('.easypie').easyPieChart({
  344. easing: 'easeOutBounce',
  345. onStep: function(from, to, percent) {
  346. $(this.el).find('.percent').text(Math.round(percent));
  347. }
  348. });
  349. var chart = window.chart = $('.easypie').data('easyPieChart');
  350. $('.js_update').on('click', function() {
  351. chart.update(Math.random()*200-100);
  352. });
  353. });// End of use strict
  354. // ------------------------------
  355. var Widgetschart = function() {
  356. // Simple bar charts
  357. var _barChartWidget = function(element, barQty, height, animate, easing, duration, delay, color, tooltip) {
  358. if (typeof d3 == 'undefined') {
  359. console.warn('Warning - d3.min.js is not loaded.');
  360. return;
  361. }
  362. // Initialize chart only if element exsists in the DOM
  363. if(element) {
  364. // Basic setup
  365. // ------------------------------
  366. // Add data set
  367. var bardata = [];
  368. for (var i=0; i < barQty; i++) {
  369. bardata.push(Math.round(Math.random() * 10) + 10);
  370. }
  371. // Main variables
  372. var d3Container = d3.select(element),
  373. width = d3Container.node().getBoundingClientRect().width;
  374. // Construct scales
  375. // ------------------------------
  376. // Horizontal
  377. var x = d3.scale.ordinal()
  378. .rangeBands([0, width], 0.3);
  379. // Vertical
  380. var y = d3.scale.linear()
  381. .range([0, height]);
  382. // Set input domains
  383. // ------------------------------
  384. // Horizontal
  385. x.domain(d3.range(0, bardata.length));
  386. // Vertical
  387. y.domain([0, d3.max(bardata)]);
  388. // Create chart
  389. // ------------------------------
  390. // Add svg element
  391. var container = d3Container.append('svg');
  392. // Add SVG group
  393. var svg = container
  394. .attr('width', width)
  395. .attr('height', height)
  396. .append('g');
  397. //
  398. // Append chart elements
  399. //
  400. // Bars
  401. var bars = svg.selectAll('rect')
  402. .data(bardata)
  403. .enter()
  404. .append('rect')
  405. .attr('class', 'd3-random-bars')
  406. .attr('width', x.rangeBand())
  407. .attr('x', function(d,i) {
  408. return x(i);
  409. })
  410. .style('fill', color);
  411. // Tooltip
  412. // ------------------------------
  413. // Initiate
  414. var tip = d3.tip()
  415. .attr('class', 'd3-tip')
  416. .offset([-10, 0]);
  417. // Show and hide
  418. if(tooltip == "hours" || tooltip == "goal" || tooltip == "members") {
  419. bars.call(tip)
  420. .on('mouseover', tip.show)
  421. .on('mouseout', tip.hide);
  422. }
  423. // Daily meetings tooltip content
  424. if(tooltip == "hours") {
  425. tip.html(function (d, i) {
  426. return "<div class='text-center'>" +
  427. "<h6 class='mb-0'>" + d + "</h6>" +
  428. "<span class='font-size-16'>meetings</span>" +
  429. "<div class='font-size-16'>" + i + ":00" + "</div>" +
  430. "</div>";
  431. });
  432. }
  433. // Statements tooltip content
  434. if(tooltip == "goal") {
  435. tip.html(function (d, i) {
  436. return "<div class='text-center'>" +
  437. "<h6 class='mb-0'>" + d + "</h6>" +
  438. "<span class='font-size-16'>statements</span>" +
  439. "<div class='font-size-16'>" + i + ":00" + "</div>" +
  440. "</div>";
  441. });
  442. }
  443. // Online members tooltip content
  444. if(tooltip == "members") {
  445. tip.html(function (d, i) {
  446. return "<div class='text-center bg-dark p-5'>" +
  447. "<h6 class='mb-0'>" + d + "0" + "</h6>" +
  448. "<span class='font-size-14'>members</span>" +
  449. "<div class='font-size-14'>" + i + ":00" + "</div>" +
  450. "</div>";
  451. });
  452. }
  453. // Bar loading animation
  454. // ------------------------------
  455. // Choose between animated or static
  456. if(animate) {
  457. withAnimation();
  458. } else {
  459. withoutAnimation();
  460. }
  461. // Animate on load
  462. function withAnimation() {
  463. bars
  464. .attr('height', 0)
  465. .attr('y', height)
  466. .transition()
  467. .attr('height', function(d) {
  468. return y(d);
  469. })
  470. .attr('y', function(d) {
  471. return height - y(d);
  472. })
  473. .delay(function(d, i) {
  474. return i * delay;
  475. })
  476. .duration(duration)
  477. .ease(easing);
  478. }
  479. // Load without animateion
  480. function withoutAnimation() {
  481. bars
  482. .attr('height', function(d) {
  483. return y(d);
  484. })
  485. .attr('y', function(d) {
  486. return height - y(d);
  487. });
  488. }
  489. // Resize chart
  490. // ------------------------------
  491. // Call function on window resize
  492. $(window).on('resize', barsResize);
  493. // Call function on sidebar width change
  494. $(document).on('click', '.sidebar-control', barsResize);
  495. // Resize function
  496. //
  497. // Since D3 doesn't support SVG resize by default,
  498. // we need to manually specify parts of the graph that need to
  499. // be updated on window resize
  500. function barsResize() {
  501. // Layout variables
  502. width = d3Container.node().getBoundingClientRect().width;
  503. // Layout
  504. // -------------------------
  505. // Main svg width
  506. container.attr("width", width);
  507. // Width of appended group
  508. svg.attr("width", width);
  509. // Horizontal range
  510. x.rangeBands([0, width], 0.3);
  511. // Chart elements
  512. // -------------------------
  513. // Bars
  514. svg.selectAll('.d3-random-bars')
  515. .attr('width', x.rangeBand())
  516. .attr('x', function(d,i) {
  517. return x(i);
  518. });
  519. }
  520. }
  521. };
  522. // Simple line chart
  523. var _lineChartWidget = function(element, chartHeight, lineColor, pathColor, pointerLineColor, pointerBgColor) {
  524. if (typeof d3 == 'undefined') {
  525. console.warn('Warning - d3.min.js is not loaded.');
  526. return;
  527. }
  528. // Initialize chart only if element exsists in the DOM
  529. if(element) {
  530. // Basic setup
  531. // ------------------------------
  532. // Add data set
  533. var dataset = [
  534. {
  535. "date": "04/13/14",
  536. "alpha": "60"
  537. }, {
  538. "date": "04/14/14",
  539. "alpha": "35"
  540. }, {
  541. "date": "04/15/14",
  542. "alpha": "65"
  543. }, {
  544. "date": "04/16/14",
  545. "alpha": "50"
  546. }, {
  547. "date": "04/17/14",
  548. "alpha": "65"
  549. }, {
  550. "date": "04/18/14",
  551. "alpha": "20"
  552. }, {
  553. "date": "04/19/14",
  554. "alpha": "60"
  555. }
  556. ];
  557. // Main variables
  558. var d3Container = d3.select(element),
  559. margin = {top: 0, right: 0, bottom: 0, left: 0},
  560. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
  561. height = chartHeight - margin.top - margin.bottom,
  562. padding = 20;
  563. // Format date
  564. var parseDate = d3.time.format("%m/%d/%y").parse,
  565. formatDate = d3.time.format("%a, %B %e");
  566. // Add tooltip
  567. // ------------------------------
  568. var tooltip = d3.tip()
  569. .attr('class', 'd3-tip')
  570. .html(function (d) {
  571. return "<ul class='list-unstyled mb-1 bg-dark p-5'>" +
  572. "<li>" + "<div class='my-1'><i class='fa fa-check mr-2'></i>" + formatDate(d.date) + "</div>" + "</li>" +
  573. "<li>" + "Sales: &nbsp;" + "<span class='float-right'>" + d.alpha + "</span>" + "</li>" +
  574. "<li>" + "Revenue: &nbsp; " + "<span class='float-right'>" + "$" + (d.alpha * 0).toFixed(2) + "</span>" + "</li>" +
  575. "</ul>";
  576. });
  577. // Create chart
  578. // ------------------------------
  579. // Add svg element
  580. var container = d3Container.append('svg');
  581. // Add SVG group
  582. var svg = container
  583. .attr('width', width + margin.left + margin.right)
  584. .attr('height', height + margin.top + margin.bottom)
  585. .append("g")
  586. .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
  587. .call(tooltip);
  588. // Load data
  589. // ------------------------------
  590. dataset.forEach(function (d) {
  591. d.date = parseDate(d.date);
  592. d.alpha = +d.alpha;
  593. });
  594. // Construct scales
  595. // ------------------------------
  596. // Horizontal
  597. var x = d3.time.scale()
  598. .range([padding, width - padding]);
  599. // Vertical
  600. var y = d3.scale.linear()
  601. .range([height, 5]);
  602. // Set input domains
  603. // ------------------------------
  604. // Horizontal
  605. x.domain(d3.extent(dataset, function (d) {
  606. return d.date;
  607. }));
  608. // Vertical
  609. y.domain([0, d3.max(dataset, function (d) {
  610. return Math.max(d.alpha);
  611. })]);
  612. // Construct chart layout
  613. // ------------------------------
  614. // Line
  615. var line = d3.svg.line()
  616. .x(function(d) {
  617. return x(d.date);
  618. })
  619. .y(function(d) {
  620. return y(d.alpha);
  621. });
  622. //
  623. // Append chart elements
  624. //
  625. // Add mask for animation
  626. // ------------------------------
  627. // Add clip path
  628. var clip = svg.append("defs")
  629. .append("clipPath")
  630. .attr("id", "clip-line-small");
  631. // Add clip shape
  632. var clipRect = clip.append("rect")
  633. .attr('class', 'clip')
  634. .attr("width", 0)
  635. .attr("height", height);
  636. // Animate mask
  637. clipRect
  638. .transition()
  639. .duration(1000)
  640. .ease('linear')
  641. .attr("width", width);
  642. // Line
  643. // ------------------------------
  644. // Path
  645. var path = svg.append('path')
  646. .attr({
  647. 'd': line(dataset),
  648. "clip-path": "url(#clip-line-small)",
  649. 'class': 'd3-line d3-line-medium'
  650. })
  651. .style('stroke', lineColor);
  652. // Animate path
  653. svg.select('.line-tickets')
  654. .transition()
  655. .duration(1000)
  656. .ease('linear');
  657. // Add vertical guide lines
  658. // ------------------------------
  659. // Bind data
  660. var guide = svg.append('g')
  661. .selectAll('.d3-line-guides-group')
  662. .data(dataset);
  663. // Append lines
  664. guide
  665. .enter()
  666. .append('line')
  667. .attr('class', 'd3-line-guides')
  668. .attr('x1', function (d, i) {
  669. return x(d.date);
  670. })
  671. .attr('y1', function (d, i) {
  672. return height;
  673. })
  674. .attr('x2', function (d, i) {
  675. return x(d.date);
  676. })
  677. .attr('y2', function (d, i) {
  678. return height;
  679. })
  680. .style('stroke', pathColor)
  681. .style('stroke-dasharray', '4,2')
  682. .style('shape-rendering', 'crispEdges');
  683. // Animate guide lines
  684. guide
  685. .transition()
  686. .duration(1000)
  687. .delay(function(d, i) { return i * 150; })
  688. .attr('y2', function (d, i) {
  689. return y(d.alpha);
  690. });
  691. // Alpha app points
  692. // ------------------------------
  693. // Add points
  694. var points = svg.insert('g')
  695. .selectAll('.d3-line-circle')
  696. .data(dataset)
  697. .enter()
  698. .append('circle')
  699. .attr('class', 'd3-line-circle d3-line-circle-medium')
  700. .attr("cx", line.x())
  701. .attr("cy", line.y())
  702. .attr("r", 3)
  703. .style({
  704. 'stroke': pointerLineColor,
  705. 'fill': pointerBgColor
  706. });
  707. // Animate points on page load
  708. points
  709. .style('opacity', 0)
  710. .transition()
  711. .duration(250)
  712. .ease('linear')
  713. .delay(1000)
  714. .style('opacity', 1);
  715. // Add user interaction
  716. points
  717. .on("mouseover", function (d) {
  718. tooltip.offset([-10, 0]).show(d);
  719. // Animate circle radius
  720. d3.select(this).transition().duration(250).attr('r', 4);
  721. })
  722. // Hide tooltip
  723. .on("mouseout", function (d) {
  724. tooltip.hide(d);
  725. // Animate circle radius
  726. d3.select(this).transition().duration(250).attr('r', 3);
  727. });
  728. // Change tooltip direction of first point
  729. d3.select(points[0][0])
  730. .on("mouseover", function (d) {
  731. tooltip.offset([0, 10]).direction('e').show(d);
  732. // Animate circle radius
  733. d3.select(this).transition().duration(250).attr('r', 4);
  734. })
  735. .on("mouseout", function (d) {
  736. tooltip.direction('n').hide(d);
  737. // Animate circle radius
  738. d3.select(this).transition().duration(250).attr('r', 3);
  739. });
  740. // Change tooltip direction of last point
  741. d3.select(points[0][points.size() - 1])
  742. .on("mouseover", function (d) {
  743. tooltip.offset([0, -10]).direction('w').show(d);
  744. // Animate circle radius
  745. d3.select(this).transition().duration(250).attr('r', 4);
  746. })
  747. .on("mouseout", function (d) {
  748. tooltip.direction('n').hide(d);
  749. // Animate circle radius
  750. d3.select(this).transition().duration(250).attr('r', 3);
  751. });
  752. // Resize chart
  753. // ------------------------------
  754. // Call function on window resize
  755. $(window).on('resize', lineChartResize);
  756. // Call function on sidebar width change
  757. $(document).on('click', '.sidebar-control', lineChartResize);
  758. // Resize function
  759. //
  760. // Since D3 doesn't support SVG resize by default,
  761. // we need to manually specify parts of the graph that need to
  762. // be updated on window resize
  763. function lineChartResize() {
  764. // Layout variables
  765. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
  766. // Layout
  767. // -------------------------
  768. // Main svg width
  769. container.attr("width", width + margin.left + margin.right);
  770. // Width of appended group
  771. svg.attr("width", width + margin.left + margin.right);
  772. // Horizontal range
  773. x.range([padding, width - padding]);
  774. // Chart elements
  775. // -------------------------
  776. // Mask
  777. clipRect.attr("width", width);
  778. // Line path
  779. svg.selectAll('.d3-line').attr("d", line(dataset));
  780. // Circles
  781. svg.selectAll('.d3-line-circle').attr("cx", line.x());
  782. // Guide lines
  783. svg.selectAll('.d3-line-guides')
  784. .attr('x1', function (d, i) {
  785. return x(d.date);
  786. })
  787. .attr('x2', function (d, i) {
  788. return x(d.date);
  789. });
  790. }
  791. }
  792. };
  793. // Simple sparklines
  794. var _sparklinesWidget = function(element, chartType, qty, chartHeight, interpolation, duration, interval, color) {
  795. if (typeof d3 == 'undefined') {
  796. console.warn('Warning - d3.min.js is not loaded.');
  797. return;
  798. }
  799. // Initialize chart only if element exsists in the DOM
  800. if(element) {
  801. // Basic setup
  802. // ------------------------------
  803. // Define main variables
  804. var d3Container = d3.select(element),
  805. margin = {top: 0, right: 0, bottom: 0, left: 0},
  806. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right,
  807. height = chartHeight - margin.top - margin.bottom;
  808. // Generate random data (for demo only)
  809. var data = [];
  810. for (var i=0; i < qty; i++) {
  811. data.push(Math.floor(Math.random() * qty) + 5);
  812. }
  813. // Construct scales
  814. // ------------------------------
  815. // Horizontal
  816. var x = d3.scale.linear().range([0, width]);
  817. // Vertical
  818. var y = d3.scale.linear().range([height - 5, 5]);
  819. // Set input domains
  820. // ------------------------------
  821. // Horizontal
  822. x.domain([1, qty - 3]);
  823. // Vertical
  824. y.domain([0, qty]);
  825. // Construct chart layout
  826. // ------------------------------
  827. // Line
  828. var line = d3.svg.line()
  829. .interpolate(interpolation)
  830. .x(function(d, i) { return x(i); })
  831. .y(function(d, i) { return y(d); });
  832. // Area
  833. var area = d3.svg.area()
  834. .interpolate(interpolation)
  835. .x(function(d,i) {
  836. return x(i);
  837. })
  838. .y0(height)
  839. .y1(function(d) {
  840. return y(d);
  841. });
  842. // Create SVG
  843. // ------------------------------
  844. // Container
  845. var container = d3Container.append('svg');
  846. // SVG element
  847. var svg = container
  848. .attr('width', width + margin.left + margin.right)
  849. .attr('height', height + margin.top + margin.bottom)
  850. .append("g")
  851. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  852. // Add mask for animation
  853. // ------------------------------
  854. // Add clip path
  855. var clip = svg.append("defs")
  856. .append("clipPath")
  857. .attr('id', function(d, i) { return "load-clip-" + element.substring(1); });
  858. // Add clip shape
  859. var clips = clip.append("rect")
  860. .attr('class', 'load-clip')
  861. .attr("width", 0)
  862. .attr("height", height);
  863. // Animate mask
  864. clips
  865. .transition()
  866. .duration(1000)
  867. .ease('linear')
  868. .attr("width", width);
  869. //
  870. // Append chart elements
  871. //
  872. // Main path
  873. var path = svg.append("g")
  874. .attr("clip-path", function(d, i) { return "url(#load-clip-" + element.substring(1) + ")"; })
  875. .append("path")
  876. .datum(data)
  877. .attr("transform", "translate(" + x(0) + ",0)");
  878. // Add path based on chart type
  879. if(chartType == "area") {
  880. path.attr("d", area).attr('class', 'd3-area').style("fill", color); // area
  881. }
  882. else {
  883. path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color); // line
  884. }
  885. // Animate path
  886. path
  887. .style('opacity', 0)
  888. .transition()
  889. .duration(500)
  890. .style('opacity', 1);
  891. // Set update interval. For demo only
  892. // ------------------------------
  893. setInterval(function() {
  894. // push a new data point onto the back
  895. data.push(Math.floor(Math.random() * qty) + 5);
  896. // pop the old data point off the front
  897. data.shift();
  898. update();
  899. }, interval);
  900. // Update random data. For demo only
  901. // ------------------------------
  902. function update() {
  903. // Redraw the path and slide it to the left
  904. path
  905. .attr("transform", null)
  906. .transition()
  907. .duration(duration)
  908. .ease("linear")
  909. .attr("transform", "translate(" + x(0) + ",0)");
  910. // Update path type
  911. if(chartType == "area") {
  912. path.attr("d", area).attr('class', 'd3-area').style("fill", color);
  913. }
  914. else {
  915. path.attr("d", line).attr("class", "d3-line d3-line-medium").style('stroke', color);
  916. }
  917. }
  918. // Resize chart
  919. // ------------------------------
  920. // Call function on window resize
  921. $(window).on('resize', resizeSparklines);
  922. // Call function on sidebar width change
  923. $(document).on('click', '.sidebar-control', resizeSparklines);
  924. // Resize function
  925. //
  926. // Since D3 doesn't support SVG resize by default,
  927. // we need to manually specify parts of the graph that need to
  928. // be updated on window resize
  929. function resizeSparklines() {
  930. // Layout variables
  931. width = d3Container.node().getBoundingClientRect().width - margin.left - margin.right;
  932. // Layout
  933. // -------------------------
  934. // Main svg width
  935. container.attr("width", width + margin.left + margin.right);
  936. // Width of appended group
  937. svg.attr("width", width + margin.left + margin.right);
  938. // Horizontal range
  939. x.range([0, width]);
  940. // Chart elements
  941. // -------------------------
  942. // Clip mask
  943. clips.attr("width", width);
  944. // Line
  945. svg.select(".d3-line").attr("d", line);
  946. // Area
  947. svg.select(".d3-area").attr("d", area);
  948. }
  949. }
  950. };
  951. //
  952. // Return objects assigned to module
  953. //
  954. return {
  955. init: function() {
  956. _barChartWidget("#chart_bar_basic", 24, 50, true, "elastic", 1200, 50, "#3e8ef7", "members");
  957. _barChartWidget("#chart_bar_color", 24, 50, true, "elastic", 1200, 50, "rgba(255,255,255,0.75)", "members");
  958. _lineChartWidget('#line_chart_simple', 50, '#0bb2d4', '#0bb2d4', '#0bb2d4', '#fff');
  959. _lineChartWidget('#line_chart_color', 50, '#fff', 'rgba(255,255,255,0.5)', '#fff', '#29B6F6');
  960. _sparklinesWidget("#sparklines_basic", "area", 30, 50, "basis", 750, 2000, "#0bb2d4");
  961. _sparklinesWidget("#sparklines_color", "area", 30, 50, "basis", 750, 2000, "rgba(255,255,255,0.75)");
  962. }
  963. }
  964. }();
  965. // Initialize module
  966. // ------------------------------
  967. // When content loaded
  968. document.addEventListener('DOMContentLoaded', function() {
  969. Widgetschart.init();
  970. });