chartist.setup.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // import Chartist from 'chartist';
  2. // Bar bipolar
  3. // -----------------------------------
  4. export const BarBipolar = {
  5. data: {
  6. labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10'],
  7. series: [
  8. [1, 2, 4, 8, 6, -2, -1, -4, -6, -2]
  9. ]
  10. },
  11. options: {
  12. high: 10,
  13. low: -10,
  14. height: 280,
  15. axisX: {
  16. labelInterpolationFnc: function(value, index) {
  17. return index % 2 === 0 ? value : null;
  18. }
  19. }
  20. }
  21. }
  22. // Bar Horizontal
  23. // -----------------------------------
  24. export const BarHorizontal = {
  25. data: {
  26. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
  27. series: [
  28. [5, 4, 3, 7, 5, 10, 3],
  29. [3, 2, 9, 5, 4, 6, 4]
  30. ]
  31. },
  32. options: {
  33. seriesBarDistance: 10,
  34. reverseData: true,
  35. horizontalBars: true,
  36. height: 280,
  37. axisY: {
  38. offset: 70
  39. }
  40. }
  41. }
  42. // Line
  43. // -----------------------------------
  44. export const Line = {
  45. data: {
  46. labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
  47. series: [
  48. [12, 9, 7, 8, 5],
  49. [2, 1, 3.5, 7, 3],
  50. [1, 3, 4, 5, 6]
  51. ]
  52. },
  53. options: {
  54. fullWidth: true,
  55. height: 280,
  56. chartPadding: {
  57. right: 40
  58. }
  59. }
  60. }
  61. // SVG Animation
  62. // -----------------------------------
  63. export const SVGAnimation = {
  64. data: {
  65. labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  66. series: [
  67. [1, 5, 2, 5, 4, 3],
  68. [2, 3, 4, 8, 1, 2],
  69. [5, 4, 3, 2, 1, 0.5]
  70. ]
  71. },
  72. options: {
  73. low: 0,
  74. showArea: true,
  75. showPoint: false,
  76. fullWidth: true,
  77. height: 300
  78. },
  79. events: {
  80. draw: function(data) {
  81. if (data.type === 'line' || data.type === 'area') {
  82. data.element.animate({
  83. d: {
  84. begin: 2000 * data.index,
  85. dur: 2000,
  86. from: data.path.clone().scale(1, 0).translate(0, data.chartRect.height()).stringify(),
  87. to: data.path.clone().stringify(),
  88. // easing: Chartist.Svg.Easing.easeOutQuint
  89. }
  90. });
  91. }
  92. }
  93. }
  94. }
  95. // Slim animation
  96. // -----------------------------------
  97. // Let's put a sequence number aside so we can use it in the event callbacks
  98. var seq = 0,
  99. delays = 80,
  100. durations = 500;
  101. export const SlimAnimation = {
  102. data: {
  103. labels: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
  104. series: [
  105. [12, 9, 7, 8, 5, 4, 6, 2, 3, 3, 4, 6],
  106. [4, 5, 3, 7, 3, 5, 5, 3, 4, 4, 5, 5],
  107. [5, 3, 4, 5, 6, 3, 3, 4, 5, 6, 3, 4],
  108. [3, 4, 5, 6, 7, 6, 4, 5, 6, 7, 6, 3]
  109. ]
  110. },
  111. options: {
  112. low: 0,
  113. height: 300
  114. },
  115. events: {
  116. // Once the chart is fully created we reset the sequence
  117. created: function() {
  118. seq = 0;
  119. },
  120. // On each drawn element by Chartist we use the Chartist.Svg API to trigger SMIL animations
  121. draw: function(data) {
  122. seq++;
  123. if (data.type === 'line') {
  124. // If the drawn element is a line we do a simple opacity fade in. This could also be achieved using CSS3 animations.
  125. data.element.animate({
  126. opacity: {
  127. // The delay when we like to start the animation
  128. begin: seq * delays + 1000,
  129. // Duration of the animation
  130. dur: durations,
  131. // The value where the animation should start
  132. from: 0,
  133. // The value where it should end
  134. to: 1
  135. }
  136. });
  137. } else if (data.type === 'label' && data.axis === 'x') {
  138. data.element.animate({
  139. y: {
  140. begin: seq * delays,
  141. dur: durations,
  142. from: data.y + 100,
  143. to: data.y,
  144. // We can specify an easing function from Chartist.Svg.Easing
  145. easing: 'easeOutQuart'
  146. }
  147. });
  148. } else if (data.type === 'label' && data.axis === 'y') {
  149. data.element.animate({
  150. x: {
  151. begin: seq * delays,
  152. dur: durations,
  153. from: data.x - 100,
  154. to: data.x,
  155. easing: 'easeOutQuart'
  156. }
  157. });
  158. } else if (data.type === 'point') {
  159. data.element.animate({
  160. x1: {
  161. begin: seq * delays,
  162. dur: durations,
  163. from: data.x - 10,
  164. to: data.x,
  165. easing: 'easeOutQuart'
  166. },
  167. x2: {
  168. begin: seq * delays,
  169. dur: durations,
  170. from: data.x - 10,
  171. to: data.x,
  172. easing: 'easeOutQuart'
  173. },
  174. opacity: {
  175. begin: seq * delays,
  176. dur: durations,
  177. from: 0,
  178. to: 1,
  179. easing: 'easeOutQuart'
  180. }
  181. });
  182. } else if (data.type === 'grid') {
  183. // Using data.axis we get x or y which we can use to construct our animation definition objects
  184. var pos1Animation = {
  185. begin: seq * delays,
  186. dur: durations,
  187. from: data[data.axis.units.pos + '1'] - 30,
  188. to: data[data.axis.units.pos + '1'],
  189. easing: 'easeOutQuart'
  190. };
  191. var pos2Animation = {
  192. begin: seq * delays,
  193. dur: durations,
  194. from: data[data.axis.units.pos + '2'] - 100,
  195. to: data[data.axis.units.pos + '2'],
  196. easing: 'easeOutQuart'
  197. };
  198. var animations = {};
  199. animations[data.axis.units.pos + '1'] = pos1Animation;
  200. animations[data.axis.units.pos + '2'] = pos2Animation;
  201. animations['opacity'] = {
  202. begin: seq * delays,
  203. dur: durations,
  204. from: 0,
  205. to: 1,
  206. easing: 'easeOutQuart'
  207. };
  208. data.element.animate(animations);
  209. }
  210. }
  211. }
  212. }