You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. "use strict";
  2. $(document).ready(function() {
  3. new Chartist.Line('#lineChart', {
  4. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
  5. series: [
  6. [12, 9, 7, 8, 5],
  7. [2, 1, 3.5, 7, 3],
  8. [1, 3, 4, 5, 6]
  9. ]
  10. }, {
  11. fullWidth: true,
  12. chartPadding: {
  13. right: 40
  14. }
  15. });
  16. new Chartist.Line('#lineChart_area', {
  17. labels: [1, 2, 3, 4, 5, 6, 7, 8],
  18. series: [
  19. [5, 9, 7, 8, 5, 3, 5, 4]
  20. ]
  21. }, {
  22. low: 0,
  23. showArea: true
  24. });
  25. var chart = new Chartist.Line('#lineChart_animation', {
  26. labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  27. series: [
  28. [12, 9, 7, 8, 5, 4, 6, 2, 3, 3, 4, 6],
  29. [4, 5, 3, 7, 3, 5, 5, 3, 4, 4, 5, 5],
  30. [5, 3, 4, 5, 6, 3, 3, 4, 5, 6, 3, 4],
  31. [3, 4, 5, 6, 7, 6, 4, 5, 6, 7, 6, 3]
  32. ]
  33. }, {
  34. low: 0
  35. });
  36. // Let's put a sequence number aside so we can use it in the event callbacks
  37. var seq = 0,
  38. delays = 80,
  39. durations = 500;
  40. // Once the chart is fully created we reset the sequence
  41. chart.on('created', function() {
  42. seq = 0;
  43. });
  44. // On each drawn element by Chartist we use the Chartist.Svg API to trigger SMIL animations
  45. chart.on('draw', function(data) {
  46. seq++;
  47. if (data.type === 'line') {
  48. // If the drawn element is a line we do a simple opacity fade in. This could also be achieved using CSS3 animations.
  49. data.element.animate({
  50. opacity: {
  51. // The delay when we like to start the animation
  52. begin: seq * delays + 1000,
  53. // Duration of the animation
  54. dur: durations,
  55. // The value where the animation should start
  56. from: 0,
  57. // The value where it should end
  58. to: 1
  59. }
  60. });
  61. } else if (data.type === 'label' && data.axis === 'x') {
  62. data.element.animate({
  63. y: {
  64. begin: seq * delays,
  65. dur: durations,
  66. from: data.y + 100,
  67. to: data.y,
  68. // We can specify an easing function from Chartist.Svg.Easing
  69. easing: 'easeOutQuart'
  70. }
  71. });
  72. } else if (data.type === 'label' && data.axis === 'y') {
  73. data.element.animate({
  74. x: {
  75. begin: seq * delays,
  76. dur: durations,
  77. from: data.x - 100,
  78. to: data.x,
  79. easing: 'easeOutQuart'
  80. }
  81. });
  82. } else if (data.type === 'point') {
  83. data.element.animate({
  84. x1: {
  85. begin: seq * delays,
  86. dur: durations,
  87. from: data.x - 10,
  88. to: data.x,
  89. easing: 'easeOutQuart'
  90. },
  91. x2: {
  92. begin: seq * delays,
  93. dur: durations,
  94. from: data.x - 10,
  95. to: data.x,
  96. easing: 'easeOutQuart'
  97. },
  98. opacity: {
  99. begin: seq * delays,
  100. dur: durations,
  101. from: 0,
  102. to: 1,
  103. easing: 'easeOutQuart'
  104. }
  105. });
  106. } else if (data.type === 'grid') {
  107. // Using data.axis we get x or y which we can use to construct our animation definition objects
  108. var pos1Animation = {
  109. begin: seq * delays,
  110. dur: durations,
  111. from: data[data.axis.units.pos + '1'] - 30,
  112. to: data[data.axis.units.pos + '1'],
  113. easing: 'easeOutQuart'
  114. };
  115. var pos2Animation = {
  116. begin: seq * delays,
  117. dur: durations,
  118. from: data[data.axis.units.pos + '2'] - 100,
  119. to: data[data.axis.units.pos + '2'],
  120. easing: 'easeOutQuart'
  121. };
  122. var animations = {};
  123. animations[data.axis.units.pos + '1'] = pos1Animation;
  124. animations[data.axis.units.pos + '2'] = pos2Animation;
  125. animations['opacity'] = {
  126. begin: seq * delays,
  127. dur: durations,
  128. from: 0,
  129. to: 1,
  130. easing: 'easeOutQuart'
  131. };
  132. data.element.animate(animations);
  133. }
  134. });
  135. // For the sake of the example we update the chart every time it's created with a delay of 10 seconds
  136. chart.on('created', function() {
  137. if (window.__exampleAnimateTimeout) {
  138. clearTimeout(window.__exampleAnimateTimeout);
  139. window.__exampleAnimateTimeout = null;
  140. }
  141. window.__exampleAnimateTimeout = setTimeout(chart.update.bind(chart), 12000);
  142. });
  143. var data = {
  144. labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
  145. series: [
  146. [1, 2, 4, 8, 6, -2, -1, -4, -6, -2]
  147. ]
  148. };
  149. var options = {
  150. high: 10,
  151. low: -10,
  152. axisX: {
  153. labelInterpolationFnc: function(value, index) {
  154. return index % 2 === 0 ? value : null;
  155. }
  156. }
  157. };
  158. new Chartist.Bar('#barChart_bipolar', data, options);
  159. var data = {
  160. labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  161. series: [
  162. [5, 4, 3, 7, 5, 10, 3, 4, 8, 10, 6, 8],
  163. [3, 2, 9, 5, 4, 6, 4, 6, 7, 8, 7, 4]
  164. ]
  165. };
  166. var options = {
  167. seriesBarDistance: 10
  168. };
  169. var responsiveOptions = [
  170. ['screen and (max-width: 640px)', {
  171. seriesBarDistance: 5,
  172. axisX: {
  173. labelInterpolationFnc: function(value) {
  174. return value[0];
  175. }
  176. }
  177. }]
  178. ];
  179. new Chartist.Bar('#barChart_overlapping', data, options, responsiveOptions);
  180. new Chartist.Bar('#barChart_responsive', {
  181. labels: ['Quarter 1', 'Quarter 2', 'Quarter 3', 'Quarter 4'],
  182. series: [
  183. [5, 4, 3, 7],
  184. [3, 2, 9, 5],
  185. [1, 5, 8, 4],
  186. [2, 3, 4, 6],
  187. [4, 1, 2, 1]
  188. ]
  189. }, {
  190. // Default mobile configuration
  191. stackBars: true,
  192. axisX: {
  193. labelInterpolationFnc: function(value) {
  194. return value.split(/\s+/).map(function(word) {
  195. return word[0];
  196. }).join('');
  197. }
  198. },
  199. axisY: {
  200. offset: 20
  201. }
  202. }, [
  203. // Options override for media > 400px
  204. ['screen and (min-width: 400px)', {
  205. reverseData: true,
  206. horizontalBars: true,
  207. axisX: {
  208. labelInterpolationFnc: Chartist.noop
  209. },
  210. axisY: {
  211. offset: 60
  212. }
  213. }],
  214. // Options override for media > 800px
  215. ['screen and (min-width: 800px)', {
  216. stackBars: false,
  217. seriesBarDistance: 10
  218. }],
  219. // Options override for media > 1000px
  220. ['screen and (min-width: 1000px)', {
  221. reverseData: false,
  222. horizontalBars: false,
  223. seriesBarDistance: 15
  224. }]
  225. ]);
  226. var data = {
  227. series: [5, 3, 4]
  228. };
  229. var sum = function(a, b) {
  230. return a + b
  231. };
  232. new Chartist.Pie('#pieChart', data, {
  233. labelInterpolationFnc: function(value) {
  234. return Math.round(value / data.series.reduce(sum) * 100) + '%';
  235. }
  236. });
  237. new Chartist.Pie('#guageChart', {
  238. series: [20, 10, 30, 40]
  239. }, {
  240. donut: true,
  241. donutWidth: 60,
  242. startAngle: 270,
  243. total: 200,
  244. showLabel: false
  245. });
  246. var chart = new Chartist.Pie('#donutChart_animated', {
  247. series: [10, 20, 50, 20, 5, 50, 15],
  248. labels: [1, 2, 3, 4, 5, 6, 7]
  249. }, {
  250. donut: true,
  251. showLabel: false
  252. });
  253. chart.on('draw', function(data) {
  254. if (data.type === 'slice') {
  255. // Get the total path length in order to use for dash array animation
  256. var pathLength = data.element._node.getTotalLength();
  257. // Set a dasharray that matches the path length as prerequisite to animate dashoffset
  258. data.element.attr({
  259. 'stroke-dasharray': pathLength + 'px ' + pathLength + 'px'
  260. });
  261. // Create animation definition while also assigning an ID to the animation for later sync usage
  262. var animationDefinition = {
  263. 'stroke-dashoffset': {
  264. id: 'anim' + data.index,
  265. dur: 1000,
  266. from: -pathLength + 'px',
  267. to: '0px',
  268. easing: Chartist.Svg.Easing.easeOutQuint,
  269. // We need to use `fill: 'freeze'` otherwise our animation will fall back to initial (not visible)
  270. fill: 'freeze'
  271. }
  272. };
  273. // 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
  274. if (data.index !== 0) {
  275. animationDefinition['stroke-dashoffset'].begin = 'anim' + (data.index - 1) + '.end';
  276. }
  277. // We need to set an initial value before the animation starts as we are not in guided mode which would do that for us
  278. data.element.attr({
  279. 'stroke-dashoffset': -pathLength + 'px'
  280. });
  281. // We can't use guided mode as the animations need to rely on setting begin manually
  282. // See http://gionkunz.github.io/chartist-js/api-documentation.html#chartistsvg-function-animate
  283. data.element.animate(animationDefinition, false);
  284. }
  285. });
  286. // For the sake of the example we update the chart every time it's created with a delay of 8 seconds
  287. chart.on('created', function() {
  288. if (window.__anim21278907124) {
  289. clearTimeout(window.__anim21278907124);
  290. window.__anim21278907124 = null;
  291. }
  292. window.__anim21278907124 = setTimeout(chart.update.bind(chart), 10000);
  293. });
  294. });