widget-chartist.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. //[widget charts Javascript]
  2. $( document ).ready(function() {
  3. "use strict";
  4. //-----demo-1
  5. new Chartist.Line('.ct-chart-1', {
  6. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
  7. series: [
  8. [12, 9, 7, 8, 5],
  9. [2, 1, 3.5, 7, 3],
  10. [1, 3, 4, 5, 6]
  11. ]
  12. }, {
  13. fullWidth: true,
  14. chartPadding: {
  15. right: 40
  16. }
  17. });
  18. //-----demo-2
  19. var chart = new Chartist.Line('.ct-chart-2', {
  20. labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
  21. series: [
  22. [5, 5, 10, 8, 7, 5, 4, null, null, null, 10, 10, 7, 8, 6, 9],
  23. [10, 15, null, 12, null, 10, 12, 15, null, null, 12, null, 14, null, null, null],
  24. [null, null, null, null, 3, 4, 1, 3, 4, 6, 7, 9, 5, null, null, null],
  25. [{x:3, y: 3},{x: 4, y: 3}, {x: 5, y: undefined}, {x: 6, y: 4}, {x: 7, y: null}, {x: 8, y: 4}, {x: 9, y: 4}]
  26. ]
  27. }, {
  28. fullWidth: true,
  29. chartPadding: {
  30. right: 10
  31. },
  32. low: 0
  33. });
  34. //-----demo-3
  35. var chart = new Chartist.Line('.ct-chart-3', {
  36. labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
  37. series: [
  38. [5, 5, 10, 8, 7, 5, 4, null, null, null, 10, 10, 7, 8, 6, 9],
  39. [10, 15, null, 12, null, 10, 12, 15, null, null, 12, null, 14, null, null, null],
  40. [null, null, null, null, 3, 4, 1, 3, 4, 6, 7, 9, 5, null, null, null],
  41. [{x:3, y: 3},{x: 4, y: 3}, {x: 5, y: undefined}, {x: 6, y: 4}, {x: 7, y: null}, {x: 8, y: 4}, {x: 9, y: 4}]
  42. ]
  43. }, {
  44. fullWidth: true,
  45. chartPadding: {
  46. right: 10
  47. },
  48. lineSmooth: Chartist.Interpolation.cardinal({
  49. fillHoles: true,
  50. }),
  51. low: 0
  52. });
  53. //-----demo-4
  54. new Chartist.Line('.ct-chart-4', {
  55. labels: [1, 2, 3, 4, 5, 6, 7, 8],
  56. series: [
  57. [5, 9, 7, 8, 5, 3, 5, 4]
  58. ]
  59. }, {
  60. low: 0,
  61. showArea: true
  62. });
  63. //-----demo-5
  64. new Chartist.Line('.ct-chart-5', {
  65. labels: [1, 2, 3, 4, 5, 6, 7, 8],
  66. series: [
  67. [1, 2, 3, 1, -2, 0, 1, 0],
  68. [-2, -1, -2, -1, -2.5, -1, -2, -1],
  69. [0, 0, 0, 1, 2, 2.5, 2, 1],
  70. [2.5, 2, 1, 0.5, 1, 0.5, -1, -2.5]
  71. ]
  72. }, {
  73. high: 3,
  74. low: -3,
  75. showArea: true,
  76. showLine: false,
  77. showPoint: false,
  78. fullWidth: true,
  79. axisX: {
  80. showLabel: false,
  81. showGrid: false
  82. }
  83. });
  84. //-----demo-6
  85. var chart = new Chartist.Line('.ct-chart-6', {
  86. labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  87. series: [
  88. [12, 9, 7, 8, 5, 4, 6, 2, 3, 3, 4, 6],
  89. [4, 5, 3, 7, 3, 5, 5, 3, 4, 4, 5, 5],
  90. [5, 3, 4, 5, 6, 3, 3, 4, 5, 6, 3, 4],
  91. [3, 4, 5, 6, 7, 6, 4, 5, 6, 7, 6, 3]
  92. ]
  93. }, {
  94. low: 0
  95. });
  96. // Let's put a sequence number aside so we can use it in the event callbacks
  97. var seq = 0,
  98. delays = 80,
  99. durations = 500;
  100. // Once the chart is fully created we reset the sequence
  101. chart.on('created', function() {
  102. seq = 0;
  103. });
  104. // On each drawn element by Chartist we use the Chartist.Svg API to trigger SMIL animations
  105. chart.on('draw', function(data) {
  106. seq++;
  107. if(data.type === 'line') {
  108. // If the drawn element is a line we do a simple opacity fade in. This could also be achieved using CSS3 animations.
  109. data.element.animate({
  110. opacity: {
  111. // The delay when we like to start the animation
  112. begin: seq * delays + 1000,
  113. // Duration of the animation
  114. dur: durations,
  115. // The value where the animation should start
  116. from: 0,
  117. // The value where it should end
  118. to: 1
  119. }
  120. });
  121. } else if(data.type === 'label' && data.axis === 'x') {
  122. data.element.animate({
  123. y: {
  124. begin: seq * delays,
  125. dur: durations,
  126. from: data.y + 100,
  127. to: data.y,
  128. // We can specify an easing function from Chartist.Svg.Easing
  129. easing: 'easeOutQuart'
  130. }
  131. });
  132. } else if(data.type === 'label' && data.axis === 'y') {
  133. data.element.animate({
  134. x: {
  135. begin: seq * delays,
  136. dur: durations,
  137. from: data.x - 100,
  138. to: data.x,
  139. easing: 'easeOutQuart'
  140. }
  141. });
  142. } else if(data.type === 'point') {
  143. data.element.animate({
  144. x1: {
  145. begin: seq * delays,
  146. dur: durations,
  147. from: data.x - 10,
  148. to: data.x,
  149. easing: 'easeOutQuart'
  150. },
  151. x2: {
  152. begin: seq * delays,
  153. dur: durations,
  154. from: data.x - 10,
  155. to: data.x,
  156. easing: 'easeOutQuart'
  157. },
  158. opacity: {
  159. begin: seq * delays,
  160. dur: durations,
  161. from: 0,
  162. to: 1,
  163. easing: 'easeOutQuart'
  164. }
  165. });
  166. } else if(data.type === 'grid') {
  167. // Using data.axis we get x or y which we can use to construct our animation definition objects
  168. var pos1Animation = {
  169. begin: seq * delays,
  170. dur: durations,
  171. from: data[data.axis.units.pos + '1'] - 30,
  172. to: data[data.axis.units.pos + '1'],
  173. easing: 'easeOutQuart'
  174. };
  175. var pos2Animation = {
  176. begin: seq * delays,
  177. dur: durations,
  178. from: data[data.axis.units.pos + '2'] - 100,
  179. to: data[data.axis.units.pos + '2'],
  180. easing: 'easeOutQuart'
  181. };
  182. var animations = {};
  183. animations[data.axis.units.pos + '1'] = pos1Animation;
  184. animations[data.axis.units.pos + '2'] = pos2Animation;
  185. animations['opacity'] = {
  186. begin: seq * delays,
  187. dur: durations,
  188. from: 0,
  189. to: 1,
  190. easing: 'easeOutQuart'
  191. };
  192. data.element.animate(animations);
  193. }
  194. });
  195. // For the sake of the example we update the chart every time it's created with a delay of 10 seconds
  196. chart.on('created', function() {
  197. if(window.__exampleAnimateTimeout) {
  198. clearTimeout(window.__exampleAnimateTimeout);
  199. window.__exampleAnimateTimeout = null;
  200. }
  201. window.__exampleAnimateTimeout = setTimeout(chart.update.bind(chart), 12000);
  202. });
  203. //-----demo-7
  204. var chart = new Chartist.Line('.ct-chart-7', {
  205. labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  206. series: [
  207. [1, 5, 2, 5, 4, 3],
  208. [2, 3, 4, 8, 1, 2],
  209. [5, 4, 3, 2, 1, 0.5]
  210. ]
  211. }, {
  212. low: 0,
  213. showArea: true,
  214. showPoint: false,
  215. fullWidth: true
  216. });
  217. chart.on('draw', function(data) {
  218. if(data.type === 'line' || data.type === 'area') {
  219. data.element.animate({
  220. d: {
  221. begin: 2000 * data.index,
  222. dur: 2000,
  223. from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
  224. to: data.path.clone().stringify(),
  225. easing: Chartist.Svg.Easing.easeOutQuint
  226. }
  227. });
  228. }
  229. });
  230. //-----demo-8
  231. var chart = new Chartist.Pie('.ct-chart-8', {
  232. series: [10, 20, 50, 20, 5, 50, 15],
  233. labels: [1, 2, 3, 4, 5, 6, 7]
  234. }, {
  235. donut: true,
  236. showLabel: false
  237. });
  238. chart.on('draw', function(data) {
  239. if(data.type === 'slice') {
  240. // Get the total path length in order to use for dash array animation
  241. var pathLength = data.element._node.getTotalLength();
  242. // Set a dasharray that matches the path length as prerequisite to animate dashoffset
  243. data.element.attr({
  244. 'stroke-dasharray': pathLength + 'px ' + pathLength + 'px'
  245. });
  246. // Create animation definition while also assigning an ID to the animation for later sync usage
  247. var animationDefinition = {
  248. 'stroke-dashoffset': {
  249. id: 'anim' + data.index,
  250. dur: 1000,
  251. from: -pathLength + 'px',
  252. to: '0px',
  253. easing: Chartist.Svg.Easing.easeOutQuint,
  254. // We need to use `fill: 'freeze'` otherwise our animation will fall back to initial (not visible)
  255. fill: 'freeze'
  256. }
  257. };
  258. // If this was not the first slice, we need to time the animation so that it uses the end sync event of the previous animation
  259. if(data.index !== 0) {
  260. animationDefinition['stroke-dashoffset'].begin = 'anim' + (data.index - 1) + '.end';
  261. }
  262. // We need to set an initial value before the animation starts as we are not in guided mode which would do that for us
  263. data.element.attr({
  264. 'stroke-dashoffset': -pathLength + 'px'
  265. });
  266. // We can't use guided mode as the animations need to rely on setting begin manually
  267. // See http://gionkunz.github.io/chartist-js/api-documentation.html#chartistsvg-function-animate
  268. data.element.animate(animationDefinition, false);
  269. }
  270. });
  271. // For the sake of the example we update the chart every time it's created with a delay of 8 seconds
  272. chart.on('created', function() {
  273. if(window.__anim21278907124) {
  274. clearTimeout(window.__anim21278907124);
  275. window.__anim21278907124 = null;
  276. }
  277. window.__anim21278907124 = setTimeout(chart.update.bind(chart), 10000);
  278. });
  279. //-----demo-9
  280. var data = {
  281. labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
  282. series: [
  283. [1, 2, 4, 8, 6, -2, -1, -4, -6, -2]
  284. ]
  285. };
  286. var options = {
  287. high: 10,
  288. low: -10,
  289. axisX: {
  290. labelInterpolationFnc: function(value, index) {
  291. return index % 2 === 0 ? value : null;
  292. }
  293. }
  294. };
  295. new Chartist.Bar('.ct-chart-9', data, options);
  296. //-----demo-10
  297. new Chartist.Bar('.ct-chart-10', {
  298. labels: ['Q1', 'Q2', 'Q3', 'Q4'],
  299. series: [
  300. [800000, 1200000, 1400000, 1300000],
  301. [200000, 400000, 500000, 300000],
  302. [100000, 200000, 400000, 600000]
  303. ]
  304. }, {
  305. stackBars: true,
  306. axisY: {
  307. labelInterpolationFnc: function(value) {
  308. return (value / 1000) + 'k';
  309. }
  310. }
  311. }).on('draw', function(data) {
  312. if(data.type === 'bar') {
  313. data.element.attr({
  314. style: 'stroke-width: 30px'
  315. });
  316. }
  317. });
  318. // ct-gauge-chart
  319. new Chartist.Pie('.ct-gauge-chart', {
  320. series: [20, 10, 30, 40]
  321. }, {
  322. donut: true,
  323. donutWidth: 60,
  324. startAngle: 270,
  325. total: 200,
  326. low:0,
  327. showLabel: false
  328. });
  329. //-----demo-11
  330. new Chartist.Bar('.ct-chart-11', {
  331. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
  332. series: [
  333. [5, 4, 3, 7, 5, 10, 3],
  334. [3, 2, 9, 5, 4, 6, 4]
  335. ]
  336. }, {
  337. seriesBarDistance: 10,
  338. reverseData: true,
  339. horizontalBars: true,
  340. axisY: {
  341. offset: 70
  342. }
  343. });
  344. //-----demo-12
  345. new Chartist.Bar('.ct-chart-12', {
  346. labels: ['Quarter 1', 'Quarter 2', 'Quarter 3', 'Quarter 4'],
  347. series: [
  348. [5, 4, 3, 7],
  349. [3, 2, 9, 5],
  350. [1, 5, 8, 4],
  351. [2, 3, 4, 6],
  352. [4, 1, 2, 1]
  353. ]
  354. }, {
  355. // Default mobile configuration
  356. stackBars: true,
  357. axisX: {
  358. labelInterpolationFnc: function(value) {
  359. return value.split(/\s+/).map(function(word) {
  360. return word[0];
  361. }).join('');
  362. }
  363. },
  364. axisY: {
  365. offset: 20
  366. }
  367. }, [
  368. // Options override for media > 400px
  369. ['screen and (min-width: 400px)', {
  370. reverseData: true,
  371. horizontalBars: true,
  372. axisX: {
  373. labelInterpolationFnc: Chartist.noop
  374. },
  375. axisY: {
  376. offset: 60
  377. }
  378. }],
  379. // Options override for media > 800px
  380. ['screen and (min-width: 800px)', {
  381. stackBars: false,
  382. seriesBarDistance: 10
  383. }],
  384. // Options override for media > 1000px
  385. ['screen and (min-width: 1000px)', {
  386. reverseData: false,
  387. horizontalBars: false,
  388. seriesBarDistance: 15
  389. }]
  390. ]);
  391. }); // End of use strict